Chapter 17: Minimum Spanning Trees
Introduction
Learning Outcomes
Explain spanning trees, tree and nontree edges, and depth-first and breadth-first spanning trees.
Discuss ways to generate the spanning trees of a graph, and the idea of isomorphism.
Trace Kruskal's algorithm in C: edges in increasing cost, cycles caught with union-find.
Implement Prim's algorithm, which grows a single tree, with O(n²) arrays and with a heap.
Compare the O(e log e), O(n²) and O(e log n) bounds and choose by graph density.
“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:
Edges that were followed to a new vertex (thick red). There are exactly n − 1 of them.
Edges never followed (dashed). In DFS they are back edges, in BFS cross edges.
DFS: N = {(0,2), (1,4), (6,7)} · BFS: N = {(4,7), (5,7), (6,7)}
Spanning Trees
Definition: Spanning Tree
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.
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
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.
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.
First find one spanning tree (e.g. with DFS).
Add a nontree edge: exactly one cycle appears.
Remove (A,B) from the cycle → a different spanning tree. Put it back and remove the next one.
Remove (B,C) from the cycle → a different spanning tree. Put it back and remove the next one.
Remove (C,D) from the cycle → a different spanning tree. Put it back and remove the next one.
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
BFS spanning tree: 8 vertices, 7 edges (thick red); the 3 nontree edges are dashed.
Adding the nontree edge (6,7) creates exactly one cycle: 6-2-0-1-3-7-6. It is no longer a tree.
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).
Sample graph
Minimum Cost
Algorithms and the Greedy Approach
Kruskal, Prim, Sollin (Borůvka), and also reverse-delete. This chapter covers the first two.
Backtracking, greedy, dynamic programming…
Each step makes the best choice for the moment and never revises it; there is no going back.
1) Never take an edge that forms a cycle. 2) Prefer the cheapest edge. 3) Choose exactly n − 1 edges for n vertices.
“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
| Edge | w | Action |
|---|---|---|
| (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.
| Edge | w | Action |
|---|---|---|
| (0,5) | 10 | Add 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.
| Edge | w | Action |
|---|---|---|
| (0,5) | 10 | Add to tree |
| (2,3) | 12 | Add 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.
| Edge | w | Action |
|---|---|---|
| (0,5) | 10 | Add to tree |
| (2,3) | 12 | Add to tree |
| (1,6) | 14 | Add 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.
| Edge | w | Action |
|---|---|---|
| (0,5) | 10 | Add to tree |
| (2,3) | 12 | Add to tree |
| (1,6) | 14 | Add to tree |
| (1,2) | 16 | Add 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.
| Edge | w | Action |
|---|---|---|
| (0,5) | 10 | Add to tree |
| (2,3) | 12 | Add to tree |
| (1,6) | 14 | Add to tree |
| (1,2) | 16 | Add to tree |
| (3,6) | 18 | Cycle, 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.
| Edge | w | Action |
|---|---|---|
| (0,5) | 10 | Add to tree |
| (2,3) | 12 | Add to tree |
| (1,6) | 14 | Add to tree |
| (1,2) | 16 | Add to tree |
| (3,6) | 18 | Cycle, skip |
| (3,4) | 22 | Add to tree |
| (4,6) | 24 | |
| (4,5) | 25 | |
| (0,1) | 28 |
(3,4) = 22: ends in different pieces → added. Total 74.
| Edge | w | Action |
|---|---|---|
| (0,5) | 10 | Add to tree |
| (2,3) | 12 | Add to tree |
| (1,6) | 14 | Add to tree |
| (1,2) | 16 | Add to tree |
| (3,6) | 18 | Cycle, skip |
| (3,4) | 22 | Add to tree |
| (4,6) | 24 | Cycle, skip |
| (4,5) | 25 | |
| (0,1) | 28 |
(4,6) = 24: both ends in the same piece → it would form a cycle; discarded.
| Edge | w | Action |
|---|---|---|
| (0,5) | 10 | Add to tree |
| (2,3) | 12 | Add to tree |
| (1,6) | 14 | Add to tree |
| (1,2) | 16 | Add to tree |
| (3,6) | 18 | Cycle, skip |
| (3,4) | 22 | Add to tree |
| (4,6) | 24 | Cycle, skip |
| (4,5) | 25 | Add 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.
Kruskal
How to Code Kruskal: Two Questions, Two Data Structures
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).
No cycle if the ends belong to two different sets: union-find. Do find(3) and find(2) return the same root?
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),
findO(log n). - Add path compression and the amortized cost per operation is ≈ O(α(n)), constant in practice.
cmp: compares costs forqsort(small integers, so no overflow).
Kruskal + union-find · Step through the code
Code Trace: the kruskal() main loop
Every vertex is the root of its own set (parent = −1). The edges are sorted by cost: O(e log e).
i = 0: (0,5) = 10. find: 0 → root 0, 5 → root 5.
0 ≠ 5 → no cycle: added to T and the sets are united (the larger set's root stays). t = 1.
i = 1: (2,3) = 12. find: 2 → root 2, 3 → root 3.
2 ≠ 3 → no cycle: added to T and the sets are united (the larger set's root stays). t = 2.
i = 2: (1,6) = 14. find: 1 → root 1, 6 → root 6.
1 ≠ 6 → no cycle: added to T and the sets are united (the larger set's root stays). t = 3.
i = 3: (1,2) = 16. find: 1 → root 1, 2 → root 2.
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)
i = 4: (3,6) = 18. find: 3 → root 1, 6 → root 1.
a = b = 1: both ends are in the same set → the edge forms a cycle; discarded.
i = 5: (3,4) = 22. find: 3 → root 1, 4 → root 4.
1 ≠ 4 → no cycle: added to T and the sets are united (the larger set's root stays). t = 5.
i = 6: (4,6) = 24. find: 4 → root 1, 6 → root 1.
a = b = 1: both ends are in the same set → the edge forms a cycle; discarded.
i = 7: (4,5) = 25. find: 4 → root 1, 5 → root 0.
1 ≠ 0 → no cycle: added to T and the sets are united (the larger set's root stays). t = 6.
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)
| Step | Cost | Notes |
|---|---|---|
| Sort / build heap | O(e log e) / O(e) | qsort or bottom-up heap construction |
| Take the cheapest edge | O(log e) × at most e times | min heap deleteMin (O(1) in a sorted array) |
| find (2 per edge) | O(log n) weighted; ≈ O(α(n)) with path compression | at most 2e calls |
| unite | O(1) | exactly n − 1 times |
| Total | O(e log e) = O(e log n) | e ≤ n² → log e ≤ 2 log n |
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
| TV | T | Candidates |
|---|---|---|
| {0} | {} | (0,1), (0,5) |
Candidate edges in yellow. The cheapest is (0,5) = 10 → 5 joins the tree.
| TV | T | Candidates |
|---|---|---|
| {0, 5} | {(0,5)} | (0,1), (5,4) |
Candidate edges in yellow. The cheapest is (5,4) = 25 → 4 joins the tree.
| TV | T | Candidates |
|---|---|---|
| {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.
| TV | T | Candidates |
|---|---|---|
| {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.
| TV | T | Candidates |
|---|---|---|
| {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.
| TV | T | Candidates |
|---|---|---|
| {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.
| TV | T | Candidates |
|---|---|---|
| {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.
Prim, O(n²) arrays · Step through the code
Code Trace: prim() (lowcost/closest arrays)
TV = {0}. lowcost[v] = w[0][v]: 1 (28) and 5 (10) are adjacent to 0; the rest are ∞. closest[v] = 0.
Smallest lowcost: u = 5 (10); edge (0,5) enters the tree. total = 10.
Cheaper links through 5: lowcost[4] = 25 (closest = 5).
Smallest lowcost: u = 4 (25); edge (5,4) enters the tree. total = 35.
Cheaper links through 4: lowcost[3] = 22, lowcost[6] = 24 (closest = 4).
Smallest lowcost: u = 3 (22); edge (4,3) enters the tree. total = 57.
Cheaper links through 3: lowcost[2] = 12, lowcost[6] = 18 (closest = 3).
Smallest lowcost: u = 2 (12); edge (3,2) enters the tree. total = 69.
Cheaper links through 2: lowcost[1] = 16 (closest = 2).
Smallest lowcost: u = 1 (16); edge (2,1) enters the tree. total = 85.
Cheaper links through 1: lowcost[6] = 14 (closest = 1).
Smallest lowcost: u = 6 (14); edge (1,6) enters the tree. total = 99.
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); } }
| Implementation | Time | Best for |
|---|---|---|
| Arrays (above) | O(n²) | dense graphs (e ≈ n²) |
| Binary heap + lists | O(e log n) | sparse graphs |
| Fibonacci heap | O(e + n log n) | theoretical |
| Kruskal | O(e log e) | sparse, edge list at hand |
Prim
How to Approach a Problem
“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; makeuniteweighted. - 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
- Stop as soon as t = n − 1, not after e heap removals.
- Check every
mallocfor NULL andfreeat the end. - 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
Initially every vertex is a component of its own.
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.
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
Minimizes the total edge cost and has no root. Distance from 0 to 1 along the tree: 85.
Minimizes each vertex's distance from 0: d(1) = 28, but the total edge cost is 105 > 99.
Prim
Kruskal and Prim Compared
| Kruskal | Prim | |
|---|---|---|
| What grows | a forest (separate pieces merge) | a single tree (TV) |
| Selection rule | cheapest of all edges | cheapest edge leaving TV |
| Cycle check | needs union-find | not needed (v ∉ TV) |
| Data structure | sorted array / min heap + sets | lowcost[] array / min heap |
| Time | O(e log e) | O(n²) or O(e log n) |
| Good for | sparse graphs, edge lists | dense graphs, matrices |
| Disconnected graph | gives a minimum spanning forest | spans only 0's component |
Prim
Exam-Style Question: Run Kruskal and Prim
Coding Kruskal was homework; the question below is the kind that may appear on an exam.
- Find the MST with Kruskal; for each edge write “add / cycle”.
- Run Prim from A and list the edges in the order chosen.
- 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
| Algorithm | Structure | Time | Note |
|---|---|---|---|
| Try every subset | — | C(e, n−1) × O(n) | exponential, impractical |
| Kruskal (bad version) | lists + search | O(e · n) | “don't code it this way” |
| Kruskal | sorting/min heap + union-find | O(e log e) | a forest grows and merges |
| Prim | arrays (lowcost, closest) | O(n²) | dense graphs |
| Prim | binary heap + adjacency lists | O(e log n) | sparse graphs |
| DFS/BFS spanning tree | stack / queue | O(n + e) | unweighted |
Summary
Common Mistakes
find first!).Summary
Exercises
- In the 7-village example, what would the MST and its cost be if edge (3,6) cost 11 instead of 18?
- When can Kruskal and Prim give different trees on the same graph?
- On the 8-vertex sample graph, list the edges of the DFS and BFS spanning trees starting from 7.
- How many spanning trees does K4 have? How many are paths, how many stars?
- If the edge weights are integers in 1..10, how does Kruskal's sorting step drop to O(e)?
- Why is the O(n²) version of Prim poor on sparse graphs?
Answers / hints
- 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.
- Only when some edges have equal weights; the total cost is still the same.
- 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).
- 16: 12 paths + 4 stars.
- With counting (bucket) sort: O(e + 10).
- Every round scans all n vertices: O(n²) ≫ O(e log n) (when e ≈ n).
Summary
Chapter Summary
An acyclic subgraph with n − 1 edges that connects every vertex; DFS and BFS each produce one.
The spanning tree of least total weight; the greedy choice is correct (cut property).
Try edges in increasing cost; catch cycles with union-find: O(e log e).
One tree grows by its cheapest edge: O(n²) with arrays, O(e log n) with a heap.
Same idea, different structure: list search O(e·n) → sets O(e log e).
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