Part VI · Graph Algorithms Chapter 21

Minimum Spanning Trees

The cheapest way to connect everything — and two famous greedy algorithms that are really the same algorithm with a different rule for what to grow.

Given a connected undirected graph with edge weights, a spanning tree is an acyclic subset of edges connecting all vertices, and a minimum spanning tree is one of least total weight. Chapter 21 presents a single generic greedy method, proves one theorem that justifies it, and then shows that Kruskal’s and Prim’s algorithms are just two ways of instantiating it. The chapter is the cleanest demonstration in the book of greedy design done properly: prove the safe-choice theorem first, and both algorithms follow.

4th edition note. Renumbered from 23 to 21. Content is unchanged.

Contents

  1. The problem
  2. The generic method
  3. Cuts and safe edges
  4. Theorem 21.1
  5. Kruskal’s algorithm
  6. Prim’s algorithm
  7. Which to use
  8. Recap

The problem

Input: a connected undirected graph G = (V, E) with a weight function w : E → ℝ. Find an acyclic subset T ⊆ E that connects all vertices and minimises w(T) = ∑(u,v)∈T w(u,v).

Such a T is a tree, and it always has exactly |V| - 1 edges. The motivating picture is a wiring problem: connect n pins with the least total wire.

The MST is not the shortest-path tree. These get confused constantly. An MST minimises the total weight of all edges; a shortest-path tree from a source s minimises the distance from s to each vertex individually. They are usually different trees, and an MST can contain a path between two vertices far longer than their shortest path. Chapter 22 does shortest paths; this chapter does not.

The generic method

Both algorithms grow a set A that is always a subset of some MST, one edge at a time.

GENERIC-MST(G, w) 1 A = ∅ 2 while A does not form a spanning tree 3 find an edge (u,v) that is safe for A 4 A = A ∪ {(u,v)} 5 return A

The loop invariant: A is a subset of some minimum spanning tree. An edge is safe for A if adding it keeps that invariant true.

Line 3 is the entire difficulty. The invariant handles correctness: it holds initially (the empty set is in every MST), it is maintained by definition of safe, and at termination A is a spanning tree that is a subset of an MST — hence is an MST.

Cuts and safe edges

Three definitions are needed to state the theorem.

TermDefinition
Cut (S, V-S)A partition of the vertices into two non-empty parts.
An edge crosses the cutIts two endpoints are on opposite sides.
A cut respects a set ANo edge in A crosses it.
Light edgeA crossing edge of minimum weight among all edges crossing that cut.
S V − S 9 7 4 the light edge (weight 4) is safe for A — solid black edges are already in A
Figure 21.1 — A cut respecting A. No edge of A crosses the dashed line, and the lightest crossing edge is guaranteed to belong to some MST.

Theorem 21.1

Theorem 21.1. Let A be a subset of some minimum spanning tree of G, let (S, V-S) be any cut that respects A, and let (u,v) be a light edge crossing that cut. Then (u,v) is safe for A.

Proof sketch (an exchange argument, as in Chapter 15). Let T be an MST containing A. If (u,v) ∈ T, done. Otherwise adding (u,v) to T creates a cycle, and that cycle must cross the cut an even number of times, so it contains some other crossing edge (x,y). Since the cut respects A, (x,y) ∉ A. Form T′ = T - {(x,y)} ∪ {(u,v)}. Because (u,v) is light, w(u,v) ≤ w(x,y), so w(T′) ≤ w(T) — and T′ is also spanning. So T′ is an MST containing both A and (u,v).

Corollary 21.2. If C is a connected component in the forest Gᵀ = (V, A), and (u,v) is a light edge connecting C to some other component, then (u,v) is safe for A. This is the form both algorithms actually use.

Kruskal’s algorithm

Kruskal grows a forest. A is a forest of many trees. At each step add the globally lightest edge that connects two different trees. The forest merges into one tree at the end.
MST-KRUSKAL(G, w) 1 A = ∅ 2 for each vertex v ∈ G.V 3 MAKE-SET(v) 4 sort the edges of G.E into non-decreasing order by weight w 5 for each edge (u,v) ∈ G.E, taken in that order 6 if FIND-SET(u) ≠ FIND-SET(v) // different trees: no cycle 7 A = A ∪ {(u,v)} 8 UNION(u, v) 9 return A

The disjoint-set structure from Chapter 19 is exactly the cycle test: two endpoints in the same set means adding the edge would close a cycle.

StepCost
Sorting the edgesO(E lg E) — dominates
O(V) MAKE-SET plus O(E) FIND-SET/UNIONO((V + E) · α(V)), effectively linear
TotalO(E lg E) = O(E lg V), since |E| < |V|²

Prim’s algorithm

Prim grows one tree. A is always a single tree, starting from an arbitrary root. At each step add the lightest edge connecting the tree to a vertex outside it. Structurally this is Dijkstra’s algorithm with a different key.
MST-PRIM(G, w, r) 1 for each u ∈ G.V 2 u.key = ∞; u.π = NIL 3 r.key = 0 4 Q = G.V // min-priority queue on key 5 while Q ≠ ∅ 6 u = EXTRACT-MIN(Q) 7 for each v ∈ G.Adj[u] 8 if v ∈ Q and w(u,v) < v.key 9 v.π = u 10 v.key = w(u,v) // DECREASE-KEY

v.key is the weight of the lightest edge connecting v to the tree built so far. The MST is {(v, v.π) : v ∈ V - {r}}.

Priority queueEXTRACT-MINDECREASE-KEYTotal
ArrayO(V)O(1)O(V²)
Binary min-heapO(lg V)O(lg V)O(E lg V)
Fibonacci heapO(lg V)O(1) amortizedO(E + V lg V)
The Fibonacci-heap row is why that structure exists. Its O(1) amortized DECREASE-KEY matters precisely because Prim and Dijkstra call DECREASE-KEY up to |E| times but EXTRACT-MIN only |V| times. The 4th edition removed the Fibonacci heap chapter from print, but the bound is still quoted here and in Chapter 22.

Which to use

KruskalPrim
GrowsA forest that mergesA single tree from a root
PicksGlobally lightest edge not forming a cycleLightest edge leaving the current tree
Key structureDisjoint sets (Ch 19)Min-priority queue (Ch 6)
TimeO(E lg V)O(E lg V), or O(E + V lg V) with a Fibonacci heap
Better onSparse graphs, or when edges arrive already sortedDense graphs — the array version is O(V²), which beats O(E lg V) when E ≈ V²
Both are GENERIC-MST with a different rule for finding a safe edge, and both are justified by the same Theorem 21.1. Kruskal applies Corollary 21.2 to the two components an edge would join; Prim applies Theorem 21.1 to the cut separating the tree from everything else.
Two facts worth knowing. If all edge weights are distinct, the MST is unique. If weights repeat, several MSTs may exist and different algorithms may return different ones — all of equal total weight. Also, MST algorithms assume the graph is connected; on a disconnected graph they produce a minimum spanning forest.

Recap

The seven things to carry forward

Where this goes next

Chapter 22 keeps the weights but changes the objective to shortest paths from a single source. It introduces relaxation, the technique underneath every shortest-path algorithm, then gives Bellman-Ford for graphs with negative edges, a linear-time DAG algorithm, and Dijkstra — which is Prim’s algorithm with the key changed from edge weight to accumulated distance.


Ch 20 — Elementary Graph Algorithms Ch 22 — Single-Source Shortest Paths