Proofs: Kruskal's algorithm
These are the human proofs of Kruskal's algorithm, at full length.
Kruskal's algorithm returns a minimum spanning forest full
This proof shows: correctness, termination.
Claim. Let be a finite undirected graph with a real weight on each edge. Weights may tie, may have parallel edges and self-loops, and need not be connected. Then KRUSKAL stops, and the set that it returns is a minimum spanning forest of : a forest with the same connected components as , of least total weight among all such forests.
Setup. Let be the edges in the order of line L3, so . Let be the set after the edge is processed, with . A spanning forest of is an acyclic set of edges whose components are the components of . A forest on vertices with trees has edges (tree edge count), so each spanning forest of has edges, where is the number of components of . A minimum spanning forest exists, because is finite and contains a spanning forest (keep a spanning tree of each component).
Lemma 1 (the sets). After each step, the sets of the disjoint-set structure are the vertex sets of the trees of , and is acyclic.
Proof. By induction on the steps. At the start the sets are the single vertices and . In step , with , line L5 compares the sets of and . If they are equal, nothing changes. If they differ, then by the induction hypothesis and are in different trees of . Adding joins these two trees into one tree and closes no cycle, and UNION merges exactly their two sets.
Lemma 2 (the cut step). Let for a minimum spanning forest . Let be a set such that no edge of has exactly one end in . Let be an edge with exactly one end in and with for each edge that has exactly one end in . Then is a subset of some minimum spanning forest.
Proof. If , take . Otherwise let with and . The edge joins and , so they are in one component of , and so in one tree of . Let be the path from to in that tree. The path starts in and ends outside , so it has an edge with exactly one end in . The edge is not in , by the choice of . Let . Removing splits its tree into two trees, one with and one with , because is on the only path between them. Adding joins these two trees again. So is acyclic and has the same components as , and is a spanning forest. Its weight is , so is a minimum spanning forest. It contains , because , and it contains .
Invariant. For each from to : there is a minimum spanning forest with .
- Initialization. is a subset of each minimum spanning forest.
- Maintenance, edge dropped. Then and works.
- Maintenance, edge added. Let , and let be the vertex set of the tree of that holds . By Lemma 1, , so has exactly one end in . No edge of has exactly one end in , because is the vertex set of a tree of the forest. Let be any edge with exactly one end in . If , then at step the edge was either added, and then , which is false, or dropped, and then its two ends were in one set at step . Sets only merge, so its two ends are in one tree of , and both are in or both are outside , which is also false. So and by the order of the sort. Lemma 2, with and , gives a minimum spanning forest .
Termination. The loop of line L4 runs once for each of the edges, and each run does a bounded number of disjoint-set operations, each of which stops. So the algorithm stops after steps.
Conclusion. Let . Each edge had both ends in one set at step , so both ends are in one tree of . Each edge of also has both ends in one tree of . So each path of stays inside one tree of , and each component of lies inside one tree of . Since , each tree of lies inside one component of . So has the components of , and by Lemma 1 it is acyclic: is a spanning forest with edges. The invariant gives , and also has edges, so . Therefore is a minimum spanning forest. When is connected, and is a minimum spanning tree.