# ClassS04CS141/MSTExerciseSolutions

ClassS04CS141 | ClassS04CS141 | recent changes | Preferences

### Minimum spanning tree exercise solutions

Review the proof of correctness for Kruskal's algorithm: MinimumSpanningTreesByKruskals .

#### (i) Prim's algorithm:

``` 0. Input: weighted graph G=(V,E).  Output: MST S.
1. Choose any "start" vertex s and let C = {s}.
2. Let S={}.
3. Until C == V do
4.    From all the edges leaving C, choose a minimum-weight one (u,w).
7. Return S.
```

claim: Prim's algorithm is correct. That is, given any connected weighted graph, the algorithm returns a minimum spanning tree.

exercise: prove or disprove the claim.

stepping stone: prove that the first edge chosen by the algorithm is in some MST

claim: The algorithm maintains the invariant that the set of chosen edges is contained in some MST.

proof: The invariant is true initially before any edge is chosen.

Suppose the invariant is true just before the algorithm adds an edge (u,w) to S. That is, the edges in S are in some MST T.

We need to convince ourselves that it is still true after the algorithm adds the edge. That is, that the edges in S, and (u,w), are in some MST T'.

If the edge (u,w) is in T, we are done.

Otherwise, there is a path p from u to w in T. Since u is in S and w is not, some edge e' on this path leaves S. Since (u,w) is a minimum-weight edge among those leaving S, WT(e) ≤ WT(e'). Therefore, adding (u,w) to T and removing e' gives a spanning tree T' that contains all the edges in S, and (u,w), and is a MST (since it has weight at most the weight of T, which by assumption was a MST).

#### (ii) cycle-breaking algorithm

``` 1. While the graph contains cycles do:
2.   Find a cycle C.
3.   Find a maximum-weight edge e on C.
4.   Delete e from the graph.
5. Return the tree formed by the remaining edges.
```

claim: This algorithm is correct. That is, given any connected weighted graph, the algorithm returns a minimum spanning tree.

exercise: prove or disprove the claim.

stepping stone: prove that there is an MST that does not contain the first deleted edge

claim: The algorithm maintains the invariant that there is an MST of the original graph in the set of remaining edges.

proof: The invariant is true initially before any edge is deleted.

Suppose the invariant is true just before the algorithm deletes an edge (u,w) to S. That is, the remaining edges contain some MST T of the original graph.

We need to convince ourselves that it is still true after the algorithm deletes the edge. That is, that the remaining edges (not including (u,w)) contain some MST T' of the original graph.

If the edge (u,w) is not in T, we are done.

Otherwise, consider removing edge (u,w) from T. This splits T into two components. Since u and w are each in one of the components, some edge e' (other than (u,w)) on the cycle C connects the two components. Thus, adding e' to T and removing (u,w) gives a spanning tree T' that contains neither (u,w) nor any of the other edges deleted so far. Furthermore, WT(e') ≤ WT(e), since e is a maximum-weight edge on the cycle C, so T' must also be a minimum-weight spanning tree.

#### (iii) uniqueness of MSTs

claim: In any connected, weighted graph with distinct edge weights (no two edges have the same weight), there is exactly one minimum spanning tree.

exercise: prove or disprove the claim.

stepping stone: prove that if the min-wt edge is unique, then every MST contains it

proof: Suppose for contradiction that T and T' are two different spanning trees. We will show that one of them is not an MST.

Let (u,w) be the minimum-weight edge that is in one tree but not the other. Suppose that (u,w) is in T but not T'. (If it's the other way around, then a similar argument will apply.)

Some path p in T' connects u to w. Some edge e' on p is in T' but not T (otherwise T would have a cycle). Adding (u,w) to T' and removing e' creates a third spanning tree T''. Since we chose (u,w) to be the minimum-weight edge among those in one tree but not the other, WT(u,w) < WT(e'). Thus, T'' has strictly smaller weight than T' does. Thus, T' is not an MST.

### References

ClassS04CS141 | ClassS04CS141 | recent changes | Preferences