Simple Algorithm
Algorithms & Data Structures · Optimal · 28 min

MST Algorithms: Borůvka to Linear Time

The cut and cycle properties, Borůvka, and randomized linear time.

Otakar Borůvka designed the first minimum-spanning-tree algorithm in 1926, for an electrical network in Moravia. Almost seventy years later, Karger, Klein and Tarjan combined his rounds with random sampling into an algorithm that runs in expected linear time. A deterministic linear-time algorithm is still unknown, even though Pettie and Ramachandran wrote down one that is provably optimal: nobody knows its running time.

What you’ll learn

  • Run Borůvka's algorithm, prove that its rounds are safe under a strict order of the edges and at least halve the number of components, and derive O(mlog⁡n)O(m \log n) in general and O(n)O(n) on planar graphs.
  • Define FF-heavy and FF-light edges for an arbitrary forest FF, prove that FF-heavy edges are in no MST, and explain linear-time MST verification.
  • Prove the sampling lemma (at most n/pn/p expected FF-light edges) and solve the Karger–Klein–Tarjan recurrence to expected O(m)O(m).
  • Place the deterministic algorithms on a timeline, from O(mlog⁡log⁡n)O(m \log\log n) to Chazelle's O(m α(m,n))O(m\,\alpha(m, n)) and Pettie and Ramachandran's optimal algorithm of unknown complexity.

Borůvka’s rounds

Borůvka's algorithm works on every component at once. Each vertex starts as its own component. In a round, every component picks the cheapest edge leaving it, all the picked edges are added together, and the components they join merge. Rounds repeat until one component remains (or no edge leaves any component, when the graph is disconnected).

DerivationProof
  1. pick(C)=min⁡≺{ e crossing (C,V∖C) }∈T∗\text{pick}(C) = \min\nolimits_{\prec}\{\, e \text{ crossing } (C, V \setminus C) \,\} \in T^*

    (a) Component CC's pick is the lightest edge across the cut (C,V∖C)(C, V \setminus C), and under a strict order it is the unique lightest one. By the cut property it lies in every MST, and a strict order makes the MST unique.

  2. {picks}⊆T∗ ⟹ acyclic\{\text{picks}\} \subseteq T^* \ \Longrightarrow\ \text{acyclic}

    (b) All picks lie in the single tree T∗T^*, so together they contain no cycle. The strict order matters: in a triangle with three equal weights, if each vertex broke the tie its own way, the three picks could be the three edges of the triangle.

  3. cnext≤⌊c/2⌋⟹#rounds≤⌊log⁡2n⌋c_{\text{next}} \le \lfloor c/2 \rfloor \quad\Longrightarrow\quad \#\text{rounds} \le \lfloor \log_2 n \rfloor

    (c) Contract each component to a node. The picks form a forest on these cc nodes in which every node has at least one edge, its own pick. A forest without isolated nodes has at most c/2c/2 trees, because each tree has at least two nodes:

python
def boruvka(n, edges):
    """edges = [(w, u, v), ...]; ties broken by edge index. Returns the forest's weight."""
    parent = list(range(n))
    def find(x):
        while parent[x] != x:
            parent[x] = parent[parent[x]]
            x = parent[x]
        return x
    total, comps = 0, n
    while comps > 1:
        best = {}                              # root -> (w, i): its cheapest outgoing edge
        for i, (w, u, v) in enumerate(edges):
            ru, rv = find(u), find(v)
            if ru != rv:
                for r in (ru, rv):
                    if r not in best or (w, i) < best[r]:
                        best[r] = (w, i)
        if not best:
            break                              # disconnected: a minimum spanning forest
        for w, i in set(best.values()):        # two components may pick the same edge
            ru, rv = find(edges[i][1]), find(edges[i][2])
            if ru != rv:
                parent[ru] = rv
                total, comps = total + w, comps - 1
    return total

Each round scans all mm edges once. Comparing the pairs (w,i)(w, i) is the strict order of the theorem. With plain weights and ties, the picks can contain a cycle: the ru != rv test then drops the edge that closes it, while a version that adds every distinct pick blindly would put a cycle into its "tree".

Predict before you look

The halving bound limits the number of Borůvka rounds on a connected graph with 16 vertices. At most how many rounds can it need?

Show the answer

4

16→8→4→2→116 \to 8 \to 4 \to 2 \to 1: at most ⌊log⁡216⌋=4\lfloor \log_2 16 \rfloor = 4 rounds. The bound is reached only when every round merges components exactly in pairs; on the 4 × 4 grid below, Borůvka needs 3.

Contraction, planarity and parallelism. After each round, contract every component to a single vertex, delete self-loops and keep only the cheapest of any parallel edges. Contracting edges keeps a planar graph planar, and a simple planar graph has at most 3n−63n - 6 edges, so on planar inputs round ii costs O(n/2i)O(n / 2^i) and the whole algorithm O(n)O(n) (Cheriton & Tarjan, 1976). Because every component chooses independently, Borůvka is also the basis of parallel and distributed MST algorithms: O(log⁡n)O(\log n) rounds of fully parallel work.

Heavy edges and verification

Verification asks: given a spanning tree TT, is it minimum? By the theorem, that is m−n+1m - n + 1 path-maximum queries on TT. Answering each by walking the path costs O(mn)O(mn); binary lifting, the contest method shown in the next lesson, costs O(mlog⁡n)O(m \log n).

Linear time is possible. Komlós (1985) showed that O(m)O(m) comparisons suffice, Dixon, Rauch and Tarjan (1992) turned this into a linear-time algorithm, and King (1997) gave a simpler one built on the tree of Borůvka's merges. The same machinery lists all FF-heavy edges of GG for any forest FF in linear time, which is exactly what the randomized algorithm in the next step needs. Verifying an MST is thus known to be linear; finding one deterministically in linear time is the open problem.

Check your understanding

FF is the minimum spanning forest of a random half of GG's edges, so FF may contain edges that are not in GG's MST. An edge ee of GG is FF-heavy. What can you conclude?

  1. ee is not in the MST of GG
  2. ee is not in the MST of the sampled half, but may be in GG's
  3. Nothing, until FF is verified to be part of GG's MST
  4. ee is in GG's MST if and only if it is FF-light for some other sample
Show the answer

A. ee is not in the MST of GG

The FF-path between ee's ends is a real path of GG whose edges are all lighter than ee, so ee closes a cycle of GG as its heaviest edge. No property of FF beyond being a forest of GG is used.

Random sampling: Karger, Klein and Tarjan

DerivationProof: flip the coins during Kruskal
  1. F=Kruskal(H), built during one scan of GF = \text{Kruskal}(H) \text{, built during one scan of } G

    Scan GG's edges in increasing order and flip each edge's coin when it is examined. Build FF on the fly: an edge that comes up heads joins FF if its ends are in different components of FF so far. This is Kruskal's algorithm on HH, so it produces exactly FF.

  2. e is F-light  ⟺  its ends lie in different components of F when e is examinede \text{ is } F\text{-light} \iff \text{its ends lie in different components of } F \text{ when } e \text{ is examined}

    When an edge ee is examined, either its ends are already connected in the current FF, by lighter edges that stay in FF forever, so ee is FF-heavy; or they are not, and then ee is FF-light (if heads it joins FF; if tails, any later FF-path between its ends uses heavier edges).

  3. E[#F-light edges] ≤ n−1p < np\E[\#F\text{-light edges}] \ \le\ \frac{n - 1}{p} \ <\ \frac{n}{p}

    So each FF-light edge flips a fresh coin, and each head adds an edge to FF, which can happen at most n−1n - 1 times. The number of FF-light edges is at most the number of independent flips needed for n−1n - 1 heads, a negative binomial count:

The bound does not depend on mm. On dense random graphs it is essentially tight, since FF soon spans and almost exactly (n−1)/p(n - 1)/p light edges appear. The Karger–Klein–Tarjan algorithm turns it into a recursion:

  1. Run two Borůvka rounds: their edges are in the MST, and the contracted graph G′G' has n′≤n/4n' \le n/4 vertices and at most mm edges.
  2. Sample H⊆G′H \subseteq G' with p=12p = \tfrac12 and recursively compute its minimum spanning forest FF.
  3. Delete the FF-heavy edges of G′G' with a linear-time verification algorithm.
  4. Recursively compute the minimum spanning forest of the remaining graph, and return it with the Borůvka edges.

Its model is a RAM whose only operations on edge weights are comparisons, so the result is comparison-based like Kruskal's and Prim's.

DerivationExpected linear time
  1. T(n,m) ≤ T(n4,m2)+T(n4,n2)+c (n+m)T(n, m) \ \le\ T\big(\tfrac n4, \tfrac m2\big) + T\big(\tfrac n4, \tfrac n2\big) + c\,(n + m)

    The first recursion has n/4n/4 vertices and m/2m/2 edges in expectation. The second has n/4n/4 vertices and, by the lemma with n′≤n/4n' \le n/4 and p=12p = \tfrac12, at most 2n′≤n/22n' \le n/2 expected edges. Everything else is linear:

  2. a4n+b2m+a4n+b2n+cn+cm ≤ an+bm⟸b=2c,  a=4c\tfrac a4 n + \tfrac b2 m + \tfrac a4 n + \tfrac b2 n + cn + cm \ \le\ an + bm \quad\Longleftarrow\quad b = 2c,\ \ a = 4c

    Try T(n,m)≤an+bmT(n, m) \le an + bm; the bound is linear, so linearity of expectation lets the random sizes be replaced by their means:

  3. E[T(n,m)] ≤ 4cn+2cm\E[T(n, m)] \ \le\ 4cn + 2cm

    So the expected running time is O(n+m)=O(m)O(n + m) = O(m) on connected graphs. The worst case, when sampling is unlucky every time, is that of Borůvka, O(min⁡{n2,mlog⁡n})O(\min\{n^2, m \log n\}).

Predict before you look

GG has n=1000n = 1000 vertices and m=106m = 10^6 edges with distinct weights. You keep each edge with probability p=0.1p = 0.1 and compute the minimum spanning forest FF of the sample. At most how many FF-light edges do you expect in GG?

Show the answer

10000

n/p=1000/0.1=10,000n/p = 1000 / 0.1 = 10{,}000. The sample has about 10510^5 edges and the filter leaves about 10410^4 candidates out of a million, so both subproblems are far smaller than GG. A smaller pp makes the sample cheaper but leaves more light edges; p=12p = \tfrac12 balances the two calls of the recursion.

Deterministic algorithms, and an optimal one

YearAlgorithmTime
1926BorůvkaO(mlog⁡n)O(m \log n)
1930, 1957Jarník; Prim (with a binary heap)O(mlog⁡n)O(m \log n)
1956KruskalO(mlog⁡m)O(m \log m)
1975–76Yao; Cheriton & TarjanO(mlog⁡log⁡n)O(m \log\log n); Cheriton–Tarjan also O(n)O(n) on planar graphs
1984Fredman & Tarjan, Prim's algorithm with Fibonacci heaps and bounded heap sizesO(m β(m,n))O(m\,\beta(m, n)), with β(m,n)=min⁡{i:log⁡(i)n≤m/n}≤log⁡∗n\beta(m, n) = \min\{i : \log^{(i)} n \le m/n\} \le \log^* n
1986Gabow, Galil, Spencer & Tarjan, refining that ideaO(mlog⁡β(m,n))O(m \log \beta(m, n))
1995Karger, Klein & Tarjan (randomized)O(m)O(m) expected
2000Chazelle, with soft heapsO(m α(m,n))O(m\,\alpha(m, n))
2002Pettie & Ramachandranoptimal, but its complexity is unknown
Comparison-based bounds. With integer weights on a word RAM, Fredman and Willard (1994) found an MST in deterministic linear time. Fredman and Tarjan run Prim from many vertices at once, cap each heap's size, contract the trees, and raise the cap exponentially from pass to pass, so few passes are needed.

Chazelle's soft heap is a priority queue that may corrupt keys, raising up to an ε\varepsilon fraction of them, in exchange for O(1)O(1) amortized time per operation. His algorithm contracts carefully chosen subgraphs while using soft heaps to find nearly cheapest edges, and repairs the damage done by corrupted keys recursively. The inverse Ackermann function enters because the recursion sizes its subproblems along Ackermann's hierarchy, much as union-find's analysis sorts nodes into levels.

How can an algorithm be optimal without anyone knowing its running time? It partitions the graph into pieces with very few vertices, about log⁡log⁡log⁡n\log\log\log n, small enough that an optimal decision tree for every possible piece can be found by brute-force search within the time budget. It runs those trees on the pieces, contracts, and finishes with Chazelle-style machinery whose cost is within a constant factor of T∗T^*. The algorithm uses the optimal trees without ever learning how large they are.

Check your understanding

Which statement about Pettie and Ramachandran's MST algorithm is correct?

  1. It is randomized, and runs in expected linear time
  2. It was proved to run in O(m)O(m) time in 2002
  3. It runs in O(mlog⁡∗n)O(m \log^* n) time, which is optimal
  4. Its running time is within a constant factor of the best possible for comparison-based algorithms, but that best possible bound is unknown
Show the answer

D. Its running time is within a constant factor of the best possible for comparison-based algorithms, but that best possible bound is unknown

It runs in O(T∗(m,n))O(T^*(m, n)), the decision-tree complexity, so no comparison-based algorithm beats it by more than a constant factor. Since T∗T^* is only known to lie between linear and O(m α(m,n))O(m\,\alpha(m, n)), so does its running time.

Key takeaways

  • Borůvka: every component adds its cheapest outgoing edge; under a strict order the picks are safe and acyclic, and each round at least halves the components: O(mlog⁡n)O(m \log n), O(n)O(n) on planar graphs with contraction, and naturally parallel.
  • For every forest FF, FF-heavy edges are in no MST; a tree is minimum iff every non-tree edge is heavy for it, and this can be verified in linear time (Komlós; Dixon, Rauch & Tarjan; King).
  • Sampling lemma: the minimum spanning forest of a pp-sample leaves at most n/pn/p expected light edges. Two Borůvka rounds, a sample with p=12p = \tfrac12, a linear-time filter and two recursions give expected O(m)O(m) (Karger, Klein & Tarjan, 1995).
  • Deterministically, the best bound is Chazelle's O(m α(m,n))O(m\,\alpha(m, n)); Pettie and Ramachandran's algorithm is optimal for comparison-based computation, but whether that optimum is linear is open.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Why does each Borůvka round at least halve the number of components of a connected graph?

  1. Each component picks two edges, one to each side
  2. The picked edges form a perfect matching of the components
  3. Every component's pick joins it to another component, and a forest without isolated nodes has at most half as many trees as nodes
  4. Half of all the edges are picked in every round
Show the answer

C. Every component's pick joins it to another component, and a forest without isolated nodes has at most half as many trees as nodes

On the contracted graph, the picks form a forest in which every node has degree at least 1. Each tree of that forest therefore contains at least two old components, so the new count is at most half the old one.

Question 2

The Karger–Klein–Tarjan algorithm starts with two Borůvka rounds before sampling. What goes wrong with only one?

  1. Nothing; one round only changes the constant factor
  2. The two recursive calls would together see about nn vertices again, so the recurrence would not shrink to linear time
  3. The sampling lemma needs a graph with at most n/4n/4 vertices
  4. One round might leave cycles among the picked edges
Show the answer

B. The two recursive calls would together see about nn vertices again, so the recurrence would not shrink to linear time

With one round, n′≤n/2n' \le n/2, and both recursive calls run on n′n' vertices, nn in total: the vertex term never decreases. With two rounds the calls see n/4+n/4=n/2n/4 + n/4 = n/2 vertices, and T(n,m)≤4cn+2cmT(n, m) \le 4cn + 2cm solves the recurrence.

Question 3

In the Karger–Klein–Tarjan algorithm, after FF, the minimum spanning forest of the sample HH, is computed, which edges of G′G' may be discarded?

  1. The edges that were not sampled into HH
  2. The FF-light edges
  3. Every edge heavier than the heaviest edge of FF
  4. The FF-heavy edges, and only those
Show the answer

D. The FF-heavy edges, and only those

An FF-heavy edge is the heaviest edge on a cycle of G′G', so it is in no MST. FF-light edges may be needed, and the sampling lemma guarantees that only about n′/pn'/p of them survive into the second recursive call.

Further reading

  • O. Borůvka, "O jistém problému minimálním" (On a certain minimal problem), Práce Moravské přírodovědecké společnosti 3 (1926).
  • D. R. Karger, P. N. Klein & R. E. Tarjan, "A randomized linear-time algorithm to find minimum spanning trees," Journal of the ACM 42(2) (1995), 321–328.
  • V. King, "A simpler minimum spanning tree verification algorithm," Algorithmica 18(2) (1997), 263–270.
  • B. Chazelle, "A minimum spanning tree algorithm with inverse-Ackermann type complexity," Journal of the ACM 47(6) (2000), 1028–1047.
  • S. Pettie & V. Ramachandran, "An optimal minimum spanning tree algorithm," Journal of the ACM 49(1) (2002), 16–34.