Proofs: Greedy MST algorithm
These are the human proofs of Greedy MST algorithm, at full length.
The greedy MST algorithm returns a minimum spanning forest for every order of the blue and red rules full
This proof shows: correctness, termination.
Let be a finite undirected graph with a real weight on each edge. Parallel edges and self-loops are allowed, weights may tie, and need not be connected. A spanning forest is a forest with the same components as ; it has edges, where is the number of components. A minimum spanning forest (MSF) is a spanning forest of least total weight. In a connected graph it is a minimum spanning tree.
During the run each edge is uncolored, blue or red. We write for the set of blue edges and for the set of red edges.
The invariant
Invariant I. Some minimum spanning forest has and .
Lemma 1 (start). Invariant I holds after line L1.
Proof. , and has a spanning forest, so it has a minimum one.
Lemma 2 (blue rule). Assume Invariant I holds with the forest . Let be a cut that no blue edge crosses, and let be a lightest uncolored edge that crosses it, with . Then Invariant I holds after is colored blue.
Proof. If , the forest still works. Otherwise has exactly one cycle, the fundamental cycle of : the edge and the path in from to . (The path exists: and are in one component of , and spans it.) The path starts in and ends outside , so it has an edge that crosses the cut. Then:
- is not blue, because no blue edge crosses the cut;
- is not red, because and ;
- so is uncolored, and since is a lightest uncolored crossing edge, .
Let . Removing splits one tree of into two, with and on different sides, since lies on their only path; adding joins them again. So is a spanning forest, and , so is an MSF. It contains , since . It avoids , since was uncolored and avoids .
Lemma 3 (red rule). Assume Invariant I holds with the forest . Let be a simple cycle with no red edge, and let be a heaviest uncolored edge of . Then Invariant I holds after is colored red.
Proof. If , the forest still works. Otherwise , so is not a self-loop and is a path from to . Removing splits the tree of that holds it into two trees; let be the vertex set of the one that holds , so . The path starts in and ends outside it, so it has an edge with exactly one end in . Then:
- , because the only edge of with exactly one end in is , and ;
- is not blue, because ;
- is not red, because has no red edge;
- so is uncolored, and since is a heaviest uncolored edge of , .
Let . The edge joins the two trees again, so is a spanning forest with , hence an MSF. It contains , since ( was uncolored). It avoids , since is uncolored.
The scheme never gets stuck
Lemma 4 (applicability). Before each step, the blue edges form a forest. If an edge is uncolored, then a rule applies in the way lines L3 to L8 describe.
Proof. By Invariant I, and is a forest, so is a forest.
- If and are in different blue trees, let be a union of blue trees that holds and not (for example the blue tree of ). No blue edge has exactly one end in , and the uncolored edge crosses the cut. So the blue rule applies to this cut.
- If and are in one blue tree, let be the blue path from to (empty if is a self-loop). Then is a simple cycle: since is uncolored, and is a simple path. It has no red edge and contains the uncolored edge , so the red rule applies. If line L7 uses any other path of non-red edges from to that avoids , the same holds for the cycle it gives; the implementation finds such a path by breadth-first search, which gives a simple path.
Termination and the result
Theorem. For each sequence of choices, GREEDY-MST stops after exactly steps, and the set it returns is a minimum spanning forest of .
Proof. Each step colors exactly one uncolored edge (Lemma 4 shows that a step exists while an edge is uncolored), and no color changes. So the loop runs exactly times. By Lemmas 1, 2 and 3 and induction on the steps, Invariant I holds at the end, with some MSF . Then every edge is blue or red, and , so . Hence , an MSF.
Remark (ties). Lemmas 2 and 3 use only and . So any lightest or heaviest uncolored edge may be chosen when weights tie, and different choices may give different minimum spanning forests, all of the least weight.