Docsity

Counterexamples & Algorithms in Graph Theory: DFS & Dynamic Edge Weights, Exams of Algorithms and Programming

Toggle sidebar
bg1
CS–170 Efficient Algorithms & Intractable Problems, Spring 2006
Midterm 1 Solutions
Note: These solutions are not necessarily model answers. Rather, they are designed to be tutorial in nature,
and sometimes contain a little more explanation than an ideal solution (especially in Q4). Also, bear in mind
that there may be more than one correct solution. You should read through these solutions carefully, even
for problems that you answered correctly! The maximum total number of points available is 65.
1. True or False?
(i) False. One possible counterexample is the following: 3pts
f(n) = n2;g(n) = (nif nis odd;
n3if nis even.
Then we have f(n)
g(n)=nfor all odd n, and so when nis large enough we cannot have f(n)≤Cg(n)
for any constant C. Thus f(n)6=O(g(n). Similarly, considering even nwe see that g(n)6=O(f(n)).
The key point here isthat the definition of big-O requires that f(n)≤Cg(n)for all large enough n; by
making the function g(n)oscillate between values that are much bigger and much smaller than f(n),
we can ensure that neither f(n)≤Cg(n)nor g(n)≤Cf (n)holds as n→ ∞. Of course, to get
this weird behavior we have to cook up a slightly strange function g(n); for most functions that we
encounter in the analysis of algorithms, this doesn’t happen.
Not many people got this part right. The main bogus answer was to say “True, because either f(n)
has to grow at least as fast as g(n)or vice versa.” This and other incorrect answers did not work
with the exact definition of big-O. Some people got close to a valid counterexample by trying to define
two oscillating functions (e.g., sin(n)and cos(n)); many of these attempts were correct in spirit, but
sometimes fell down on details such as f(n)and g(n)not tending to infinity.
(ii) True. If pre(u)<post(u)<pre(v)<post(v)then the DFS backtracks from ubefore vertex vis 3pts
discovered. But if there were an edge between uand vthen the search would have to explore this edge
(and hence discover v) before backtracking from u.
Most people got this right.
(iii) False. If pre(u)<post(u)<pre(v)<post(v)then there could still be a directed edge from vto u;3pts
this would be a cross edge for the DFS. Here is asmall example, showing the pre- and post-visit labels
(the search starts at s):
vu
4,52,3
1,6 s
Most people got this right.
pf3
pf4
pf5

Partial preview of the text

Download Counterexamples & Algorithms in Graph Theory: DFS & Dynamic Edge Weights and more Exams Algorithms and Programming in PDF only on Docsity!

CS–170 Efficient Algorithms & Intractable Problems, Spring 2006

Midterm 1 Solutions

Note: These solutions are not necessarily model answers. Rather, they are designed to be tutorial in nature, and sometimes contain a little more explanation than an ideal solution (especially in Q4). Also, bear in mind that there may be more than one correct solution. You should read through these solutions carefully, even for problems that you answered correctly! The maximum total number of points available is 65.

1. True or False?

(i) False. One possible counterexample is the following: 3pts

f (n) = n^2 ; g(n) =

n if n is odd; n^3 if n is even.

Then we have f g^ ((nn)) = n for all odd n, and so when n is large enough we cannot have f (n) ≤ Cg(n) for any constant C. Thus f (n) 6 = O(g(n). Similarly, considering even n we see that g(n) 6 = O(f (n)). The key point here is that the definition of big-O requires that f (n) ≤ Cg(n) for all large enough n; by making the function g(n) oscillate between values that are much bigger and much smaller than f (n), we can ensure that neither f (n) ≤ Cg(n) nor g(n) ≤ Cf (n) holds as n → ∞. Of course, to get this weird behavior we have to cook up a slightly strange function g(n); for most functions that we encounter in the analysis of algorithms, this doesn’t happen. Not many people got this part right. The main bogus answer was to say “True, because either f (n) has to grow at least as fast as g(n) or vice versa.” This and other incorrect answers did not work with the exact definition of big-O. Some people got close to a valid counterexample by trying to define two oscillating functions (e.g., sin(n) and cos(n) ); many of these attempts were correct in spirit, but sometimes fell down on details such as f (n) and g(n) not tending to infinity.

(ii) True. If pre(u) < post(u) < pre(v) < post(v) then the DFS backtracks from u before vertex v is 3pts discovered. But if there were an edge between u and v then the search would have to explore this edge (and hence discover v) before backtracking from u. Most people got this right.

(iii) False. If pre(u) < post(u) < pre(v) < post(v) then there could still be a directed edge from v to u; 3pts this would be a cross edge for the DFS. Here is a small example, showing the pre- and post-visit labels (the search starts at s):

u v

s

Most people got this right.

(iv) False. Let v be the vertex with lowest post-visit label. There could be a back edge from v to one of its 3pts ancestors a, but it is not necessary that v is reachable from every vertex that is reachable from a. Here is a simple counterexample:

v u

a

Many people got this wrong. The most common mistake was to claim “True” because the only vertices reachable from v are its ancestors, and each of them certainly has a path to v_. This argument fails to realize that those ancestors may themselves be able to reach other vertices that are not ancestors of_ v , and indeed from which v is not reachable at all, such as u in the above example.

(v) False. The same example as in part (iv) works here: a is the vertex with highest post-visit label, and 3pts all vertices are reachable from a, but not all vertices are in the same SCC. Most people got this right.

(vi) False. Consider the tree below, which specifies a prefix-free code for an alphabet of five letters. This 3pts tree can arise (e.g.) from the following frequencies: 60%, 10%, 10%, 10%, 10%.

10% 10% 10% 10%

60%

The most common mistake here was to say “True because otherwise the Huffman tree would not be a full tree.” But as the example above demonstrates, the tree can be full without any leaves at level 2.

2. Divide-and-Conquer

(a) Recurrence: T (n) = 5T (n/2) + Θ(n^2 ). 4pts Solution: From the Master Theorem with a = 5, b = 2, d = 2, we get that T (n) = Θ(nlogb^ a) = Θ(nlog^2 5 ).

(b) Recurrence: T (n) = 3T (n/2) + Θ(n^2 ). 4pts Solution: From the Master Theorem with a = 3, b = 2, d = 2, we get that T (n) = Θ(nd) = Θ(n^2 ).

(c) Recurrence: T (n) =

nT (

n ) + Θ(n). 4pts

procedure ModifiedDijkstra(G, l, s, t 0 ): for all u ∈ V : time(u) = ∞ prev(u) = nil time(s) = t 0

H = makeheap(V ) (using time values as keys) while H is not empty: u = deletemin(H) for all edges (u, v) ∈ E: if time(v) > time(u) + (u,v)(time(u)) : time(v) = time(u) +(u,v)(time(u)) prev(v) = u decreasekey(H, v)

After running this algorithm, the fastest route from s to v can be obtained easily using by backtracking using the prev labels. The most common mistake here was just to say “use the current time stamp to query the black box to get the travel time of the edge.” But many people did not indicate how to keep track of that time stamp, and simply used t in the query, rather than time(u). 2pts (c) As argued in part (b), the physicality constraint allows us to assume that we leave a vertex as soon as we arrive at it. This is the reason we can use the arrival time time(u) to query the travel time for edges in the update operation. But if the physicality constraint is removed, we might be able to wait at some vertex u until some time t′^ > time(u) such that t′^ + e(t′) < time(u) +e(time(u)). If we could figure out the optimal waiting time t′^ at u, then we could still use the above algorithm with time(u) + (u,v)(time(u)) replaced by t′^ +e(t′). However, in the absence of other information we don’t have any way to find the optimal t′. Many people just said that removing the physicality constraint is the same as introducing negative edges into the graph (without explaining why). Actually this answer is inadequate for at least two rea- sons: (1) the connection with negative edges doesn’t really make sense; (2) even if this were the same as introducing negative edges, it wouldn’t show that the algorithm fails because Dijkstra’s algorithm does not necessarily always fail with negative edges.

4. Yet another MST algorithm

(a) First, since the graph is connected, clearly there is an MST of G. Now there are two reasonable 4pts approaches for this problem: Using the Cut Property: Let X = ∅, S = {v}, and consider the cut between S and V − S. Clearly ev is the minimum edge crossing this cut, so by the Cut Property ev is in some MST of G. Proof by contradiction: Suppose there exists an MST T of G that doesn’t contain ev. Add ev to T. Obviously this creates a cycle. Consider the other edge incident on v, e′ v. If we remove this edge, since w(ev ) < w(e′ v ), then we destroy the cycle and create a new tree T ′^ with cost(T ′) < cost (T ). This contradicts the assumption that T is an MST. Thus we can conclude that all MSTs of G contain ev. Almost everyone got this question right. However, we need to apologize. We should have asked you to prove that ev is in all MSTs of G_. Notice that this follows directly from the proof by contradiction, but not from the Cut Property proof. The reason we should have done this is because this fact greatly simplifies the proof of part (b)._

(b) There are basically three parts to this proof: (1) showing that adding all the ev to X in one iteration of 5pts the while loop is correct; (2) showing that the contraction operation does the right thing; (3) showing that all the iterations taken together lead to the MST of G. (1) By the proof by contradiction in part (a), we know that all the ev selected in the first loop of the algorithm belong to an MST of G, and thus it is OK to add them to X. (This can also be deduced from the weaker version of part (a), with a little bit of extra work.) (2) First, note that the set of edges ev that are contracted form a forest (no cycles) in G; this follows from the fact that all edge weights are unique. Contracting the edges ev causes each connected com- ponent (tree) of this forest to collapse into a single vertex, which means that we will never consider any of the other edges internal to the component. But this is correct because we already have a path between all vertices in the component, and any further edges would create a cycle. Furthermore, con- sider two edges coming out of such a component, both of which point to a node w. Choosing either of these edges will connect the component to w, and hence make every vertex in the component reachable from w. Since all edge weights are unique, we know that one edge is cheaper. Thus, we can simply throw away the more expensive edge. This is exactly what contraction does. (3) After performing contraction, we now have a new set of vertices (essentially “super-vertices”) that themselves need to be connected. We can again appeal to the proof by contradiction in part (a) to choose edges for the MST of this reduced graph G′. This is exactly what happens in the second iteration. We continue in this fashion until the vertex set V consists of a single super-vertex. At this point we have created a single connected component whose edge set X forms a spanning tree of G. This was a hard question, especially because we didn’t give you the best tool to use from part (a). Nonetheless, it was doable, and we were surprised that nobody quite managed full points. The most common mistake was to miss parts of the analysis. When you are analyzing an algorithm, you must consider all the key operations. Many people either discussed the choice of minimum edges, or con- traction, but not both. Both are needed in any completely correct answer!

(c) Let Vk be the set of vertices considered in the kth iteration. So V 1 = V. In the kth iteration, we 4pts choose |Vk| edges to contract, but these edges are not necessarily all distinct. However, any edge can be selected at most twice (e.g., edge (u, v) could be chosen both as eu and as ev). Hence we contract at least |Vk|/ 2 edges in this iteration, so we reduce the number of vertices by at least a factor of 2. But clearly we only need to do this at most log 2 |V | times before |V | reaches 1. [Note that the algorithm might perform much better than this; in fact, it is even possible that we could form the entire MST in one iteration!] Most students got this. One common mistake was to jump from noticing that there are |Vk| contractions at iteration k , and then say that thus there are |Vk|/ 2 vertices left over after the contractions. This misses the core part of the argument. Another common mistake was to say that in the best case there are |Vk|/ 2 contractions, rather than in the worst case.

(d) Here is a concrete implementation of this algorithm that uses disjoint sets with path compression and 5pts union-by-rank to handle contracting nodes. You did not need to provide this algorithm, but it will help anchor the explanation below.

function MST(G = (V, E)) X = ∅ for each v ∈ V : MAKESET(v) while |V | > 1 : for each e = (vi, vj ) ∈ E: pi := FIND(vi) pj := FIND(vj) if pi 6 = pj :