Algorithm Search⌘K

Proofs: Yao's minimum spanning tree algorithm

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

Yao's algorithm returns the minimum spanning forest in O(m log log n) time full

This proof shows: correctness, termination, complexity.

Claim. Let G=(V,E)G = (V, E) be a finite undirected multigraph with nn vertices, mm edges and real weights; self-loops, parallel edges, ties and several components are allowed. Order the edges by the key (w(e),index of e)(w(e), \text{index of } e). Then YAO-MST stops and returns the unique minimum spanning forest F∗F^\ast for this order, which is a minimum spanning forest for ww. It runs in O(mlog⁡log⁡n+n)O(m \log \log n + n) time.

Facts used

Uniqueness. All keys are distinct, so the minimum spanning forest for the keys is unique; call it F∗F^\ast. A forest of least total key order is also of least total weight for ww, because the weight order is a coarsening of the key order: Kruskal's algorithm with the key order is a valid run of Kruskal's algorithm for ww.

Cut property. If SS is the vertex set of a component of a forest F⊆F∗F \subseteq F^\ast, and ee is the edge of least key with exactly one end in SS, then e∈F∗e \in F^\ast. Proof. If e∉F∗e \notin F^\ast, the path in F∗F^\ast between the ends of ee crosses from SS to V∖SV \setminus S by some edge f≠ef \ne e, and ff has a larger key than ee. Then F∗−f+eF^\ast - f + e is a spanning forest with the same components and a smaller key sum, which is impossible. □\square

Invariant

Invariant I. At the start of each phase of line L2 and of each pass of line L7, F⊆F∗F \subseteq F^\ast, and the components of the disjoint-set structure are the components of (V,F)(V, F).

It holds at line L1, where F=∅F = \emptyset. In one phase, every chosen edge eCe_C is the least key edge that leaves the component CC; this is immediate in line L2, and in line L13 it is Lemma 1 below. By the cut property, every chosen edge is in F∗F^\ast. So FF together with all chosen edges is a subset of the forest F∗F^\ast, and adding them one by one never closes a cycle; line L13 skips an edge only when its ends are already merged, which happens only when two components chose the same edge. Merging along the added edges keeps the second part of the invariant.

Lemma 1: line L11 finds the least leaving edge at v

Lemma 1. At line L11, if vv has an edge to another component, then eve_v exists and is the least key edge at vv that leaves the component of vv. Otherwise eve_v does not exist.

Proof. Two facts hold for all vv at all times.

(a) Every edge that was removed from a group of vv had both ends in one component when it was removed, and components only merge, so it still has both ends in one component.

(b) The groups L(v,1),…,L(v,c(v)−1)L(v, 1), \ldots, L(v, c(v) - 1) hold no edge that leaves a component. Indeed, c(v)c(v) grows only in line L10, after L(v,c(v))L(v, c(v)) became empty by removals, so every edge that was ever in these groups was removed, and by (a) it does not leave a component.

After line L10, either L(v,c(v))L(v, c(v)) is nonempty and holds only edges that leave the component of vv (the inner ones were just removed), or c(v)=kc(v) = k and the group is empty. In the first case, let ee be the least key edge at vv that leaves the component. By (b), ee is not in a group before c(v)c(v). Every key in L(v,c(v))L(v, c(v)) is below every key in later groups, by line L5, and L(v,c(v))L(v, c(v)) contains a leaving edge, so ee is not in a later group either. So e∈L(v,c(v))e \in L(v, c(v)), and it is the least edge there. In the second case, all groups are empty or hold no leaving edge, by (b) and the emptiness of the last group, so vv has no leaving edge. □\square

So eCe_C in line L12, the least of eve_v over the vertices vv of HH inside CC, is the least key edge that leaves CC: every edge of GG that leaves CC is an edge of HH at some vertex of HH inside CC, since HH keeps all edges between different components after line L2.

Termination and the result

Each pass of lines L7 to L13 in which some component has a leaving edge adds at least one edge to FF, and F⊆F∗F \subseteq F^\ast has at most n−1n - 1 edges, so the loop stops. When it stops, no edge leaves any component. Then the components of (V,F)(V, F) are the components of GG, so FF is a spanning forest with n−cn - c edges, where cc is the number of components of GG. Also F⊆F∗F \subseteq F^\ast and ∣F∗∣=n−c|F^\ast| = n - c, so F=F∗F = F^\ast.

Running time

Let p=⌈log⁡2log⁡2n⌉p = \lceil \log_2 \log_2 n \rceil. Each plain Borůvka phase costs O(n+m)O(n + m). In each phase, each component that has a leaving edge merges with at least one other, so after pp phases each component with a leaving edge has at least 2p≥log⁡2n2^p \ge \log_2 n vertices, and HH has n′≤n/log⁡2nn' \le n / \log_2 n vertices with edges. The cost of line L2 is O((n+m)log⁡log⁡n)O((n + m) \log \log n).

Line L5 on a list of dd edges uses ⌈log⁡2k⌉\lceil \log_2 k \rceil levels of splitting, and each level selects one rank in each part and splits it, which costs O(d)O(d) by linear-time selection. So lines L4 to L6 cost O(mlog⁡k)=O(mlog⁡log⁡n)O(m \log k) = O(m \log \log n), as k≤log⁡2n+1k \le \log_2 n + 1. Each group has at most ⌈d/k⌉+1\lceil d/k \rceil + 1 edges.

In lines L7 to L13, each pass at least halves the number of components that have a leaving edge, so there are at most ⌊log⁡2n′⌋+1\lfloor \log_2 n' \rfloor + 1 passes. In one pass, vertex vv reads its current group and every group it empties. Each edge is removed from each of its two lists at most once, so removals cost O(m)O(m) over the whole run. Reading the final current group costs O(d(v)/k+1)O(d(v)/k + 1), so a pass costs O(m/k+n′)O(m/k + n') apart from removals. Over all passes this is O((m/k+n′)log⁡n′)=O(m+n)O((m/k + n') \log n') = O(m + n), since k≥log⁡2n′k \ge \log_2 n' and n′log⁡2n′≤nn' \log_2 n' \le n.

The total is O(mlog⁡log⁡n+n)O(m \log \log n + n). ■\blacksquare

Back to Yao's minimum spanning tree algorithm