Chapter 17: Minimum Spanning Trees

Data Structures and Algorithms with C
Spanning treesIsomorphismGreedy methodKruskalUnion-findPrimHeap
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Define

Explain spanning trees, tree and nontree edges, and depth-first and breadth-first spanning trees.

Count

Discuss ways to generate the spanning trees of a graph, and the idea of isomorphism.

Kruskal

Trace Kruskal's algorithm in C: edges in increasing cost, cycles caught with union-find.

Prim

Implement Prim's algorithm, which grows a single tree, with O(n²) arrays and with a heap.

Analyze

Compare the O(e log e), O(n²) and O(e log n) bounds and choose by graph density.

Approach

“Discovering the data structure”: how do a min heap and sets speed up the algorithm?

Spanning Trees

A Traversal Splits the Edges in Two

When DFS or BFS visits every vertex of a connected graph, the edges fall into two groups:

T: tree edges

Edges that were followed to a new vertex (thick red). There are exactly n − 1 of them.

N: nontree edges

Edges never followed (dashed). In DFS they are back edges, in BFS cross edges.

0123456701234567depth-first spanning tree (from 0)breadth-first spanning tree (from 0)

DFS: N = {(0,2), (1,4), (6,7)} · BFS: N = {(4,7), (5,7), (6,7)}

Spanning Trees

Definition: Spanning Tree

Definition: a spanning tree is a tree made of edges of the graph that connects all the vertices: n vertices, n − 1 edges, no cycle.
K4a)b)c)d)e)
Isomorphism

K4 is a complete graph. a and b are two different spanning trees (a is a path, b a star); c, d and e are isomorphic to a: a ≡ c ≡ d ≡ e.

How many?

K4 has 16 spanning trees (Cayley: nⁿ⁻² = 4² = 16): 12 paths + 4 stars. Once weights come in, “how many?” is asked again.

Spanning Trees

Isomorphism, Drawings and Planarity

123abc1b2a3cK3,3another drawing: one crossing (red)

The same graph (K3,3), two drawings

  • DFS/BFS help test isomorphism, but in general it is a hard problem.
  • A graph that can be drawn without crossing edges is planar. K3,3 is not planar: at least one crossing is needed.
  • In circuit design, a K3,3-shaped circuit needs at least two layers.
From the lecture: “This would make a nice senior project: a program, an algorithm that shows a ≡ e with a 3D view… Which vertices have to swap places?”
From the lecture: “These are hard problems… You can work on them in a master's or PhD. And by that we are also saying: ‘Without a master's or PhD, the problem won't get solved.’”

Spanning Trees

Question: How Many Spanning Trees Are There?

n − 1 of the e edges can be chosen in C(e, n − 1) ways; each choice is checked for a cycle and isomorphic copies are removed. Here 8 of the C(5, 3) = 10 choices are trees.

ABCDinitial tree T (3 edges)

First find one spanning tree (e.g. with DFS).

ABCD+ (D,A) → cycle A-B-C-D-A

Add a nontree edge: exactly one cycle appears.

ABCD(A,B) removed → new tree

Remove (A,B) from the cycle → a different spanning tree. Put it back and remove the next one.

ABCD(B,C) removed → new tree

Remove (B,C) from the cycle → a different spanning tree. Put it back and remove the next one.

ABCD(C,D) removed → new tree

Remove (C,D) from the cycle → a different spanning tree. Put it back and remove the next one.

ABCD8 different trees in all

Repeat for every nontree edge. This graph has 8 spanning trees, but the method is long and produces duplicates.

The algorithm is long, so it is not a sensible choice. (For the exact count, use Kirchhoff's matrix-tree theorem, an advanced topic.)

Spanning Trees

Properties of Spanning Trees

01234567

BFS spanning tree: 8 vertices, 7 edges (thick red); the 3 nontree edges are dashed.

01234567

Adding the nontree edge (6,7) creates exactly one cycle: 6-2-0-1-3-7-6. It is no longer a tree.

01234567

Deleting any tree edge, e.g. (0,2), splits the tree: {2, 5, 6} breaks off. A spanning tree is a minimal connected subgraph.

  • Exactly n − 1 edges, no cycle, connected: any two of these imply the third.
  • There is a unique path between any two vertices.
  • Adding a nontree edge creates a single cycle; removing any edge of that cycle gives a new spanning tree (the generation method relies on this).
  • Uses: dropping redundant links in a network, broadcast trees, circuit equations (Kirchhoff).

Minimum Cost

Minimum Cost Spanning Tree (MST)

  • The edges of an undirected graph can be weighted: G(V, E, w) instead of G(V, E).
  • The spanning tree(s) with the least total edge cost: the minimum spanning tree.
  • Ex. a: edge costs are the costs of links between computers.
  • Ex. b: power or fiber for 7 villages: a feasibility study gives the km of cable between village pairs. Pairs with no link cost ∞ (not shown).
2810161412221825240156243

Sample graph

From the lecture: “When we pick edges for a minimum spanning tree we avoid cycles… ‘If the power doesn't come from Lower Tepecik, let it come from Upper Tepecik.’ No way!”

Minimum Cost

Algorithms and the Greedy Approach

Algorithms

Kruskal, Prim, Sollin (Borůvka), and also reverse-delete. This chapter covers the first two.

Design methods

Backtracking, greedy, dynamic programming…

Greedy = stubborn

Each step makes the best choice for the moment and never revises it; there is no going back.

Three rules

1) Never take an edge that forms a cycle. 2) Prefer the cheapest edge. 3) Choose exactly n − 1 edges for n vertices.

Why is it correct? (extra) Cut property: for any cut that splits the vertices in two, the cheapest edge crossing the cut is in some MST. Kruskal and Prim pick such an edge at every step, so the greedy choice is never regretted.

“Beauty is the purgation of superfluities.” — Michelangelo

Kruskal

Kruskal's Algorithm (1956)

T = {};                    /* chosen edges */
while (|T| < n-1 && E ≠ Ø) {
    choose a least-cost edge (v,w) from E;
    E = E - {(v,w)};
    if ((v,w) does not create a cycle in T)
        T = T ∪ {(v,w)};
    else
        discard (v,w);
}
if (|T| < n-1)
    printf("no spanning tree\n");
  • T: chosen edges, E: remaining edges, |T|: number of chosen edges.
  • The loop runs until n − 1 edges are chosen; it also stops if no edges are left before that → the graph is not connected.
  • In Kruskal the chosen edges may form separate pieces (a forest); at the end there is a single tree.
  • Joseph B. Kruskal, 1956; its correctness is proved. “Reading the proof makes it clearer.”

Kruskal

Kruskal Step by Step

2810161412221825240156243
EdgewAction
(0,5)10
(2,3)12
(1,6)14
(1,2)16
(3,6)18
(3,4)22
(4,6)24
(4,5)25
(0,1)28

The edges are sorted by cost; every vertex is a piece of its own.

2810161412221825240156243
EdgewAction
(0,5)10Add to tree
(2,3)12
(1,6)14
(1,2)16
(3,6)18
(3,4)22
(4,6)24
(4,5)25
(0,1)28

(0,5) = 10: ends in different pieces → added. Total 10.

2810161412221825240156243
EdgewAction
(0,5)10Add to tree
(2,3)12Add to tree
(1,6)14
(1,2)16
(3,6)18
(3,4)22
(4,6)24
(4,5)25
(0,1)28

(2,3) = 12: ends in different pieces → added. Total 22.

2810161412221825240156243
EdgewAction
(0,5)10Add to tree
(2,3)12Add to tree
(1,6)14Add to tree
(1,2)16
(3,6)18
(3,4)22
(4,6)24
(4,5)25
(0,1)28

(1,6) = 14: ends in different pieces → added. Total 36.

2810161412221825240156243
EdgewAction
(0,5)10Add to tree
(2,3)12Add to tree
(1,6)14Add to tree
(1,2)16Add to tree
(3,6)18
(3,4)22
(4,6)24
(4,5)25
(0,1)28

(1,2) = 16: ends in different pieces → added. Total 52.

2810161412221825240156243
EdgewAction
(0,5)10Add to tree
(2,3)12Add to tree
(1,6)14Add to tree
(1,2)16Add to tree
(3,6)18Cycle, skip
(3,4)22
(4,6)24
(4,5)25
(0,1)28

(3,6) = 18: both ends in the same piece → it would form a cycle; discarded.

2810161412221825240156243
EdgewAction
(0,5)10Add to tree
(2,3)12Add to tree
(1,6)14Add to tree
(1,2)16Add to tree
(3,6)18Cycle, skip
(3,4)22Add to tree
(4,6)24
(4,5)25
(0,1)28

(3,4) = 22: ends in different pieces → added. Total 74.

2810161412221825240156243
EdgewAction
(0,5)10Add to tree
(2,3)12Add to tree
(1,6)14Add to tree
(1,2)16Add to tree
(3,6)18Cycle, skip
(3,4)22Add to tree
(4,6)24Cycle, skip
(4,5)25
(0,1)28

(4,6) = 24: both ends in the same piece → it would form a cycle; discarded.

2810161412221825240156243
EdgewAction
(0,5)10Add to tree
(2,3)12Add to tree
(1,6)14Add to tree
(1,2)16Add to tree
(3,6)18Cycle, skip
(3,4)22Add to tree
(4,6)24Cycle, skip
(4,5)25Add to tree
(0,1)28

(4,5) = 25: ends in different pieces → added. Total 99. n − 1 = 6 edges chosen: done (28 is never examined).

Kruskal

How Not to Code Kruskal

  • Observation: first there were 2 components, then 3, then back to 2. “Got it! I'll keep the chosen edges in adjacency lists.”
  • The unchosen edges sit in an array. Bad: each time, search for the minimum, delete it, shift the rest. Better: keep the edges sorted by cost (end1, end2, cost).
  • (0,5) arrives: search the lists, neither 0 nor 5 is there → new list. (3,2) and (6,1) also start new lists. (1,2): in different lists → no cycle, merge the two lists.
  • (3,6): both in the same list → cycle! It works, but it pays a search cost every time.
From the lecture: “We detected the cycle, but I am describing the wrong algorithm. You don't code it like this, or you won't get the points. I say ‘don't code it like this’, yet it works correctly; that's what I'm trying to show.”
From the lecture: “I may give coding Kruskal's algorithm as homework. Let's see whether you can discover the data structures it needs. I'm curious about that.”

Kruskal

How to Code Kruskal: Two Questions, Two Data Structures

1) Which edge is cheapest?

A min priority queue (min heap). Inserting edges one by one is O(e log e); turning the array into a heap bottom-up is O(e). Or sort the edges once (qsort).

2) Does it form a cycle?

No cycle if the ends belong to two different sets: union-find. Do find(3) and find(2) return the same root?

From the lecture: “So the instructor had taught us these data structures all along and we never noticed!! And there we were, keeping lists… Look how useful the sets we learned in primary school turn out to be.”
Cost: |E|·lg|E| + |V|·(lg|E| + Θ(1)); with a bottom-up heap, 2|E| + |V|·lg|E| (Θ(1): inverse Ackermann, α ≤ lg|E|). In the worst case the cycle-forming edges may force e deleteMins rather than n − 1; a safe upper bound is O(e log e) = O(e log n).

Kruskal

C: Sets (Union-Find) and the Comparator

typedef struct { int u, v, w; } edge;

int find(int parent[], int i)    /* root: < 0 */
{
    while (parent[i] >= 0) i = parent[i];
    return i;
}
void unite(int parent[], int a, int b)   /* roots */
{
    if (parent[a] <= parent[b]) {  /* a is larger */
        parent[a] += parent[b];  parent[b] = a;
    } else {
        parent[b] += parent[a];  parent[a] = b;
    }
}
int cmp(const void *x, const void *y)
{
    return ((const edge *)x)->w - ((const edge *)y)->w;
}
  • parent[i] < 0: i is a root and −parent[i] is the set size (initially all −1).
  • Weighted union: the smaller set goes under the larger → tree height O(log n), find O(log n).
  • Add path compression and the amortized cost per operation is ≈ O(α(n)), constant in practice.
  • cmp: compares costs for qsort (small integers, so no overflow).

Kruskal + union-find · Step through the code

Code Trace: the kruskal() main loop

2810161412221825240156243parent0-11-12-13-14-15-16-1edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288

Every vertex is the root of its own set (parent = −1). The edges are sorted by cost: O(e log e).

2810161412221825240156243parent0-11-12-13-14-15-16-1edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288▲ i

i = 0: (0,5) = 10. find: 0 → root 0, 5 → root 5.

2810161412221825240156243parent0-21-12-13-14-1506-1edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288▲ i

0 ≠ 5 → no cycle: added to T and the sets are united (the larger set's root stays). t = 1.

2810161412221825240156243parent0-21-12-13-14-1506-1edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288▲ i

i = 1: (2,3) = 12. find: 2 → root 2, 3 → root 3.

2810161412221825240156243parent0-21-12-2324-1506-1edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288▲ i

2 ≠ 3 → no cycle: added to T and the sets are united (the larger set's root stays). t = 2.

2810161412221825240156243parent0-21-12-2324-1506-1edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288▲ i

i = 2: (1,6) = 14. find: 1 → root 1, 6 → root 6.

2810161412221825240156243parent0-21-22-2324-15061edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288▲ i

1 ≠ 6 → no cycle: added to T and the sets are united (the larger set's root stays). t = 3.

2810161412221825240156243parent0-21-22-2324-15061edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288▲ i

i = 3: (1,2) = 16. find: 1 → root 1, 2 → root 2.

2810161412221825240156243parent0-21-421324-15061edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288▲ i

1 ≠ 2 → no cycle: added to T and the sets are united (the larger set's root stays). t = 4.

for (i = 0; i < n; i++) parent[i] = -1;
qsort(edges, e, sizeof(edge), cmp);
for (i = 0, t = 0; i < e && t < n - 1; i++) {
    int a = find(parent, edges[i].u);
    int b = find(parent, edges[i].v);
    if (a != b) {                 /* no cycle */
        T[t++] = edges[i];
        unite(parent, a, b);
    }                             /* else skip */
}
if (t < n - 1) printf("no spanning tree\n");

Kruskal + union-find · Step through the code

Code Trace: kruskal() (continued)

2810161412221825240156243parent0-21-421324-15061edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288▲ i

i = 4: (3,6) = 18. find: 3 → root 1, 6 → root 1.

2810161412221825240156243parent0-21-421324-15061edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288▲ i

a = b = 1: both ends are in the same set → the edge forms a cycle; discarded.

2810161412221825240156243parent0-21-421324-15061edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288▲ i

i = 5: (3,4) = 22. find: 3 → root 1, 4 → root 4.

2810161412221825240156243parent0-21-52132415061edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288▲ i

1 ≠ 4 → no cycle: added to T and the sets are united (the larger set's root stays). t = 5.

2810161412221825240156243parent0-21-52132415061edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288▲ i

i = 6: (4,6) = 24. find: 4 → root 1, 6 → root 1.

2810161412221825240156243parent0-21-52132415061edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288▲ i

a = b = 1: both ends are in the same set → the edge forms a cycle; discarded.

2810161412221825240156243parent0-21-52132415061edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288▲ i

i = 7: (4,5) = 25. find: 4 → root 1, 5 → root 0.

2810161412221825240156243parent011-72132415061edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288▲ i

1 ≠ 0 → no cycle: added to T and the sets are united (the larger set's root stays). t = 6.

2810161412221825240156243parent011-72132415061edgessorted0-51002-31211-61421-21633-61843-42254-62464-52570-1288

t = n − 1 = 6 → the loop ends; (0,1) = 28 is never examined. MST cost 10 + 12 + 14 + 16 + 22 + 25 = 99.

for (i = 0; i < n; i++) parent[i] = -1;
qsort(edges, e, sizeof(edge), cmp);
for (i = 0, t = 0; i < e && t < n - 1; i++) {
    int a = find(parent, edges[i].u);
    int b = find(parent, edges[i].v);
    if (a != b) {                 /* no cycle */
        T[t++] = edges[i];
        unite(parent, a, b);
    }                             /* else skip */
}
if (t < n - 1) printf("no spanning tree\n");

Kruskal

Complexity of Kruskal (with Union-Find)

StepCostNotes
Sort / build heapO(e log e) / O(e)qsort or bottom-up heap construction
Take the cheapest edgeO(log e) × at most e timesmin heap deleteMin (O(1) in a sorted array)
find (2 per edge)O(log n) weighted; ≈ O(α(n)) with path compressionat most 2e calls
uniteO(1)exactly n − 1 times
TotalO(e log e) = O(e log n)e ≤ n² → log e ≤ 2 log n
The “bad” version (search and merge lists) spends an O(n) search per edge: O(e · n). The choice of data structure moves the same algorithm to a completely different cost.

Prim

Prim's Algorithm

T  = {};
TV = {0};           /* tree vertices: start at 0 */
while (|T| < n-1) {
    pick a least-cost (u,v) with u ∈ TV, v ∉ TV;
    if (there is no such edge) break;
    TV = TV ∪ {v};
    T  = T  ∪ {(u,v)};
}
if (|T| < n-1)
    printf("no spanning tree\n");
  • Jarník (1930), Prim (1957), Dijkstra (1959) → the DJP or Prim–Jarník algorithm.
  • Kruskal may choose disconnected edges; Prim grows the chosen edges as a single tree.
  • Candidate edges: one end in TV, the other end not in TV.
  • A single tree keeps growing; no cycle check is needed (v ∉ TV already guarantees it).

Prim

Prim Step by Step

2810161412221825240156243
TVTCandidates
{0}{}(0,1), (0,5)

Candidate edges in yellow. The cheapest is (0,5) = 10 → 5 joins the tree.

2810161412221825240156243
TVTCandidates
{0, 5}{(0,5)}(0,1), (5,4)

Candidate edges in yellow. The cheapest is (5,4) = 25 → 4 joins the tree.

2810161412221825240156243
TVTCandidates
{0, 5, 4}{(0,5), (5,4)}(0,1), (4,3), (4,6)

Candidate edges in yellow. The cheapest is (4,3) = 22 → 3 joins the tree.

2810161412221825240156243
TVTCandidates
{0, 5, 4, 3}{(0,5), (5,4), (4,3)}(0,1), (3,2), (3,6), (4,6)

Candidate edges in yellow. The cheapest is (3,2) = 12 → 2 joins the tree.

2810161412221825240156243
TVTCandidates
{0, 5, 4, 3, 2}{(0,5), (5,4), (4,3), (3,2)}(0,1), (2,1), (3,6), (4,6)

Candidate edges in yellow. The cheapest is (2,1) = 16 → 1 joins the tree.

2810161412221825240156243
TVTCandidates
{0, 5, 4, 3, 2, 1}{(0,5), (5,4), (4,3), (3,2), (2,1)}(1,6), (3,6), (4,6)

Candidate edges in yellow. The cheapest is (1,6) = 14 → 6 joins the tree.

2810161412221825240156243
TVTCandidates
{0, 5, 4, 3, 2, 1, 6}{(0,5), (5,4), (4,3), (3,2), (2,1), (1,6)}—

TV holds every vertex: 6 edges, total 99, the same tree as Kruskal (the weights are distinct, so the MST is unique).

In the last steps (0,1) may look like a candidate, but both its ends are in TV, so it is not.

Prim

Coding Prim: the Real Difficulty

  • The cheapest candidate edge must be found in one move.
  • When an edge is chosen, new edges enter the candidate set and some leave it (those with both ends now in TV): set A becomes set B.
  • When 0 is chosen, the data structure should see only (0,1) and (0,5) and not even look at the rest.
  • Solution (extra): for each v ∉ TV, lowcost[v] = cheapest link to TV. O(n²) with arrays; O(e log n) with a min heap.
From the lecture: “If you can find the answer to this, data structures have taken you somewhere… Say ‘Prim already wrote it, I'll just code it’ and the complexity suddenly blows up.”
From the lecture: “Give this problem to a graph theorist who has never heard the word ‘algorithm’ and this is how they will solve it. A computer engineer discovers what the data structure behind it must be.”

Prim, O(n²) arrays · Step through the code

Code Trace: prim() (lowcost/closest arrays)

2810161412221825240156243in_tree01102030405060lowcost028∞∞∞10∞closest0000000

TV = {0}. lowcost[v] = w[0][v]: 1 (28) and 5 (10) are adjacent to 0; the rest are ∞. closest[v] = 0.

2810161412221825240156243in_tree01102030405160lowcost028∞∞∞10∞closest0000000

Smallest lowcost: u = 5 (10); edge (0,5) enters the tree. total = 10.

2810161412221825240156243in_tree01102030405160lowcost028∞∞2510∞closest0000500

Cheaper links through 5: lowcost[4] = 25 (closest = 5).

2810161412221825240156243in_tree01102030415160lowcost028∞∞2510∞closest0000500

Smallest lowcost: u = 4 (25); edge (5,4) enters the tree. total = 35.

2810161412221825240156243in_tree01102030415160lowcost028∞22251024closest0004504

Cheaper links through 4: lowcost[3] = 22, lowcost[6] = 24 (closest = 4).

2810161412221825240156243in_tree01102031415160lowcost028∞22251024closest0004504

Smallest lowcost: u = 3 (22); edge (4,3) enters the tree. total = 57.

2810161412221825240156243in_tree01102031415160lowcost0281222251018closest0034503

Cheaper links through 3: lowcost[2] = 12, lowcost[6] = 18 (closest = 3).

2810161412221825240156243in_tree01102131415160lowcost0281222251018closest0034503

Smallest lowcost: u = 2 (12); edge (3,2) enters the tree. total = 69.

2810161412221825240156243in_tree01102131415160lowcost0161222251018closest0234503

Cheaper links through 2: lowcost[1] = 16 (closest = 2).

2810161412221825240156243in_tree01112131415160lowcost0161222251018closest0234503

Smallest lowcost: u = 1 (16); edge (2,1) enters the tree. total = 85.

2810161412221825240156243in_tree01112131415160lowcost0161222251014closest0234501

Cheaper links through 1: lowcost[6] = 14 (closest = 1).

2810161412221825240156243in_tree01112131415161lowcost0161222251014closest0234501

Smallest lowcost: u = 6 (14); edge (1,6) enters the tree. total = 99.

2810161412221825240156243in_tree01112131415161lowcost0161222251014closest0234501

n − 1 vertices added: total 99. Each round is an O(n) search + O(n) update → O(n²); ideal for dense graphs.

in_tree[0] = 1;                    /* TV = {0} */
for (v = 0; v < n; v++)
    lowcost[v] = w[0][v], closest[v] = 0;
for (k = 1; k < n; k++) {
    u = cheapest(lowcost, in_tree, n); /* O(n) */
    if (u < 0) break;             /* no tree */
    in_tree[u] = 1;  total += lowcost[u];
    for (v = 0; v < n; v++)
        if (!in_tree[v] && w[u][v] < lowcost[v])
            lowcost[v] = w[u][v], closest[v] = u;
}

Prim

Speeding Up Prim with a Heap (extra topic)

/* adjacency lists + min-heap (key = lowcost) */
heap_push(H, 0, 0);
while (!heap_empty(H)) {
    u = heap_pop_min(H);         /* O(log n) */
    if (in_tree[u]) continue;    /* stale entry */
    in_tree[u] = 1;
    for (p = graph[u]; p; p = p->link)
        if (!in_tree[p->vertex] &&
            p->weight < lowcost[p->vertex]) {
            lowcost[p->vertex] = p->weight;
            closest[p->vertex] = u;
            heap_push(H, p->vertex, p->weight);
        }
}
ImplementationTimeBest for
Arrays (above)O(n²)dense graphs (e ≈ n²)
Binary heap + listsO(e log n)sparse graphs
Fibonacci heapO(e + n log n)theoretical
KruskalO(e log e)sparse, edge list at hand
Answer to the research question: array-based Prim (O(n²)) wins on dense graphs; Kruskal or heap-based Prim wins on sparse ones.

Prim

How to Approach a Problem

From the lecture: “How has this problem been solved? Which data structures does the literature propose? How does the world solve it, and how should I?”
From the lecture: “‘We have mastered the algorithm.’ No! We are still working on it. Knowing what to use where while solving a problem: that is where data structures come in.”

“To follow the path: look to the master, follow the master, walk with the master, see through the master, become the master.”

First learn the master's solution (the literature), then produce your own.

Prim

Homework: Code Kruskal with the Right Data Structures

“By next week everyone codes Kruskal's algorithm the way I described it.” Expected: min heap + union-find; input an edge list, output the tree edges and the total cost.

  • Edge structure edge {u, v, w}; build the heap bottom-up from the array (O(e)).
  • Add path compression to find; make unite weighted.
  • If the graph is not connected, print “no spanning tree” (|T| < n − 1).
  • On the 7-village example, print output showing the total 99 and the rejection of (3,6) and (4,6).
Checklist
  1. Stop as soon as t = n − 1, not after e heap removals.
  2. Check every malloc for NULL and free at the end.
  3. With equal weights the MST may not be unique; the total cost is still the same.

Prim

Other MST Algorithms: Sollin (Borůvka), Reverse-Delete

2810161412221825240156243

Initially every vertex is a component of its own.

2810161412221825240156243

Round 1: each component picks its cheapest outgoing edge (yellow): (0,5)=10, (1,6)=14, (2,3)=12, (3,4)=22. Components left: 3.

2810161412221825240156243

Round 2: each component picks its cheapest outgoing edge (yellow): (4,5)=25, (1,2)=16. Components left: 1. Total 99.

  • Sollin / Borůvka (1926): in each round every component picks its cheapest outgoing edge; the number of components at least halves → at most log n rounds, O(e log n). Well suited to parallelism.
  • Reverse-delete: try the edges in decreasing cost; delete an edge if doing so keeps the graph connected. The reverse of Kruskal.
  • All rely on the same cut/cycle properties and (with distinct weights) find the same tree.

Prim

An MST Is Not a Shortest-Path Tree

28101614122218252401562432810161412221825240156243MST (total 99)shortest-path tree from 0 (total 105)
MST

Minimizes the total edge cost and has no root. Distance from 0 to 1 along the tree: 85.

Shortest-path tree

Minimizes each vertex's distance from 0: d(1) = 28, but the total edge cost is 105 > 99.

Prim

Kruskal and Prim Compared

KruskalPrim
What growsa forest (separate pieces merge)a single tree (TV)
Selection rulecheapest of all edgescheapest edge leaving TV
Cycle checkneeds union-findnot needed (v ∉ TV)
Data structuresorted array / min heap + setslowcost[] array / min heap
TimeO(e log e)O(n²) or O(e log n)
Good forsparse graphs, edge listsdense graphs, matrices
Disconnected graphgives a minimum spanning forestspans only 0's component

Prim

Exam-Style Question: Run Kruskal and Prim

436527189ABCDEF

Coding Kruskal was homework; the question below is the kind that may appear on an exam.

  1. Find the MST with Kruskal; for each edge write “add / cycle”.
  2. Run Prim from A and list the edges in the order chosen.
  3. Give the final union-find parent[] array (weighted union).
Solution

Kruskal: (C,F)=1 add, (B,E)=2 add, (A,D)=3 add, (A,B)=4 add, (B,D)=5 cycle, (B,C)=6 add. Total 16.

Prim (A): (A,D)=3 → (A,B)=4 → (B,E)=2 → (B,C)=6 → (C,F)=1. Total 16, the same tree.

parent[] (A..F = 0..5, edges (u,v) in order, unite(find(u), find(v))): [-6, 0, 0, 0, 1, 2]

Lab · C code step by step

Lab: Kruskal and Prim

Summary

Complexity Summary

AlgorithmStructureTimeNote
Try every subset—C(e, n−1) × O(n)exponential, impractical
Kruskal (bad version)lists + searchO(e · n)“don't code it this way”
Kruskalsorting/min heap + union-findO(e log e)a forest grows and merges
Primarrays (lowcost, closest)O(n²)dense graphs
Primbinary heap + adjacency listsO(e log n)sparse graphs
DFS/BFS spanning treestack / queueO(n + e)unweighted

Summary

Common Mistakes

1Checking for a cycle in Kruskal by asking “were both ends chosen before?” (wrong: they may be in different pieces).
2In Prim, not dropping candidate edges whose ends are both in TV.
3Confusing an MST with a shortest-path tree: the MST minimizes the total cost, shortest paths minimize individual distances.
4Forgetting the |T| < n − 1 check for a disconnected graph.
5Uniting non-root vertices in union-find (call find first!).
6Using INT_MAX for ∞ and overflowing when adding.

Summary

Exercises

  1. In the 7-village example, what would the MST and its cost be if edge (3,6) cost 11 instead of 18?
  2. When can Kruskal and Prim give different trees on the same graph?
  3. On the 8-vertex sample graph, list the edges of the DFS and BFS spanning trees starting from 7.
  4. How many spanning trees does K4 have? How many are paths, how many stars?
  5. If the edge weights are integers in 1..10, how does Kruskal's sorting step drop to O(e)?
  6. Why is the O(n²) version of Prim poor on sparse graphs?
Answers / hints
  1. Order: (0,5)=10 add, (3,6)=11 add, (2,3)=12 add, (1,6)=14 add, (1,2)=16 cycle, (3,4)=22 add, (4,6)=24 cycle, (4,5)=25 add → MST {(0,5),(3,6),(2,3),(1,6),(3,4),(4,5)}, cost 94.
  2. Only when some edges have equal weights; the total cost is still the same.
  3. With sorted lists, DFS(7): (7,3),(3,1),(1,0),(0,2),(2,5),(2,6),(1,4); BFS(7): (7,3),(7,4),(7,5),(7,6),(3,1),(5,2),(1,0).
  4. 16: 12 paths + 4 stars.
  5. With counting (bucket) sort: O(e + 10).
  6. Every round scans all n vertices: O(n²) ≫ O(e log n) (when e ≈ n).

Summary

Chapter Summary

Spanning tree

An acyclic subgraph with n − 1 edges that connects every vertex; DFS and BFS each produce one.

Minimum cost

The spanning tree of least total weight; the greedy choice is correct (cut property).

Kruskal

Try edges in increasing cost; catch cycles with union-find: O(e log e).

Prim

One tree grows by its cheapest edge: O(n²) with arrays, O(e log n) with a heap.

Data structure

Same idea, different structure: list search O(e·n) → sets O(e log e).

Choice

Sparse graph: Kruskal or heap-based Prim; dense graph: array-based Prim.

Summary

References

  • E. Horowitz, S. Sahni, S. Anderson-Freed, Fundamentals of Data Structures in C, 2nd ed., §6.3 Minimum Cost Spanning Trees.
  • T. H. Cormen et al., Introduction to Algorithms, 4th ed., Chapter 19 (Disjoint Sets) and Chapter 21 (Minimum Spanning Trees).
  • R. Sedgewick, Algorithms in C, Part 5, Chapter 20 (Minimum Spanning Trees).

Related: Graphs · Sets and Union-Find

The End

Chapter 17: Minimum Spanning Trees
Next chapter: Chapter 18: Shortest Paths and Activity Networks · Course page
1 / 1 Sercan KÜLCÜ. All rights reserved.