Algorithm Search⌘K

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 G=(V,E)G = (V, E) be a finite undirected graph with a real weight w(e)w(e) on each edge. Parallel edges and self-loops are allowed, weights may tie, and GG need not be connected. A spanning forest is a forest F⊆EF \subseteq E with the same components as GG; it has n−cn - c edges, where cc 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 BB for the set of blue edges and RR for the set of red edges.

The invariant

Invariant I. Some minimum spanning forest TT has B⊆TB \subseteq T and T∩R=∅T \cap R = \emptyset.

Lemma 1 (start). Invariant I holds after line L1.

Proof. B=R=∅B = R = \emptyset, and GG has a spanning forest, so it has a minimum one. □\square

Lemma 2 (blue rule). Assume Invariant I holds with the forest TT. Let (S,V∖S)(S, V \setminus S) be a cut that no blue edge crosses, and let e=(u,v)e = (u, v) be a lightest uncolored edge that crosses it, with u∈Su \in S. Then Invariant I holds after ee is colored blue.

Proof. If e∈Te \in T, the forest TT still works. Otherwise T∪{e}T \cup \{e\} has exactly one cycle, the fundamental cycle of ee: the edge ee and the path PP in TT from uu to vv. (The path exists: uu and vv are in one component of GG, and TT spans it.) The path starts in SS and ends outside SS, so it has an edge ff that crosses the cut. Then:

  • ff is not blue, because no blue edge crosses the cut;
  • ff is not red, because f∈Tf \in T and T∩R=∅T \cap R = \emptyset;
  • so ff is uncolored, and since ee is a lightest uncolored crossing edge, w(e)≤w(f)w(e) \le w(f).

Let T′=T−f+eT' = T - f + e. Removing ff splits one tree of TT into two, with uu and vv on different sides, since ff lies on their only path; adding ee joins them again. So T′T' is a spanning forest, and w(T′)=w(T)−w(f)+w(e)≤w(T)w(T') = w(T) - w(f) + w(e) \le w(T), so T′T' is an MSF. It contains B∪{e}B \cup \{e\}, since f∉Bf \notin B. It avoids RR, since ee was uncolored and TT avoids RR. □\square

Lemma 3 (red rule). Assume Invariant I holds with the forest TT. Let CC be a simple cycle with no red edge, and let e=(u,v)e = (u, v) be a heaviest uncolored edge of CC. Then Invariant I holds after ee is colored red.

Proof. If e∉Te \notin T, the forest TT still works. Otherwise e∈Te \in T, so ee is not a self-loop and C∖{e}C \setminus \{e\} is a path from uu to vv. Removing ee splits the tree of TT that holds it into two trees; let SS be the vertex set of the one that holds uu, so v∉Sv \notin S. The path C∖{e}C \setminus \{e\} starts in SS and ends outside it, so it has an edge ff with exactly one end in SS. Then:

  • f∉Tf \notin T, because the only edge of TT with exactly one end in SS is ee, and f≠ef \ne e;
  • ff is not blue, because B⊆TB \subseteq T;
  • ff is not red, because CC has no red edge;
  • so ff is uncolored, and since ee is a heaviest uncolored edge of CC, w(f)≤w(e)w(f) \le w(e).

Let T′=T−e+fT' = T - e + f. The edge ff joins the two trees again, so T′T' is a spanning forest with w(T′)≤w(T)w(T') \le w(T), hence an MSF. It contains BB, since e∉Be \notin B (ee was uncolored). It avoids R∪{e}R \cup \{e\}, since ff is uncolored. □\square

The scheme never gets stuck

Lemma 4 (applicability). Before each step, the blue edges form a forest. If an edge e=(u,v)e = (u, v) is uncolored, then a rule applies in the way lines L3 to L8 describe.

Proof. By Invariant I, B⊆TB \subseteq T and TT is a forest, so BB is a forest.

  • If uu and vv are in different blue trees, let SS be a union of blue trees that holds uu and not vv (for example the blue tree of uu). No blue edge has exactly one end in SS, and the uncolored edge ee crosses the cut. So the blue rule applies to this cut.
  • If uu and vv are in one blue tree, let PP be the blue path from uu to vv (empty if ee is a self-loop). Then P∪{e}P \cup \{e\} is a simple cycle: e∉Pe \notin P since ee is uncolored, and PP is a simple path. It has no red edge and contains the uncolored edge ee, so the red rule applies. If line L7 uses any other path of non-red edges from uu to vv that avoids ee, the same holds for the cycle it gives; the implementation finds such a path by breadth-first search, which gives a simple path. □\square

Termination and the result

Theorem. For each sequence of choices, GREEDY-MST stops after exactly mm steps, and the set BB it returns is a minimum spanning forest of GG.

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 mm times. By Lemmas 1, 2 and 3 and induction on the steps, Invariant I holds at the end, with some MSF TT. Then every edge is blue or red, B⊆TB \subseteq T and T∩R=∅T \cap R = \emptyset, so T⊆E∖R=BT \subseteq E \setminus R = B. Hence B=TB = T, an MSF. ■\blacksquare

Remark (ties). Lemmas 2 and 3 use only w(e)≤w(f)w(e) \le w(f) and w(f)≤w(e)w(f) \le w(e). 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.

Back to Greedy MST algorithm