Chapter 7: Minimum Spanning Trees

Algorithms
Spanning treesCut and cycle propertiesKruskalUnion-Find PrimBorůvkaReverse-deleteComparison
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

The concept

Define spanning trees and minimum spanning trees (MSTs); explain the V − 1 edges and the cut and cycle properties.

Kruskal and Union-Find

Sort the edges and keep those that close no cycle; check for cycles with disjoint sets (find / union).

Prim

Grow a tree from one vertex; trace the key and parent arrays with a priority queue step by step.

Borůvka, Reverse-Delete, Analysis

Apply the component-based and deletion-based approaches; compare the running times of all four algorithms and pick the right one.

The MST Concept

Spanning Trees and Minimum Spanning Trees

  • G = (V, E): a connected, undirected graph with weighted edges.
  • A spanning tree is a connected, acyclic subgraph that contains every vertex. It always has exactly V − 1 edges.
  • A minimum spanning tree (MST) is a spanning tree with the smallest total edge weight.
  • Adding an edge to a tree creates a cycle; removing one splits the tree in two.
3154276aebcd
A graph can have very many spanning trees (nn−2 for a complete graph on n vertices, by Cayley's formula). Instead of trying them all, greedy algorithms find an MST directly. In the graph at right the MST weighs w(T) = 1 + 2 + 3 + 5 = 11.

The MST Concept

Example: The "MST" Graph

152251610781011254118202524124050
  • 15 vertices, 20 edges. The 14 bold edges (= V − 1) form the minimum spanning tree: total weight 141.
  • The thin edges are left out: each closes a cycle with the bold edges and is the heaviest edge on that cycle (e.g. 15, 16, 18, 25, 40, 50).
  • Heavy edges (20, 24, 25) can still be chosen: none of them is the heaviest on any cycle. For example, the cycle closed by 40 contains all three; the heaviest on that cycle is 40.

The MST Concept

The Cut Property and the Cycle Property

Cut propertyFor any cut splitting the vertices in two, the lightest edge crossing the cut belongs to an MST.
cut3154276aebcd

{a, b} | {c, d, e}: crossing edges ae = 1, be = 4, bc = 5 → ae is safe.

Cycle propertyThe heaviest edge on any cycle (if unique) is in no MST.
3154276aebcd

cycle b–c–e: 4, 5, 6 → ec = 6 is never chosen.

Kruskal and Prim rely on the cut property, reverse-delete on the cycle property. If all weights are distinct, the MST is unique.

The MST Concept

Greedy MST Algorithms

All four algorithms are greedy: each step takes the locally best edge (or throws away the worst), and that choice is never undone.

AlgorithmIdeaHelper structureTime
Kruskal (1956)Sort edges from light to heavy, add those that close no cycleunion-findO(E log E)
Prim (1957)Start at one vertex and grow the tree by the cheapest adjacent edgepriority queueO((V + E) log V)
Borůvka (1926)Every component adds its own cheapest edge at the same timeunion-findO(E log V)
Reverse-deleteTry edges from heavy to light, delete those whose removal keeps the graph connectedBFS/DFS or union-findO(E · (V + E))
All of them find an MST of the same weight; with distinct weights they find the same tree. They differ in the order they consider edges and in the data structures they need.

The MST Concept

Where Are MSTs Used?

Network designConnecting cities with the least cable: power, water, fiber and road networks. Borůvka's starting point was the power grid of Moravia.
ClusteringDeleting the k − 1 heaviest edges of the MST leaves k clusters (single-linkage clustering).
Approximation algorithmsFor the metric traveling salesman problem (TSP), an MST-based tour is at most twice the optimum.
Image processingSegmentation, with pixels as vertices and color differences as weights.
Circuit designConnecting pins with the shortest total wire (a first step in VLSI routing).
Other graph problemsEvery path in the MST is a bottleneck path (its largest edge is as small as possible); the basic approximation for Steiner trees.

Kruskal

Kruskal's Algorithm

  • Builds a tree of minimum total weight that connects all vertices; a greedy approach.
  • Developed by Joseph Kruskal (1956).
  • At first every vertex is a separate tree (a forest); step by step these trees are merged.
  • Edges are sorted from lightest to heaviest; an edge that closes no cycle is taken, one that closes a cycle is skipped.
1223457abcdefgreen: chosen · red: cycle (skip)blue: next, merges two trees

Strengths

  • Well suited to sparse graphs (E ≈ V).
  • Simple: an edge list plus union-find is enough.
  • On a disconnected graph it gives a minimum spanning forest.

Weaknesses

  • The cost of sorting edges: O(E log E).
  • On dense graphs (E ≈ V²) Prim can be faster.
  • Cycle checks need an extra structure (union-find).

Kruskal

Kruskal: Steps and Pseudocode

  1. Sort all edges of the graph by weight.
  2. Take the next edge with the smallest weight from the sorted list.
  3. If it joins two vertices in different trees (closes no cycle), add it and merge the two trees.
  4. If it joins two vertices of the same tree, it closes a cycle: skip it.
  5. Repeat steps 2–4 until V − 1 edges are chosen (or the edges run out).
KRUSKAL(G):
  MST = [ ]
  dsu.makeSets(V)
  edges.sort(by increasing weight)
  for each (u, v, weight) in edges:
    if dsu.find(u) ≠ dsu.find(v):
      MST.add((u, v, weight))
      dsu.union(u, v)
  return MST

"Are they in the same tree?" is answered in nearly constant time by the union-find (disjoint-set) structure.

Kruskal

Kruskal Step by Step

ab3ae1bc5be4cd2ed7ec63154276aebcd
MST—
w(T)0
Trees{a} {b} {c} {d} {e}

Start: each vertex is a separate tree (a forest of 5 trees). The edge table is still unsorted.

ae1cd2ab3be4bc5ec6ed73154276aebcd
MST—
w(T)0
Trees{a} {b} {c} {d} {e}

Step 1: sort the edges by increasing weight: ae, cd, ab, be, bc, ec, ed.

ae1cd2ab3be4bc5ec6ed73154276aebcd
MSTae
w(T)1
Trees{a, e} {b} {c} {d}

ae = 1: a and e are in different trees → add the edge; the two trees merge.

ae1cd2ab3be4bc5ec6ed73154276aebcd
MSTae, cd
w(T)3
Trees{a, e} {c, d} {b}

cd = 2: c and d are in different trees → add the edge; the two trees merge.

ae1cd2ab3be4bc5ec6ed73154276aebcd
MSTae, cd, ab
w(T)6
Trees{a, b, e} {c, d}

ab = 3: a and b are in different trees → add the edge; the two trees merge.

ae1cd2ab3be4bc5ec6ed73154276aebcd
MSTae, cd, ab
w(T)6
Trees{a, b, e} {c, d}

be = 4: b and e are already in the same tree → adding it would close a cycle, so skip it.

ae1cd2ab3be4bc5ec6ed73154276aebcd
MSTae, cd, ab, bc
w(T)11
Trees{a, b, c, d, e}

bc = 5: b and c are in different trees → add the edge; the two trees merge.

ae1cd2ab3be4bc5ec6ed73154276aebcd
MSTae, cd, ab, bc
w(T)11
Treesone tree

V − 1 = 4 edges chosen: the tree is complete, w(T) = 11. There is no need to look at ec and ed.

Kruskal

Union-Find (Disjoint Sets)

  • Keeps a collection of disjoint sets. Each set is a tree whose root is the set's representative. The parent array parent[]: parent[i] is i's parent, and a root has parent[r] = r.
  • makeSets(V): each vertex is its own set → parent[i] = i.
  • find(x): climb to the root and return it.
  • union(x, y): hang one root under the other.
  • In Kruskal: if find(u) == find(v), u and v are in the same tree → the edge closes a cycle.
int find(int[] parent, int i) {
  if (parent[i] != i)
    parent[i] = find(parent, parent[i]); // compress
  return parent[i];
}
void union(int[] parent, int x, int y) {
  parent[find(parent, x)] = find(parent, y);
}
Two improvements: path compression and union by rank. Together they give O(α(V)) amortized per operation; α (the inverse Ackermann function) is ≤ 4 in practice.

Kruskal

Cycle Checking with Union-Find

1234567acbdeDisjoint-set forest (arrow: parent)abcdeparent[ ]aabbccddee

makeSets(5): every vertex is the root of its own set, parent[i] = i. Edges: ac 1, de 2, ab 3, bc 4, bd 5, cd 6, ce 7.

1234567acbdeDisjoint-set forest (arrow: parent)abcdeparent[ ]cabbccddee

ac = 1: find(a) = a, find(c) = c → different → the edge joins the MST; parent[a] = c.

1234567acbdeDisjoint-set forest (arrow: parent)abcdeparent[ ]cabbccedee

de = 2: find(d) = d, find(e) = e → different → the edge joins the MST; parent[d] = e.

1234567acbdeDisjoint-set forest (arrow: parent)abcdeparent[ ]cabbbcedee

ab = 3: find(a) = c, find(b) = b → different → the edge joins the MST; parent[c] = b.

1234567acbdeDisjoint-set forest (arrow: parent)abcdeparent[ ]cabbbcedee

bc = 4: find(b) = b (path b), find(c) = b → same root: a cycle, so skip the edge.

1234567acbdeDisjoint-set forest (arrow: parent)abcdeparent[ ]caebbcedee

bd = 5: find(b) = b, find(d) = e → different → the edge joins the MST; parent[b] = e.

1234567acbdeDisjoint-set forest (arrow: parent)abcdeparent[ ]caebbcedee

The MST is complete (4 edges, w = 11). But the forest has become a long chain: find(a) follows a→c→b→e (3 steps).

1234567acbdeDisjoint-set forest (arrow: parent)abcdeparent[ ]eaebecedee

Path compression: as find(a) returns, it points every vertex on the path straight at the root (parent[a] = parent[c] = e). Later lookups take 1 step.

Kruskal · Step through the code

Union-Find: Tracing find and union

disjoint-set forest (arrow: parent)01234parent[ ]1011223344

Initially each element is the root of its own set: parent[i] = i. union(0, 1): find(0) = 0, find(1) = 1 → parent[0] = 1; 0 now hangs under 1.

disjoint-set forest (arrow: parent)01234parent[ ]1021223344

union(1, 2): find(1) = 1, find(2) = 2 → parent[1] = 2. A root was hung under a root, and the chain grew: 0 → 1 → 2.

disjoint-set forest (arrow: parent)01234parent[ ]1021323344

union(2, 3): parent[2] = 3. Chain 0 → 1 → 2 → 3: find(0) now takes 3 steps.

disjoint-set forest (arrow: parent)01234parent[ ]1021323344

parent[0] = 1 ≠ 0: 0 is not a root → call find(1).

disjoint-set forest (arrow: parent)01234parent[ ]1021323344

parent[1] = 2 ≠ 1: 1 is not a root → call find(2).

disjoint-set forest (arrow: parent)01234parent[ ]1021323344

parent[2] = 3 ≠ 2: 2 is not a root → call find(3).

disjoint-set forest (arrow: parent)01234parent[ ]1021323344

parent[3] = 3: 3 is a root. Return root 3; the recursion starts to unwind.

disjoint-set forest (arrow: parent)01234parent[ ]1021323344

On return, parent[2] = 3 (it already pointed to the root; unchanged).

disjoint-set forest (arrow: parent)01234parent[ ]1031323344

On return, parent[1] = 3: 1 now points straight to the root (2 → 3). Path compression.

disjoint-set forest (arrow: parent)01234parent[ ]3031323344

On return, parent[0] = 3: 0 now points straight to the root (1 → 3). Path compression.

disjoint-set forest (arrow: parent)01234parent[ ]3031323344

find(0) = 3. All of 0, 1, 2 on the path now point directly to 3: later find calls finish in one step.

disjoint-set forest (arrow: parent)01234parent[ ]3031323334

union(4, 0): find(4) = 4, find(0) = 3 (one step!) → parent[4] = 3. All elements are in one set with root 3.

int find(int[] parent, int i) {
  if (parent[i] != i)
    parent[i] = find(parent, parent[i]); // compress
  return parent[i];
}
void union(int[] parent, int x, int y) {
  parent[find(parent, x)] = find(parent, y);
}

Kruskal · Step through the code

Kruskal: Step Through the Code

22344612534

Sort the edges: (1,3)=2, (2,5)=2, (1,2)=3, (2,3)=4, (3,4)=4, (1,4)=6.

22344612534

parent[i] = i: each vertex is a separate tree (5 trees).

22344612534

e = (1,3), weight 2: find(1) = 1, find(3) = 3.

22344612534

x ≠ y → (1,3) joins the MST, parent[1] = 3. v1 and v3 merge.

22344612534

e = (2,5), weight 2: find(2) = 2, find(5) = 5.

22344612534

x ≠ y → (2,5) joins the MST, parent[2] = 5. v2 and v5 merge.

22344612534

e = (1,2), weight 3: find(1) = 3, find(2) = 5.

22344612534

x ≠ y → (1,2) joins the MST, parent[3] = 5. The two earlier trees merge.

22344612534

e = (2,3), weight 4: find(2) = 5, find(3) = 5.

22344612534

x = y = 5: (2,3) does not join two different trees → a cycle, skip it.

22344612534

e = (3,4), weight 4: find(3) = 5, find(4) = 4.

22344612534

x ≠ y → (3,4) joins the MST, parent[5] = 4. v4 joins the tree; w(T) = 11.

22344612534

tree.size() = 4 = V − 1: the loop ends. (1,4)=6 is never examined. w(T) = 11.

void kruskal(int V, List<Edge> es) {
  Collections.sort(es);         // ascending
  int[] parent = new int[V + 1];
  for (int i = 1; i <= V; i++) parent[i] = i;
  List<Edge> tree = new ArrayList<>();
  int k = 0;
  while (tree.size() < V - 1) {
    Edge e = es.get(k++);
    int x = find(parent, e.u);
    int y = find(parent, e.v);
    if (x != y) {               // different trees
      tree.add(e);
      parent[x] = y;            // union
    }
  }
}

Kruskal

Kruskal Example: Nine Vertices

EF1CF1DE2FH3AB4EH5GI5DG6AC7BD9EG10BC11HI12EI15BF2047119201126101553512ABCDEFGHISets: singletons · Weight: 0 · Connected: NO

Edges sorted: 1, 1, 2, 3, 4, 5, 5, 6, 7, 9, 10, 11, 12, 15, 20. No edge chosen yet.

EF1CF1DE2FH3AB4EH5GI5DG6AC7BD9EG10BC11HI12EI15BF2047119201126101553512ABCDEFGHISets: {E, F} · Weight: 1 · Connected: NO

EF = 1 added (two different sets). Total weight 1.

EF1CF1DE2FH3AB4EH5GI5DG6AC7BD9EG10BC11HI12EI15BF2047119201126101553512ABCDEFGHISets: {C, E, F} · Weight: 2 · Connected: NO

CF = 1 added (two different sets). Total weight 2.

EF1CF1DE2FH3AB4EH5GI5DG6AC7BD9EG10BC11HI12EI15BF2047119201126101553512ABCDEFGHISets: {C, D, E, F} · Weight: 4 · Connected: NO

DE = 2 added (two different sets). Total weight 4.

EF1CF1DE2FH3AB4EH5GI5DG6AC7BD9EG10BC11HI12EI15BF2047119201126101553512ABCDEFGHISets: {C, D, E, F, H} · Weight: 7 · Connected: NO

FH = 3 added (two different sets). Total weight 7.

EF1CF1DE2FH3AB4EH5GI5DG6AC7BD9EG10BC11HI12EI15BF2047119201126101553512ABCDEFGHISets: {C, D, E, F, H} {A, B} · Weight: 11 · Connected: NO

AB = 4 added (two different sets). Total weight 11.

EF1CF1DE2FH3AB4EH5GI5DG6AC7BD9EG10BC11HI12EI15BF2047119201126101553512ABCDEFGHISets: {C, D, E, F, H} {A, B} · Weight: 11 · Connected: NO

EH = 5: E and H are already in the same set (path E–F–H) → a cycle, not added. The total stays 11.

EF1CF1DE2FH3AB4EH5GI5DG6AC7BD9EG10BC11HI12EI15BF2047119201126101553512ABCDEFGHISets: {C, D, E, F, H} {A, B} {G, I} · Weight: 16 · Connected: NO

GI = 5 added (two different sets). Total weight 16.

EF1CF1DE2FH3AB4EH5GI5DG6AC7BD9EG10BC11HI12EI15BF2047119201126101553512ABCDEFGHISets: {C, D, E, F, G, H, I} {A, B} · Weight: 22 · Connected: NO

DG = 6 added (two different sets). Total weight 22.

EF1CF1DE2FH3AB4EH5GI5DG6AC7BD9EG10BC11HI12EI15BF2047119201126101553512ABCDEFGHISets: {A, B, C, D, E, F, G, H, I} · Weight: 29 · Connected: YES

AC = 7 added (two different sets). Total weight 29. 8 = V − 1 edges: MST = 29, and the graph is connected.

Kruskal

Kruskal: Complexity Analysis

OperationHow oftenCost
Sorting the edges by weight1O(E log E)
makeSets(V)1O(V)
find (2 per edge)≤ 2EO(E · α(V)) — path compression + rank
O(E log V) — rank only
unionV − 1
  • Total: O(E log E); sorting dominates. Since E ≤ V², log E ≤ 2 log V, so O(E log E) = O(E log V).
  • If the edges are already sorted (or the weights are small integers, so counting sort applies), the time is nearly linear: O(E · α(V)).
  • Memory: edge list + parent array (parent) → O(V + E).
With union by rank alone, union-find operations take O(log V) in the worst case; adding path compression brings the amortized cost per operation to O(α(V)). Either way, sorting determines the total.

Prim

Prim's Algorithm

  • Builds a minimum-weight tree that connects all vertices; a greedy algorithm.
  • Robert C. Prim (1957); V. Jarník found it earlier (1930), and E. W. Dijkstra rediscovered it (1959).
  • Pick a start vertex; at each step, add the non-tree vertex that connects to the tree by the cheapest edge.
  • At any moment there is a single tree (unlike Kruskal's forest).
236471sabcdcheapest crossing edge: 4

Strengths

  • Good for dense graphs: O(V²) with a matrix.
  • No edge sorting needed.
  • Works naturally with an adjacency list or matrix.

Weaknesses

  • Performance depends on the data structure (array, binary heap, Fibonacci heap).
  • On a disconnected graph it spans only the start vertex's component.

Prim

Prim: Steps, Keys and Parents

  1. Choose a start vertex and add all its edges to a priority queue.
  2. From the queue, take the lightest edge leading to a vertex not yet in the tree.
  3. Add the new vertex on that edge to the tree.
  4. Add the new vertex's edges to the queue; repeat steps 2–4 until every vertex is in the tree.
key[v]The weight of the cheapest edge found so far that connects v to the tree. ∞ at first, 0 at the source.
parent[v] (π)The tree end of that cheapest edge. At the end, the edges (parent[v], v) form the MST.

For the vertex taken from the queue, for each non-tree neighbor v: if weight(u, v) < key[v], update the key and the parent.

Prim

Prim: Pseudocode

PRIM(G, source):
  MST = [ ]
  dist = [ ]
  parent = [ ]
  visited = [ ]
  for each v in V:
    dist[v] = ∞
    visited[v] = 0
  dist[source] = 0
  pq.add(source, 0)
while pq is not empty do:
  u = pq.extractMin()
  if visited[u] == 0:
    visited[u] = 1
    for each (u, v, weight) in G[u]:
      if visited[v] == 0 and
           weight < dist[v]:
        dist[v] = weight
        parent[v] = u
        pq.add(v, weight)
for each v in V
    (except source, with dist[v] ≠ ∞):
  MST.add((parent[v], v, dist[v]))
return MST

This "lazy" version may add a vertex to the queue several times; the visited check throws away stale entries. The queue holds at most E entries → O(E log E).

Prim

Prim Step by Step: The Priority Queue

22344612534
keyuπ
01/
∞2/
∞3/
∞4/
∞5/

w(T) = 0

Initialization: the queue Q starts with every vertex; key(1) = 0, the others ∞, π = /.

22344612534
keyuπ
231
321
641
∞5/

w(T) = 0

Vertex 1 leaves the queue. Neighbors are updated: key(3) = 2 (1,3), key(2) = 3 (1,2), key(4) = 6 (1,4).

22344612534
keyuπ
321
443
∞5/

w(T) = 2

Vertex 3 leaves; (1,3) is added to T. key(4) becomes 4 (3,4); (2,3) = 4 > key(2) = 3 → unchanged.

22344612534
keyuπ
252
443

w(T) = 5

Vertex 2 leaves; (1,2) is added to T. key(5) = 2 (2,5).

22344612534
keyuπ
443

w(T) = 7

Vertex 5 leaves; (2,5) is added to T. It has no neighbor outside the tree: no update in Q.

22344612534
keyuπ
Q = ∅

w(T) = 11

Vertex 4 leaves; (3,4) is added to T. No update in Q.

22344612534
keyuπ
Q = ∅

w(T) = 11

Final state: Q = ∅. MST = (1,3), (1,2), (2,5), (3,4), w(T) = 11: the same tree as Kruskal.

Dashed line: the cheapest connection recorded in π, not yet final. The Q table is sorted by key.

Prim · Step through the code

Prim: Step Through the Code (Adjacency Matrix)

1332654432ABCDEF

Graph A–F, source B (index 1). All keys are ∞; key[B] = 0, parent[B] = −1.

1332654432ABCDEF

minKey → u = B (key 0): the source enters the tree.

1332654432ABCDEF

v = A: G[B][A] = 1 < key[A] = ∞ → key[A] = 1, parent[A] = B.

1332654432ABCDEF

v = C: G[B][C] = 3 < key[C] = ∞ → key[C] = 3, parent[C] = B.

1332654432ABCDEF

v = D: G[B][D] = 2 < key[D] = ∞ → key[D] = 2, parent[D] = B.

1332654432ABCDEF

v = E: G[B][E] = 6 < key[E] = ∞ → key[E] = 6, parent[E] = B.

1332654432ABCDEF

v = F: G[B][F] = 5 < key[F] = ∞ → key[F] = 5, parent[F] = B.

1332654432ABCDEF

minKey → u = A (key 1); (B,A) enters the tree. w(T) = 1. AD = 3 > key[D] = 2: no update.

1332654432ABCDEF

minKey → u = D (key 2); (B,D) enters the tree. w(T) = 3.

1332654432ABCDEF

v = E: G[D][E] = 3 < key[E] = 6 → key[E] = 3, parent[E] = D.

1332654432ABCDEF

minKey → u = C (key 3); (B,C) enters the tree. w(T) = 6. C and E tie at 3: the lower index, C, wins.

1332654432ABCDEF

v = F: G[C][F] = 4 < key[F] = 5 → key[F] = 4, parent[F] = C.

1332654432ABCDEF

minKey → u = E (key 3); (D,E) enters the tree. w(T) = 9.

1332654432ABCDEF

v = F: G[E][F] = 2 < key[F] = 4 → key[F] = 2, parent[F] = E.

1332654432ABCDEF

The loop ran V − 1 = 5 times; the last vertex, F, has parent E. MST = (parent[v], v): AB, BC, BD, DE, EF → w(T) = 11.

void prim(int[][] G, int src) {
  int V = G.length;
  int[] key = new int[V];
  int[] parent = new int[V];
  boolean[] inTree = new boolean[V];
  Arrays.fill(key, Integer.MAX_VALUE);
  key[src] = 0;
  parent[src] = -1;
  for (int s = 0; s < V - 1; s++) {
    int u = minKey(key, inTree);
    inTree[u] = true;
    for (int v = 0; v < V; v++)
      if (G[u][v] != 0 && !inTree[v]
          && G[u][v] < key[v]) {
        key[v] = G[u][v];
        parent[v] = u;
      }
  }
}

Prim · Step through the code

Prim: Tracing the Lazy Priority-Queue Version

22344612534pq(1,0)

Undirected graph with five vertices, source 1. key[1] = 0, the others ∞; the queue holds a single (vertex, key) entry, (1,0). In the parent array (parent[]), − means not yet assigned.

22344612534pq(3,2)(2,3)(4,6)

(1,0) is the smallest entry: 1 enters the tree. key[2]: ∞ → 3, key[3]: ∞ → 2, key[4]: ∞ → 6; the new entries are added to the queue.

22344612534pq(2,3)(4,4)(4,6)

(3,2) is the smallest entry: 3 enters the tree (edge 1–3, weight 2). key[4]: 6 → 4; the new entries are added to the queue. The edge of weight 4 to 2 costs more than the current 3: no update. The entry (4,6) stays in the queue.

22344612534pq(5,2)(4,4)(4,6)

(2,3) is the smallest entry: 2 enters the tree (edge 1–2, weight 3). key[5]: ∞ → 2; the new entries are added to the queue.

22344612534pq(4,4)(4,6)

(5,2) is the smallest entry: 5 enters the tree (edge 2–5, weight 2). No key of a non-tree neighbor decreased.

22344612534pq(4,6)

(4,4) is the smallest entry: 4 enters the tree (edge 3–4, weight 4). No key of a non-tree neighbor decreased.

22344612534pq

(4,6) comes out, but 4 is already in the tree: it is a stale entry, skipped with continue (lazy deletion).

22344612534pq

The queue is empty. MST edges (parent[v], v): 1–3 (2), 1–2 (3), 2–5 (2), 3–4 (4); total 11.

void primPQ(int src) {
  Arrays.fill(key, INF);
  key[src] = 0;
  PriorityQueue<int[]> pq =
      new PriorityQueue<>((a, b) -> a[1] - b[1]);
  pq.add(new int[] { src, 0 });
  while (!pq.isEmpty()) {
    int u = pq.poll()[0];
    if (inTree[u]) continue;  // stale
    inTree[u] = true;
    for (Edge e : adj.get(u))
      if (!inTree[e.v] && e.w < key[e.v]) {
        key[e.v] = e.w; parent[e.v] = u;
        pq.add(new int[] { e.v, e.w });
      }
  }
}

Prim

Prim Example: Nine Vertices (Starting at A)

47119201126101553512ABCDEFGHI
ABCDEFGHI
key047∞∞∞∞∞∞
parent−AA−−−−−−

Start at A: its neighbors B (4) and C (7) become candidates.

47119201126101553512ABCDEFGHI
ABCDEFGHI
key0479∞20∞∞∞
parent−AAB−B−−−

B enters the tree: (A,B) = 4, total 4. Updated: D → 9, F → 20.

47119201126101553512ABCDEFGHI
ABCDEFGHI
key0479∞1∞∞∞
parent−AAB−C−−−

C enters the tree: (A,C) = 7, total 11. Updated: F → 1.

47119201126101553512ABCDEFGHI
ABCDEFGHI
key047911∞3∞
parent−AABFC−F−

F enters the tree: (C,F) = 1, total 12. Updated: E → 1, H → 3.

47119201126101553512ABCDEFGHI
ABCDEFGHI
key04721110315
parent−AAEFCEFE

E enters the tree: (F,E) = 1, total 13. Updated: D → 2, G → 10, I → 15.

47119201126101553512ABCDEFGHI
ABCDEFGHI
key0472116315
parent−AAEFCDFE

D enters the tree: (E,D) = 2, total 15. Updated: G → 6.

47119201126101553512ABCDEFGHI
ABCDEFGHI
key0472116312
parent−AAEFCDFH

H enters the tree: (F,H) = 3, total 18. Updated: I → 12. EH = 5 can no longer be used: E and H are both in the tree (a cycle).

47119201126101553512ABCDEFGHI
ABCDEFGHI
key047211635
parent−AAEFCDFG

G enters the tree: (D,G) = 6, total 24. Updated: I → 5.

47119201126101553512ABCDEFGHI
ABCDEFGHI
key047211635
parent−AAEFCDFG

I enters the tree: (G,I) = 5, total 29. Every vertex is in the tree: MST = 29 (same as Kruskal).

Prim

Prim: Complexity Analysis

Implementationextract-mindecrease-keyTotal
Adjacency matrix + array scan (the code above)V × O(V)O(1)O(V²)
Adjacency list + binary heapV × O(log V)E × O(log V)O((V + E) log V)
Lazy version (pseudocode): edge copies in the queue≤ E inserts/removals, each O(log E)O(E log E)
Fibonacci heapO(log V) amortizedO(1) amortizedO(E + V log V)
Dense graphs (E ≈ V²)The matrix version, O(V²), is best; the heap version becomes O(V² log V).
Sparse graphs (E ≈ V)The heap version is O(V log V); the matrix version needlessly costs O(V²).

Prim

How Prim Differs from Dijkstra

The two algorithms have almost the same code: start at a source, extract the vertex with the smallest key from a priority queue, and relax its neighbors. The only difference is what the key means.

Prim (MST)Dijkstra (shortest paths)
key[v]the cheapest single edge joining v to the treelength of the whole path from the source to v
Updatew(u,v) < key[v]dist[u] + w(u,v) < dist[v]
Resulttree of minimum total weightshortest-path tree (SPT)
Sourcedoes not affect the result (same weight)determines the result
Negative weightsno problemfails

In the five-vertex example, the shortest-path tree from 1 can use the edge (1,4) = 6; the MST takes (3,4) = 4 instead.

Borůvka

Borůvka's (Sollin's) Algorithm

  • The oldest MST algorithm: Otakar Borůvka, 1926 (the Moravian power grid). Also known as Sollin's algorithm.
  • At first every vertex is an independent component.
  • In each round every component picks the cheapest edge leaving it; all the picks are added at once.
  • Components are merged with union-find until only one is left. On a connected graph it yields |V| − 1 edges.
1429367518abcdefghround 1: each vertex picks its own cheapest edge→ 8 components shrink to 3

Strengths

  • Components choose independently: well suited to parallel execution.
  • At most ⌈log₂ V⌉ rounds.

Weaknesses

  • Harder to code than Kruskal or Prim.
  • With equal weights a consistent tie-breaker (e.g. edge index) is essential; otherwise a cycle can form.

Borůvka

Borůvka: Steps and Pseudocode

  1. At first each vertex counts as a component (set) on its own.
  2. For each component, find the lightest edge connecting it to another component.
  3. Add all the edges found and merge the components they connect (no edge is added twice).
  4. Repeat steps 2 and 3 until one component remains; stop if a round adds no edge (disconnected graph → forest).

Note: Borůvka does not sort the edges; each round scans all edges once and keeps the lightest per component.

BORUVKA(G):
  MST = [ ]
  numComp = |V|
  while numComp > 1 do:
    cheapest = [ ]  // per component
    for each e = (u, v, weight) in E:
      cu = dsu.find(u)
      cv = dsu.find(v)
      if cu ≠ cv:
        cheapest[cu] = min(cheapest[cu], e)
        cheapest[cv] = min(cheapest[cv], e)
    added = false
    for each (u, v, weight) in cheapest:
      if dsu.find(u) ≠ dsu.find(v):
        MST.add((u, v, weight))
        dsu.union(u, v)
        numComp -= 1
        added = true
    if not added: break   // forest
  return MST

Borůvka

Borůvka Step by Step: Six Vertices

154736982ABCDEF

Start: each vertex is a separate component (6 components).

154736982ABCDEF
ComponentCheapest
{A}AB 1
{B}AB 1
{C}BC 5
{D}AD 4
{E}EF 2
{F}EF 2

Round 1: each component picks its own cheapest outgoing edge (blue). AB and EF are each picked by two components but added once.

154736982ABCDEF

End of round 1: 4 distinct edges added (AB, BC, AD, EF), 2 components left: {A, B, C, D} {E, F}. Total 12.

154736982ABCDEF
ComponentCheapest
{A,B,C,D}BE 3
{E,F}BE 3

Round 2: each component picks its own cheapest outgoing edge (blue). Both components pick the same edge.

154736982ABCDEF

End of round 2: 1 distinct edge added (BE), 1 component left: {A, B, C, D, E, F}. Total 15. MST = 15 (AB, BC, AD, EF, BE).

Kruskal adds the edges 1, 2, 3, 4, 5 one at a time; Borůvka adds four edges at once in round 1. The resulting tree is the same.

Borůvka

Borůvka Example: Twelve Vertices

1367114820932181519105171116412ABCDEFGHIJKL

Start: each vertex is a separate component (12 components).

1367114820932181519105171116412ABCDEFGHIJKL
ComponentCheapest
{A}AC 6
{B}BD 1
{C}AC 6
{D}BD 1
{E}EF 2
{F}EF 2
{G}GI 5
{H}GH 15
{I}GI 5
{J}JL 4
{K}GK 10
{L}JL 4

Round 1: the cheapest outgoing edge of each component (blue).

1367114820932181519105171116412ABCDEFGHIJKL

End of round 1: 7 distinct edges added (AC, BD, EF, GI, GH, JL, GK), 5 components left: {G, H, I, K} {A, C} {B, D} {E, F} {J, L}. Total 43.

1367114820932181519105171116412ABCDEFGHIJKL
ComponentCheapest
{A,C}BC 7
{B,D}DF 3
{E,F}DF 3
{G,H,I,K}KL 12
{J,L}KL 12

Round 2: the cheapest outgoing edge of each component (blue).

1367114820932181519105171116412ABCDEFGHIJKL

End of round 2: 3 distinct edges added (BC, DF, KL), 2 components left: {A, B, C, D, E, F} {G, H, I, J, K, L}. Total 65.

1367114820932181519105171116412ABCDEFGHIJKL
ComponentCheapest
{A,B,C,D,E,F}EJ 18
{G,H,I,J,K,L}EJ 18

Round 3: the cheapest outgoing edge of each component (blue).

1367114820932181519105171116412ABCDEFGHIJKL

End of round 3: 1 distinct edge added (EJ), 1 component left: {A, B, C, D, E, F, G, H, I, J, K, L}. MST = 83, in just 3 rounds.

Borůvka

Borůvka: Java Code and Complexity

while (numComp > 1) {
  Edge[] cheapest = new Edge[V];
  for (Edge e : edges) {         // 1) cheapest
    int k1 = find(parent, e.u), k2 = find(parent, e.v);
    if (k1 == k2) continue;         // same comp.
    if (cheapest[k1] == null || e.w < cheapest[k1].w)
      cheapest[k1] = e;
    if (cheapest[k2] == null || e.w < cheapest[k2].w)
      cheapest[k2] = e;
  }
  boolean added = false;
  for (Edge e : cheapest) {      // 2) add them
    if (e == null) continue;
    int k1 = find(parent, e.u), k2 = find(parent, e.v);
    if (k1 == k2) continue;         // already in
    tree.add(e);
    parent[k1] = k2;                // union
    numComp--;
    added = true;
  }
  if (!added) break;  // forest
}
  • In each round every component merges with at least one other → the number of components at least halves.
  • Number of rounds ≤ ⌈log₂ V⌉; each round scans all edges once: O(E).
  • Total: O(E log V). No sorting is done; no "weight order" is needed.
  • If edges are added at the same time, a consistent tie-breaker is needed; in this sequential code the find(k1) == find(k2) check already prevents cycles.
  • If a round adds no edge (added false), the loop exits: a minimum spanning forest for a disconnected graph.

Borůvka · Step through the code

Borůvka: Step Through the Code

154736982ABCDEF

Six components (each vertex alone); edges are scanned in list order. Edge AB(1): cheapest[A] and cheapest[B] = AB(1).

154736982ABCDEF

Edge BC(5): cheapest[C] = BC(5). Edge AD(4): cheapest[D] = AD(4).

154736982ABCDEF

Edge BE(3): cheapest[E] = BE(3).

154736982ABCDEF

BD(7) beat no component's cheapest. Edge BF(6): cheapest[F] = BF(6).

154736982ABCDEF

CF(9) and DE(8) lost to cheaper edges. Edge EF(2): cheapest[E] and cheapest[F] = EF(2), cheaper than 3 for E and 6 for F.

154736982ABCDEF

AB(1) joins the tree; find gives roots A and B → parent[A] = B. Components: 5.

154736982ABCDEF

cheapest[B] = AB(1), but both ends are now in one component: already added, skip.

154736982ABCDEF

BC(5) joins the tree; find gives roots B and C → parent[B] = C. Components: 4.

154736982ABCDEF

AD(4) joins the tree; find gives roots C and D → parent[C] = D. Components: 3.

154736982ABCDEF

EF(2) joins the tree; find gives roots E and F → parent[E] = F. Components: 2.

154736982ABCDEF

cheapest[F] = EF(2), but both ends are now in one component: already added, skip.

154736982ABCDEF

Round 2: AB, BC, AD, BD, EF stay inside a component (continue). Among the edges between components, BE(3), BF(6), CF(9), DE(8), the cheapest is BE(3): both roots' cheapest is BE.

154736982ABCDEF

BE(3) joins the tree; find gives roots D and F → parent[D] = F. Components: 1. One component is left.

154736982ABCDEF

cheapest[F] = BE(3), but both ends are now in one component: already added, skip. numComp = 1: the loop ends, MST weight 15.

while (numComp > 1) {
  Edge[] cheapest = new Edge[V];
  for (Edge e : edges) {      // 1) cheapest
    int k1 = find(parent, e.u), k2 = find(parent, e.v);
    if (k1 == k2) continue;
    if (cheapest[k1] == null || e.w < cheapest[k1].w)
      cheapest[k1] = e;
    if (cheapest[k2] == null || e.w < cheapest[k2].w)
      cheapest[k2] = e;
  }
  boolean added = false;
  for (Edge e : cheapest) {   // 2) add them
    if (e == null) continue;
    int k1 = find(parent, e.u), k2 = find(parent, e.v);
    if (k1 == k2) continue;
    tree.add(e);
    parent[k1] = k2;        // union
    numComp--;
    added = true;
  }
  if (!added) break;  // forest
}

Reverse-Delete

The Reverse-Delete Algorithm

  • A greedy MST algorithm (Kruskal proposed it in his 1956 paper).
  • Sorts the edges by weight in decreasing order.
  • Starting from the heaviest edge, deletes every edge whose removal keeps the graph connected.
  • Relies on the cycle property: a deleted edge is the heaviest edge on some cycle.
Kruskal's mirror imageKruskal adds the lightest, reverse-delete deletes the heaviest edges. One starts from the empty graph, the other from the full graph.
349abc
UseMostly of theoretical and teaching value; since every deletion needs a connectivity test, it is usually slow in practice.

Reverse-Delete

Reverse-Delete: Steps, Pseudocode, Complexity

  1. Sort all edges by weight in decreasing order.
  2. Delete the next heaviest edge.
  3. If the graph is no longer connected, put the deleted edge back.
  4. Repeat steps 2 and 3 until every edge has been processed.
Complexity: one BFS/DFS per edge (or a union-find rebuild) → O(E · (V + E)). The O(E log E) cost of sorting is small next to that.
REVERSE_DELETE(G):
  MST = all edges
  edges.sort(by decreasing weight)
  for each (u, v, weight) in edges:
    MST.remove((u, v, weight))
    dsu.reset()
    for each (x, y) in MST:
      dsu.union(x, y)
    root = dsu.find(V[0])
    if dsu.find(v) ≠ root for some v:
      MST.add((u, v, weight))  // put back
  return MST

The connectivity test looks at every vertex; if at least one vertex is cut off from the root, the deleted edge is put back once.

Reverse-Delete

Reverse-Delete Step by Step: Seven Vertices

58911871596752465130
EdgewStatus
3–415
5–611
1–39
4–69
1–28
4–58
1–47
0–17
3–56
2–45
0–35

Edges in decreasing order: 15, 11, 9, 9, 8, 8, 7, 7, 6, 5, 5. Total weight 90.

58911871596752465130
EdgewStatus
3–415deleted
5–611
1–39
4–69
1–28
4–58
1–47
0–17
3–56
2–45
0–35

Delete 3–4 = 15: the graph stays connected (the edge is the heaviest on a cycle).

58911871596752465130
EdgewStatus
3–415deleted
5–611deleted
1–39
4–69
1–28
4–58
1–47
0–17
3–56
2–45
0–35

Delete 5–6 = 11: the graph stays connected (the edge is the heaviest on a cycle).

58911871596752465130
EdgewStatus
3–415deleted
5–611deleted
1–39deleted
4–69
1–28
4–58
1–47
0–17
3–56
2–45
0–35

Delete 1–3 = 9: the graph stays connected (the edge is the heaviest on a cycle).

58911871596752465130
EdgewStatus
3–415deleted
5–611deleted
1–39deleted
4–69kept
1–28
4–58
1–47
0–17
3–56
2–45
0–35

Deleting 4–6 = 9 would isolate vertex 6 → connectivity breaks, so the edge is put back.

58911871596752465130
EdgewStatus
3–415deleted
5–611deleted
1–39deleted
4–69kept
1–28deleted
4–58
1–47
0–17
3–56
2–45
0–35

Delete 1–2 = 8: the graph stays connected (the edge is the heaviest on a cycle).

58911871596752465130
EdgewStatus
3–415deleted
5–611deleted
1–39deleted
4–69kept
1–28deleted
4–58deleted
1–47
0–17
3–56
2–45
0–35

Delete 4–5 = 8: the graph stays connected (the edge is the heaviest on a cycle).

58911871596752465130
EdgewStatus
3–415deleted
5–611deleted
1–39deleted
4–69kept
1–28deleted
4–58deleted
1–47kept
0–17kept
3–56kept
2–45kept
0–35kept

The remaining 6 edges (7, 7, 6, 5, 5 and 9) are all bridges: none can be deleted. 6 = V − 1 edges, MST = 39.

Reverse-Delete · Step through the code

Reverse-Delete: Step Through the Code

7589751568911ABCDEFG

Sort the edges in decreasing order: DE 15, FG 11, BD 9, EG 9, BC 8, EF 8, AB 7, BE 7, DF 6, AD 5, CE 5.

7589751568911ABCDEFG

Heaviest: DE = 15 deleted, still connected.

7589751568911ABCDEFG

FG = 11 deleted, still connected.

7589751568911ABCDEFG

BD = 9 deleted, still connected.

7589751568911ABCDEFG

EG = 9 would isolate G: back, i = 1.

7589751568911ABCDEFG

BC = 8 deleted, still connected.

7589751568911ABCDEFG

EF = 8 deleted, still connected.

7589751568911ABCDEFG

AB = 7 cuts off {A, D, F}: back, i = 2.

7589751568911ABCDEFG

BE = 7 cuts off {C, E, G}: back, i = 3.

7589751568911ABCDEFG

DF = 6 cuts off {F}: back, i = 4.

7589751568911ABCDEFG

AD = 5 cuts off {D, F}: back, i = 5.

7589751568911ABCDEFG

CE = 5 cuts off {C}: back, i = 6.

7589751568911ABCDEFG

i = size = 6: the loop ends. No more edges to delete; the remaining tree is AB, AD, BE, CE, DF, EG → w = 39.

void reverseDelete(int V, List<Edge> es) {
  es.sort((a, b) -> b.w - a.w); // descending
  int i = 0;
  while (i < es.size()) {
    Edge e = es.remove(i);      // try deleting
    if (!isConnected(V, es)) {
      es.add(i, e);             // put back
      i++;
    }
  }
}

Comparison

Comparing the Four Algorithms

KruskalPrimBorůvkaReverse-delete
Viewpointedge-centeredvertex-centeredcomponent-centerededge-centered (deletion)
Intermediate stateforestone treeforestconnected graph
Property usedcutcutcutcycle
Data structuresorted edges + union-findpriority queue / arrayunion-findBFS/DFS
TimeO(E log E)O(V²) or O((V + E) log V)O(E log V)O(E · (V + E))
Best forsparse graphsdense graphsparallel / distributedteaching, theory
Disconnected graphgives a forestone componentgives a forestkeeps the components
On the nine-vertex graph Kruskal and Prim find the same tree (29) but add edges in a different order: Kruskal 1, 1, 2, 3, 4, 5, 6, 7; Prim 4, 7, 1, 1, 2, 3, 6, 5.

Summary

Summary

TopicKey points
MSTspanning tree of minimum total weight in a connected, undirected, weighted graph; V − 1 edges
Propertiesthe lightest edge across a cut is safe; the heaviest edge on a cycle is out; distinct weights → unique MST
Kruskalsort the edges; if find(u) ≠ find(v), add the edge and merge the trees — O(E log E)
Union-Finda parent[] forest; path compression + rank → O(α(V)) per operation
Primgrow a single tree; key = cheapest edge to the tree — O(V²) / O((V + E) log V)
Borůvkaevery component adds its cheapest outgoing edge at once; ≤ log₂ V rounds — O(E log V)
Reverse-deletedelete in decreasing order, put back if connectivity breaks — O(E · (V + E))

Summary

Common Mistakes

Confusing the MST with a shortest-path treeThe MST minimizes total weight; the MST path between two vertices need not be a shortest path.
Checking cycles by adjacencyIn Kruskal, "are u and v directly connected?" is not enough; ask whether they are in the same component (find).
Adding up distances in PrimWriting key[u] + w gives Dijkstra. In Prim the key is the weight of a single edge.
Fearing negative weightsMST algorithms work correctly with negative weights too (they only compare); the problem is specific to Dijkstra.
Disconnected graphsThere is no MST, only a minimum spanning forest. while (tree.size() < V − 1) runs past the end of the edge list.
Applying them to directed graphsThese algorithms are for undirected graphs. The directed counterpart (minimum spanning arborescence) is the Chu–Liu/Edmonds algorithm.

Summary

Worked Problems

Graph: AB = 2, AC = 3, BC = 1, BD = 4, CD = 5, CE = 6, DE = 7 (undirected).

  1. List the edges Kruskal examines, in order; which are rejected? w(T) = ?
  2. Run Prim from A: in what order do the vertices enter the tree? What is the key array after the first step?
  3. How many rounds does Borůvka take? Which edges are chosen in round 1?
  4. Which edges does reverse-delete remove?
  5. How many edges does the MST of a connected graph with 100 vertices have?
  6. If 5 is added to every weight, does the MST change? What is the new total?
Show answers
  1. BC 1 ✓, AB 2 ✓, AC 3 ✗ (cycle), BD 4 ✓, CD 5 ✗, CE 6 ✓ → 4 edges, w(T) = 13. DE is never examined.
  2. A, B (2), C (1, via B), D (4), E (6). After A: key[B] = 2, [C] = 3, [D] = [E] = ∞.
  3. 1 round: A→AB, B→BC, C→BC, D→BD, E→CE; four distinct edges make a single component.
  4. DE 7 is deleted, CE 6 stays (E would be cut off), CD 5 is deleted, BD 4 stays, AC 3 is deleted, AB and BC stay.
  5. V − 1 = 99.
  6. No: the order of the edges is preserved. The total becomes 13 + 4 · 5 = 33.

Summary

Exercises

  1. Use the cut property to show that the MST is unique when all weights are distinct.
  2. How would you change Kruskal to find a maximum spanning tree?
  3. Use the MST of the nine-vertex example to split the vertices into 3 clusters.
  4. On 0..5 with parent[find(x)] = find(y): union (0,1), (2,3), (1,3), (4,5), (3,5). What is the final parent? What does find(0) change?
  5. V = 1000: compare the O(V²) and O(E log V) versions of Prim for E = 5,000 and 500,000.
  6. Fix Kruskal so that it returns a forest on a disconnected graph.
Answers / hints
  1. Every MST edge e is the unique lightest edge across the cut defined by T − e; that edge must be in every MST → all MSTs are the same.
  2. Process the edges in decreasing order (or use −w).
  3. Delete the 2 heaviest MST edges (AC 7, DG 6): {C, D, E, F, H} {A, B} {G, I}.
  4. parent = [1, 3, 3, 5, 5, 5]. find(0): the path 0→1→3→5 is compressed → parent = [5, 5, 3, 5, 5, 5].
  5. V² = 10⁶; E log V ≈ 5 · 10⁴ (the heap wins) and ≈ 5 · 10⁶ (the matrix wins).
  6. Condition: k < es.size() && tree.size() < V − 1; if tree.size() < V − 1 at the end, the graph is disconnected.

Summary

References

  • Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed., MIT Press, 2022 (Chapter 19: disjoint sets, Chapter 21: MSTs).
  • Sedgewick and Wayne, Algorithms, 4th ed., Addison-Wesley, 2011 (Section 4.3: Minimum Spanning Trees).
  • J. B. Kruskal, "On the Shortest Spanning Subtree of a Graph and the Traveling Salesman Problem", Proc. AMS 7(1), 1956.
  • R. C. Prim, "Shortest Connection Networks and Some Generalizations", Bell System Technical Journal 36(6), 1957.
  • O. Borůvka, "O jistém problému minimálním", Práce Moravské přírodovědecké společnosti 3, 1926.
  • R. E. Tarjan, "Efficiency of a Good But Not Linear Set Union Algorithm", Journal of the ACM 22(2), 1975.

The End

Chapter 7: Minimum Spanning Trees
Previous: Chapter 6: Shortest Paths · Next: Chapter 8: Network Flow
1 / 1 Sercan KÜLCÜ. All rights reserved.