Algorithm Search⌘K

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 G=(V,E)G = (V, E) be a finite undirected graph with a real weight w(e)w(e) on each edge. Weights may tie, GG may have parallel edges and self-loops, and GG need not be connected. Then KRUSKAL(G,w)(G, w) stops, and the set FF that it returns is a minimum spanning forest of GG: a forest with the same connected components as GG, of least total weight among all such forests.

Setup. Let e1,e2,…,eme_1, e_2, \ldots, e_m be the edges in the order of line L3, so w(e1)≤w(e2)≤⋯≤w(em)w(e_1) \le w(e_2) \le \cdots \le w(e_m). Let FkF_k be the set FF after the edge eke_k is processed, with F0=∅F_0 = \emptyset. A spanning forest of GG is an acyclic set of edges whose components are the components of GG. A forest on nn vertices with tt trees has n−tn - t edges (tree edge count), so each spanning forest of GG has n−cn - c edges, where cc is the number of components of GG. A minimum spanning forest exists, because EE 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 FF, and FF is acyclic.

Proof. By induction on the steps. At the start the sets are the nn single vertices and F=∅F = \emptyset. In step kk, with ek=(u,v)e_k = (u, v), line L5 compares the sets of uu and vv. If they are equal, nothing changes. If they differ, then by the induction hypothesis uu and vv are in different trees of FF. Adding eke_k joins these two trees into one tree and closes no cycle, and UNION merges exactly their two sets. □\square

Lemma 2 (the cut step). Let A⊆F∗A \subseteq F^* for a minimum spanning forest F∗F^*. Let S⊆VS \subseteq V be a set such that no edge of AA has exactly one end in SS. Let ee be an edge with exactly one end in SS and with w(e)≤w(f)w(e) \le w(f) for each edge ff that has exactly one end in SS. Then A∪{e}A \cup \{e\} is a subset of some minimum spanning forest.

Proof. If e∈F∗e \in F^*, take F∗F^*. Otherwise let e=(u,v)e = (u, v) with u∈Su \in S and v∉Sv \notin S. The edge ee joins uu and vv, so they are in one component of GG, and so in one tree of F∗F^*. Let PP be the path from uu to vv in that tree. The path starts in SS and ends outside SS, so it has an edge ff with exactly one end in SS. The edge ff is not in AA, by the choice of SS. Let F′=(F∗∖{f})∪{e}F' = (F^* \setminus \{f\}) \cup \{e\}. Removing ff splits its tree into two trees, one with uu and one with vv, because ff is on the only path between them. Adding ee joins these two trees again. So F′F' is acyclic and has the same components as F∗F^*, and F′F' is a spanning forest. Its weight is w(F∗)−w(f)+w(e)≤w(F∗)w(F^*) - w(f) + w(e) \le w(F^*), so F′F' is a minimum spanning forest. It contains AA, because f∉Af \notin A, and it contains ee. □\square

Invariant. For each kk from 00 to mm: there is a minimum spanning forest Fk∗F^*_k with Fk⊆Fk∗F_k \subseteq F^*_k.

  1. Initialization. F0=∅F_0 = \emptyset is a subset of each minimum spanning forest.
  2. Maintenance, edge dropped. Then Fk=Fk−1F_k = F_{k-1} and Fk∗=Fk−1∗F^*_k = F^*_{k-1} works.
  3. Maintenance, edge added. Let ek=(u,v)e_k = (u, v), and let SS be the vertex set of the tree of Fk−1F_{k-1} that holds uu. By Lemma 1, v∉Sv \notin S, so eke_k has exactly one end in SS. No edge of Fk−1F_{k-1} has exactly one end in SS, because SS is the vertex set of a tree of the forest. Let f=ejf = e_j be any edge with exactly one end in SS. If j<kj < k, then at step jj the edge was either added, and then f∈Fk−1f \in F_{k-1}, which is false, or dropped, and then its two ends were in one set at step jj. Sets only merge, so its two ends are in one tree of Fk−1F_{k-1}, and both are in SS or both are outside SS, which is also false. So j≥kj \ge k and w(ek)≤w(ej)w(e_k) \le w(e_j) by the order of the sort. Lemma 2, with A=Fk−1A = F_{k-1} and F∗=Fk−1∗F^* = F^*_{k-1}, gives a minimum spanning forest Fk∗⊇FkF^*_k \supseteq F_k.

Termination. The loop of line L4 runs once for each of the mm edges, and each run does a bounded number of disjoint-set operations, each of which stops. So the algorithm stops after mm steps.

Conclusion. Let F=FmF = F_m. Each edge ek∉Fe_k \notin F had both ends in one set at step kk, so both ends are in one tree of FF. Each edge of FF also has both ends in one tree of FF. So each path of GG stays inside one tree of FF, and each component of GG lies inside one tree of FF. Since F⊆EF \subseteq E, each tree of FF lies inside one component of GG. So FF has the components of GG, and by Lemma 1 it is acyclic: FF is a spanning forest with n−cn - c edges. The invariant gives F⊆Fm∗F \subseteq F^*_m, and Fm∗F^*_m also has n−cn - c edges, so F=Fm∗F = F^*_m. Therefore FF is a minimum spanning forest. When GG is connected, c=1c = 1 and FF is a minimum spanning tree. ■\blacksquare

Back to Kruskal's algorithm