Algorithm Search⌘K

Proofs: Chazelle's minimum spanning tree algorithm

These are the human proofs of Chazelle's minimum spanning tree algorithm, at full length.

The soft-heap filter returns the minimum spanning forest full

This proof shows: correctness, termination.

This proof is for the simplified algorithm SOFT-HEAP-MSF on the page. Chazelle's full algorithm rests on the same two lemmas, the contractibility of the pieces (Lemma 2) and the filter (Lemma 3), and its time analysis is in the paper.

Let G=(V,E)G = (V, E) be a finite undirected graph without self-loops (they are dropped at the start), with parallel edges allowed. Each edge ee has the key key(e)=(w(e),i)\mathrm{key}(e) = (w(e), i), where ii is its index. Keys are compared lexicographically, so distinct edges have distinct keys. All current keys of the soft heaps are keys of edges, so they live in the same totally ordered set. For a set of edges XX, MSF(X)\mathrm{MSF}(X) is the unique minimum spanning forest of the graph (V,X)(V, X) under the key order. "Minimum" means least in the order on the sorted list of keys, which for distinct keys is the same as least total key.

Facts

Fact 1 (cut property). Let AA be a set of edges contained in some minimum spanning forest for a weight function cc (ties allowed). Let S⊆VS \subseteq V be a set such that no edge of AA has exactly one end in SS, and let ee be an edge with exactly one end in SS such that c(e)≤c(f)c(e) \le c(f) for each such edge ff. Then A∪{e}A \cup \{e\} is contained in some minimum spanning forest for cc. This is the cut property in its form with ties.

Fact 2 (cycle property, distinct keys). An edge ee is not in MSF(X)\mathrm{MSF}(X) if and only if XX has a cycle through ee on which ee has the largest key. See the cycle property.

Fact 3 (exchange). Let T′T' be a minimum spanning forest for cc, let g∈T′g \in T', and let SS be the vertex set of one of the two trees of T′−gT' - g inside the component of gg. If an edge hh has exactly one end in SS, then c(g)≤c(h)c(g) \le c(h).

Proof. The ends of hh lie in one component of GG, because hh is an edge. That component is the component of gg, since one end of hh is in SS. So T′−g+hT' - g + h is a spanning forest with the same components. If c(h)<c(g)c(h) < c(g), its weight is smaller, a contradiction. □\square

Fact 4. If MSF(E)⊆K⊆E\mathrm{MSF}(E) \subseteq K \subseteq E, then MSF(K)=MSF(E)\mathrm{MSF}(K) = \mathrm{MSF}(E).

Proof. Every edge e∈K∖MSF(E)e \in K \setminus \mathrm{MSF}(E) has, by Fact 2, a cycle in EE on which it is the heaviest edge. Its fundamental cycle with respect to MSF(E)\mathrm{MSF}(E) is such a cycle, and it lies in KK. So e∉MSF(K)e \notin \mathrm{MSF}(K), and MSF(K)⊆MSF(E)\mathrm{MSF}(K) \subseteq \mathrm{MSF}(E). Since MSF(E)⊆K⊆E\mathrm{MSF}(E) \subseteq K \subseteq E, both graphs have the same components, so the two forests have the same number of edges and are equal. □\square

Lemma 1: the soft heap

Lemma 1. In the soft heap of the implementation, every item has a current key at least its own key, the current key of an item never decreases, and DELETE-MIN returns an item whose current key is the smallest current key among all items in the heap.

Proof. The current key of an item is the ckey of the node whose list holds it. We show three invariants: (i) every item's key is at most the ckey of its node; (ii) the ckey of a child is at least the ckey of its parent; (iii) a node with an empty list is a leaf, except during a sift.

All three hold for a new node of rank 0. In SIFT(x)(x), the child cc with the smaller ckey is chosen. Its list moves to xx, and x.ckeyx.\mathrm{ckey} becomes c.ckeyc.\mathrm{ckey}. By (ii), this is at least the old ckey of xx, so the current keys of the items already in xx do not decrease. The moved items keep their current key. (i) holds for all of them. The new ckey of xx is at most the ckey of the other child, by the choice of cc, and at most the ckeys of the children of cc, by (ii). Then cc is sifted recursively, which only raises its ckey, so (ii) is kept. If cc ends with an empty list, the loop in SIFT(c)(c) has stopped, so cc is a leaf, and it is removed. This keeps (iii).

A new node of a link is sifted at once, and its ckey becomes the ckey of a child. In DELETE-MIN, a root with an empty list is either sifted or, if it is a leaf, removed.

Items move only up the tree, and only by SIFT. So, by the argument above, current keys never decrease. DELETE-MIN takes an item from a root of smallest ckey. By (ii), every node has a ckey at least that of its root, so every item in the heap has a current key at least the returned one. □\square

Lemma 2: the pieces are contractible for the corrupted keys

Number the soft heaps H1,H2,…H_1, H_2, \dots in the order in which they are made at line L3. Each edge enters a given heap at most once. The root inserts each of its edges once, and line L7 skips edges whose other end is already in the piece. For an edge ee and a heap HH that held it, let cH(e)c_H(e) be the current key of ee when it was deleted from HH (line L5), or when HH was discarded (line L8). Define

w′(e)=max⁡HcH(e),and w′(e)=key(e) if e was in no heap.w'(e) = \max_H c_H(e), \quad \text{and } w'(e) = \mathrm{key}(e) \text{ if } e \text{ was in no heap.}

By Lemma 1, w′(e)≥key(e)w'(e) \ge \mathrm{key}(e). Lines L5 and L8 put ee into BB whenever some cH(e)>key(e)c_H(e) > \mathrm{key}(e). So

{e:w′(e)≠key(e)}⊆B,that is, w′(e)=key(e) for e∉B.(1)\{e : w'(e) \ne \mathrm{key}(e)\} \subseteq B, \qquad \text{that is, } w'(e) = \mathrm{key}(e) \text{ for } e \notin B. \tag{1}

Call an edge a tree edge if it is added at line L7 (the edge that brings yy into CC) or used to join two pieces at line L6. Let AA be the set of tree edges.

Lemma 2. Some minimum spanning forest T′T' for w′w' (ties allowed) contains AA. Every final piece, after all joins, is spanned by the tree edges inside it.

Proof. We add the tree edges to AA in the order of the run and apply Fact 1 with c=w′c = w' at each step. Note that w′w' is fixed by the whole run, so the comparisons below are between final values.

Consider the moment when a tree edge ee is found by DELETE-MIN from the heap HH of the piece CC being grown. Here CC is its vertex set at that moment, and S=CS = C.

  • The cut respects AA. The tree edges added so far lie inside CC or inside earlier pieces, and earlier pieces are disjoint from CC. A join only happens at the end of the growth of a piece.
  • Every edge ff with exactly one end in CC is in HH at this moment. Let f=(p,q)f = (p, q) with p∈Cp \in C and q∉Cq \notin C. When pp entered CC (or when p=rp = r), qq was not in CC, so ff was inserted into HH. If ff had been deleted from HH before, then at that time its far end was in CC (it was skipped), or it brought its far end into CC, or it ended the growth of CC. The first two cases contradict q∉Cq \notin C, and the third contradicts the fact that CC is still growing.
  • So ee is light for w′w'. For each such ff, Lemma 1 gives w′(f)≥cH(f)≥w'(f) \ge c_H(f) \ge the current key of ff now ≥\ge the current key of ee now =cH(e)= c_H(e). It remains to show w′(e)=cH(e)w'(e) = c_H(e).
    • If ee is added at line L7, both of its ends are now in CC. An earlier heap holds an edge only if one of its ends is in an earlier piece, and a later heap only if one of its ends is free when it is inserted. So HH is the only heap that holds ee, and w′(e)=cH(e)w'(e) = c_H(e).
    • If ee joins CC to an earlier piece at line L6, then e∉Be \notin B, so by (1) and Lemma 1, w′(e)=key(e)≤cH(e)≤w′(e)w'(e) = \mathrm{key}(e) \le c_H(e) \le w'(e).

By Fact 1, AA stays inside a minimum spanning forest for w′w' after each addition. Since GG is finite, the final AA is contained in one such forest T′T'.

A grown piece is spanned by its line-L7 edges, because each one brings a new vertex in and attaches it to the piece. A join adds an edge between two spanned pieces. So every final piece is spanned by the tree edges inside it. □\square

Lemma 3: the filter

Let P1,…,PkP_1, \dots, P_k be the final pieces. Let HH be the multigraph whose vertices are the pieces. For each edge e∉Be \notin B whose ends lie in different pieces, HH has an edge between those pieces, with the same key. This is the graph of line L10.

Lemma 3. MSF(E)⊆⋃iMSF(G[Pi])∪MSF(H)∪B\mathrm{MSF}(E) \subseteq \bigcup_i \mathrm{MSF}(G[P_i]) \cup \mathrm{MSF}(H) \cup B, where the edges of MSF(H)\mathrm{MSF}(H) are identified with the edges of GG they come from.

Proof. Let e∈MSF(E)e \in \mathrm{MSF}(E) with e∉Be \notin B.

Case 1: both ends of ee are in one piece PiP_i. If e∉MSF(G[Pi])e \notin \mathrm{MSF}(G[P_i]), then by Fact 2 some cycle of G[Pi]G[P_i] has ee as its heaviest edge. That is also a cycle of GG, so e∉MSF(E)e \notin \mathrm{MSF}(E) by Fact 2, a contradiction.

Case 2: the ends of ee are in different pieces. Then ee is an edge of HH. Suppose e∉MSF(H)e \notin \mathrm{MSF}(H). By Fact 2, HH has a cycle QQ through ee on which ee is the heaviest edge. As a cycle, QQ visits each piece at most once. At a piece PiP_i that it visits, it arrives at a vertex xi∈Pix_i \in P_i and leaves from a vertex yi∈Piy_i \in P_i, along two edges of QQ. By Lemma 2, T′∩E(Pi)T' \cap E(P_i) contains a spanning tree of PiP_i. Let RiR_i be the path from xix_i to yiy_i in that tree, which is empty if xi=yix_i = y_i. Then Ri⊆T′R_i \subseteq T'.

Let WW be the closed walk in GG that follows the edges of QQ and crosses each visited piece PiP_i along RiR_i.

Claim. Every edge gg on a path RiR_i has key(g)<key(e)\mathrm{key}(g) < \mathrm{key}(e).

Remove gg from T′T', and let SS be the side that contains xix_i. Since gg lies on the path RiR_i in T′T', yi∉Sy_i \notin S. A closed walk crosses the cut (S,V∖S)(S, V \setminus S) an even number of times, counted with multiplicity. Consider the edges of WW:

  • gg itself is used once, since the paths RjR_j lie in different pieces and a path uses each edge once;
  • no other edge of T′T' crosses the cut, because SS is a side of T′−gT' - g;
  • the edges of QQ are the only other edges of WW.

So some edge hh of QQ crosses the cut. Since h∈Qh \in Q, it is an edge of HH, so h∉Bh \notin B, and w′(h)=key(h)w'(h) = \mathrm{key}(h) by (1). By Fact 3, applied to T′T' and c=w′c = w', w′(g)≤w′(h)w'(g) \le w'(h). Since ee is the heaviest edge of QQ, key(h)≤key(e)\mathrm{key}(h) \le \mathrm{key}(e). Together:

key(g)≤w′(g)≤w′(h)=key(h)≤key(e).\mathrm{key}(g) \le w'(g) \le w'(h) = \mathrm{key}(h) \le \mathrm{key}(e).

Moreover g≠eg \ne e, since gg has both ends in PiP_i and ee does not. Distinct edges have distinct keys, so key(g)<key(e)\mathrm{key}(g) < \mathrm{key}(e). This proves the claim.

Every edge of WW other than ee is lighter than ee: the edges of QQ by the choice of QQ, and the edges of the paths RiR_i by the claim. The edge ee occurs exactly once in WW, because it is not inside a piece. Removing it from WW leaves a walk between the two ends of ee that does not use ee, and that walk contains a path. With ee, the path forms a cycle of GG on which ee is the heaviest edge. By Fact 2, e∉MSF(E)e \notin \mathrm{MSF}(E), a contradiction. □\square

The theorem

Theorem. SOFT-HEAP-MSF(G)(G) terminates and returns MSF(E)\mathrm{MSF}(E).

Proof. By induction on m=∣E∣m = |E|.

Base. If m≤m \le base, line L1 returns Kruskal's result, which is MSF(E)\mathrm{MSF}(E).

Step. Assume m>m > base and m≥1m \ge 1. Lines L2 to L8 terminate, because each iteration deletes an item from a finite heap. Let rr be the first vertex, in the order of line L3, that has an edge. When rr is reached, it is still free, because a piece made earlier was grown from a vertex without edges and so stayed alone. So rr starts a piece, and its heap is not empty. Since s≥2s \ge 2, line L5 deletes some edge e=(r,y)e = (r, y) with y≠ry \ne r. Then one of three things happens:

  • e∈Be \in B;
  • yy joins the piece at line L7, so ee is inside a final piece;
  • yy is in an earlier piece and the pieces are joined, so ee is again inside a final piece.

In each case ee is not an edge of HH. So HH has at most m−1m - 1 edges, and by the induction hypothesis, line L10 terminates and returns MSF(H)\mathrm{MSF}(H).

Lines L9 and L12 are Kruskal's algorithm, which returns MSF\mathrm{MSF} of its input under the key order. By Lemma 3, K=F∪F′∪B⊇MSF(E)K = F \cup F' \cup B \supseteq \mathrm{MSF}(E), and K⊆EK \subseteq E. By Fact 4, line L12 returns MSF(K)=MSF(E)\mathrm{MSF}(K) = \mathrm{MSF}(E). ■\blacksquare

The keys (w,i)(w, i) order the edges first by weight. So MSF(E)\mathrm{MSF}(E) is a minimum spanning forest for the weights ww, and when weights tie it is the one that Kruskal's algorithm picks with ties broken by index. If GG is disconnected, it is a minimum spanning forest, one tree per component.

Back to Chazelle's minimum spanning tree algorithm