Solutions to Homework Four
CSE 101
5.1. The graph has two minimum spanning trees, of cost 19. There are several different orders in which Kruskal might potentially add the edges, for instance: Edge A−E E−F B−F F −G G−H C −G D−G
one side of cut {A} {A, E} {A, E, F } {A, B, E, F } {H} {C} {D}
5.4. Claim: A graph G with k connected components must have at least |V | − k edges. Take any such G = (V, E) and run an iterative trimming procedure on it: “while there is a cycle, remove an edge from the cycle”. The result is a forest of k trees; say they contain n1 , n2 , . . . , nk nodes respectively (where n1 + · · · + nk = |V |). Then they must contain n1 − 1, . . . , nk − 1 edges respectively, for a total of |V | − k. Therefore G must originally have had at least this many edges. 5.6. Claim: If all edges in an undirected graph have different weights, then the MST is unique. Suppose this isn’t the case – suppose there are two distinct MSTs T and T ′ . We’ll find a contradiction. Let e be the lightest edge which is present in one of the MSTs but not the other. Say it’s in T but not T ′ (it doesn’t make a difference either way). Then T ′ ∪ {e} contains a cycle C. There must be some edge e′ of this cycle which is not in T (T can’t contain all of C because it is a tree). Then T ′′ = T ′ ∪ {e} − {e′ } is once again a tree. However, notice that e′ is part of T ′ but not T . Since e was the lightest edge of this type (ie. in one tree but not the other), and since edge weights are distinct, w(e) < w(e′ ). But then T ′′ has weight weight(T ′′ ) = weight(T ′ ) + w(e) − w(e′ ) < weight(T ′ ), a contradiction since T ′ is an MST. 5.11. Path compression example.
8
1
2
4
5
6
7
3 5.22. (a) Claim: Pick any cycle in a graph, and let e be the heaviest edge in that cycle. Then there is a minimum spanning tree that does not contain e. Call the cycle C. We’ll show that for any spanning tree T that contains e, there is another spanning tree T ′ which doesn’t contain e and whose weight is at most that of T . Remove e from T ; this splits T into two subtrees, T1 and T2 . Since e crosses the cut (T1 , T2 ), the cycle C must contain at least one other edge e′ across this cut. Let T ′ = T − e + e′ . T ′ is connected and has |V | − 1 edges; therefore it is a spanning tree. And since w(e) ≥ w(e′ ), the weight of T ′ is at most that of T . 1
(b) Prove that the new MST algorithm is correct. Let Gt be what remains of the graph after t iterations of the loop. Part (a) tells us that for any t, we have MST(Gt+1 ) = MST(Gt ); by induction, we therefore have MST(GT ) = MST(G), where GT is the final graph. This GT has no remaining cycles and is therefore a spanning tree, whereby it must be a minimum spanning tree of G. (c) Give a linear-time algorithm to check if G has a cycle containing a specific edge e. Algorithm: If e = {u, v}, run explore(G − e, u). If v is visited return yes else return no. Justification: G has a cycle containing e if and only if there is a path from u to v in G − e. (d) What is the overall time taken by this algorithm? Sorting takes O(|E| log |E|) and there are |E| iterations of the loop, each of which takes time O(|E|). The total is therefore O(|E|2 ). 5.26. Here’s a problem that occurs in automatic program analysis. For a set of variables x1 , . . . , xn , you are given some equality constraints, of the form “xi = xj ” and some disequality constraints, of the form “xi 6= xj .” Is it possible to satisfy all of them? Give an efficient algorithm that takes as input m constraints over n variables and decides whether the constraints can be satisfied. Denote the set of variables by V . Let E be the equality constraints and D the disequality constraints. for all u ∈ V : makeset(u) for all (u, v) ∈ E: union(u, v) for all (u, v) ∈ D: if find(u) = find(v): halt and output ‘‘not satisfiable’’ output ‘‘satisfiable’’ Justification: Consider an undirected graph with nodes V and edges E. The connected components of this graph are precisely the sets of variables which must have the same value. The problem is not satisfiable if and only if there is a disequality constraint between two of these connected components. Running time: Since union and find operations take time O(log n), the overall running time is O(n + (|D| + |E|) log n) = O(n + m log n).
2