Minimum Spanning Trees
Suppose a town wants to connect seven buildings with fibre-optic cable so every building is linked to every other, directly or indirectly, using as little cable as possible. That is the minimum spanning tree problem. Two short, reliable algorithms solve it, Kruskal’s and Prim’s, and you’ll be expected to carry out both and show each choice.
Key ideas
Section titled “Key ideas”Spanning trees
Section titled “Spanning trees”Recall that a tree is a connected graph with no cycles. A spanning tree of a connected graph is a subgraph that is a tree and includes every vertex of . If has vertices, every spanning tree has exactly edges.
In a weighted graph, a minimum spanning tree (MST) is a spanning tree whose total weight is as small as possible. It never contains a cycle, because you could always drop the heaviest edge of a cycle and stay connected.
Kruskal’s algorithm
Section titled “Kruskal’s algorithm”- List the edges in order of weight, smallest first.
- Go down the list. Add each edge unless it would make a cycle with the edges already chosen; if it would, reject it.
- Stop when you have edges.
Kruskal’s picks the cheapest edges anywhere in the graph, so the chosen edges can be in separate pieces until they join up at the end.
Prim’s algorithm
Section titled “Prim’s algorithm”- Choose any starting vertex.
- Look at every edge joining a vertex already in the tree to a vertex not yet in the tree. Add the one with the smallest weight.
- Repeat until every vertex is in the tree.
Prim’s grows a single tree outward from the start, so it never needs a cycle check: an edge to a new vertex can’t make a cycle.
Prim’s algorithm on a table (the matrix method)
Section titled “Prim’s algorithm on a table (the matrix method)”When the graph is given as a weighted adjacency table, Prim’s can be done on the table itself:
- Choose a starting vertex. Cross out its row, and label its column with .
- In all the labelled columns, find the smallest entry that hasn’t been crossed out. Circle it. That edge joins the tree to the vertex of that entry’s row.
- Cross out that vertex’s row, and label its column with the next number.
- Repeat steps 2 and 3 until every column is labelled. The circled entries are the MST edges.
Crossing out rows stops you from choosing an edge to a vertex that’s already in the tree.
Do Kruskal’s and Prim’s give the same tree?
Section titled “Do Kruskal’s and Prim’s give the same tree?”Both algorithms always give a tree of the same minimum total weight. If all the edge weights are different, the MST is unique, so both find exactly the same tree. When some weights are equal, there may be several MSTs with the same total, and the two algorithms (or different starting vertices) might find different ones.
Worked examples
Section titled “Worked examples”Example 1: Kruskal’s algorithm
Section titled “Example 1: Kruskal’s algorithm”Use Kruskal’s algorithm to find a minimum spanning tree for the graph above. State its total weight.
Solution. There are vertices, so the tree needs edges. Work down the edges in order of weight:
| Edge | Weight | Decision |
|---|---|---|
| CF | 3 | Add |
| AD | 4 | Add |
| BE | 5 | Add |
| BD | 6 | Add |
| FG | 6 | Add |
| AB | 7 | Reject (makes cycle A–B–D–A) |
| EG | 7 | Add: that’s edges, so stop |
(BD and FG tie at , and AB and EG tie at ; you can take either first and get the same result.)
The MST is CF, AD, BE, BD, FG, EG, with total weight
Example 2: Prim’s algorithm
Section titled “Example 2: Prim’s algorithm”Use Prim’s algorithm, starting at A, on the same graph. Show the order in which edges are chosen.
Solution. At each step, list the edges from the tree to new vertices and pick the smallest.
| Step | Tree so far | Cheapest edges to new vertices | Chosen |
|---|---|---|---|
| 1 | A | AB 7, AD 4 | AD 4 |
| 2 | A, D | AB 7, BD 6, DE 10, DG 12 | BD 6 |
| 3 | A, D, B | BC 9, BE 5, DE 10, DG 12 | BE 5 |
| 4 | A, D, B, E | BC 9, CE 8, EF 11, EG 7, DG 12 | EG 7 |
| 5 | A, D, B, E, G | BC 9, CE 8, EF 11, FG 6 | FG 6 |
| 6 | A, D, B, E, G, F | BC 9, CE 8, CF 3 | CF 3 |
The edges in order are AD, BD, BE, EG, FG, CF, with total weight . It’s the same tree as Kruskal’s. That’s expected here: the tied weights never give a choice between two different trees, so this graph has only one MST.
Example 3: Prim’s matrix method
Section titled “Example 3: Prim’s matrix method”The table shows the lengths, in km, of possible pipelines between five sites. Use Prim’s algorithm, starting at P, to find the minimum total length of pipeline that connects all five sites.
| P | Q | R | S | T | |
|---|---|---|---|---|---|
| P | – | 8 | 5 | – | 9 |
| Q | 8 | – | 4 | 7 | – |
| R | 5 | 4 | – | 6 | 10 |
| S | – | 7 | 6 | – | 3 |
| T | 9 | – | 10 | 3 | – |
Solution. Follow the matrix method:
- Cross out row P and label column P with . Smallest entry left in column P: (row R). Circle PR .
- Cross out row R; label column R with . In columns P and R, the uncrossed entries are Q , T (column P) and Q , S , T (column R). Smallest: in row Q. Circle RQ .
- Cross out row Q; label column Q with . Uncrossed entries in columns P, R, Q: T ; S , T ; S . Smallest: in row S. Circle RS .
- Cross out row S; label column S with . Uncrossed entries: T , T , and T in column S. Smallest: . Circle ST .
Every column is now labelled. The MST is PR, RQ, RS, ST, with total length
Example 4: When there are ties
Section titled “Example 4: When there are ties”A graph has edges AB , BC , CA and CD . Find the weight of a minimum spanning tree. Is the MST unique?
Solution. Kruskal’s: take CD , then any two of the three edges of weight (the third would complete the triangle A–B–C). The total is .
The MST is not unique: three different trees (CD with AB and BC, CD with AB and CA, or CD with BC and CA) all have weight . Different algorithms or starting points might pick different ones, but the minimum total is always .
Common mistakes
Section titled “Common mistakes”Adding an edge that makes a cycle in Kruskal’s. Before you add each edge, check whether its two ends are already connected through edges you’ve chosen. If they are, reject it.
Choosing the cheapest edge from the newest vertex only in Prim’s. At each step, look at edges from every vertex already in the tree, not just the last one added. (In Example 2, step 4 compares edges from A, D, B and E.)
Stopping too early or too late. A spanning tree on vertices has exactly edges. Count them.
Reading the table the wrong way in the matrix method. Search the labelled columns, and the new vertex is the row of the circled entry. Cross out rows of vertices already in the tree so you never circle an edge back into the tree.
Not showing the order of choices. IB questions usually ask you to show the algorithm. List the edges in the order chosen (and rejections for Kruskal’s), not just the final tree.
Confusing an MST with a shortest route. The MST connects all vertices as cheaply as possible; it is not the shortest path between two particular vertices, and not a route you could drive in one go.
Practice
Section titled “Practice”1. (Warm-up) A graph has edges AB , AC , BC , BD , CD , CE and DE . Use Kruskal’s algorithm to find a minimum spanning tree and its weight.
Solution
In order: CD add, AB add, BC add, AC reject (cycle A–B–C–A), BD reject (cycle B–C–D–B), CE add. That’s edges for vertices, so stop.
MST: CD, AB, BC, CE, with weight .
2. (Warm-up) A connected graph has vertices and edges. How many edges are in a spanning tree? How many edges of the graph are not used?
Solution
A spanning tree on vertices has edges, so edges are not used.
3. (Warm-up) Explain why Prim’s algorithm never creates a cycle.
Solution
Each step adds an edge from a vertex already in the tree to a vertex not yet in the tree. A cycle needs an edge joining two vertices that are already connected, and Prim’s never adds one of those.
4. (Core) Use Prim’s algorithm, starting at A, to find a minimum spanning tree for the complete graph given by this table. Show the order in which edges are added.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | – | 14 | 9 | 17 | 12 |
| B | 14 | – | 11 | 8 | 15 |
| C | 9 | 11 | – | 13 | 10 |
| D | 17 | 8 | 13 | – | 16 |
| E | 12 | 15 | 10 | 16 | – |
Solution
- From A: the smallest entry in column A is (C). Add AC .
- From A, C: candidates B , D , E (column A) and B , D , E (column C). Add CE .
- From A, C, E: smallest is (C to B). Add CB .
- From A, C, E, B: smallest to D is (B to D). Add BD .
Order: AC, CE, CB, BD. Total weight .
5. (Core) Use Prim’s algorithm on the graph in the first figure, starting at G. List the edges in the order they’re added, and check the total.
Solution
- From G: GD , GE , GF . Add FG .
- From G, F: CF is smallest. Add CF .
- From G, F, C: GD , GE , EF , CE , BC . Add EG .
- From G, F, C, E: BE is smallest. Add BE .
- From G, F, C, E, B: BD , AB , DE , DG . Add BD .
- Last: AD (smaller than AB ). Add AD .
Order: FG, CF, EG, BE, BD, AD. Total , the same tree as Examples 1 and 2.
6. (Core) Six villages K, L, M, N, O, P are to be linked by fibre-optic cable. Possible cable routes, in km, are KL , KM , LM , LN , MN , MO , NO , NP and OP . Cable costs $15 000 per km to lay. Use Kruskal’s algorithm to find the minimum cost of linking all six villages.
Solution
In order: LM add, KL add, NO add, MN add, OP add. That’s edges for villages, so stop. (KM , NP , LN and MO would each make a cycle anyway.)
Total length: km.
Cost: , so the minimum cost is $315 000.
7. (Core) In the graph in the first figure, the edge DG must be included in the network (for example, a cable that already exists). Find the minimum total weight of a spanning tree that includes DG.
Solution
Start with DG already chosen, then apply Kruskal’s to the rest: CF add, AD add, BE add, BD add, FG add. Now all vertices are connected with edges (D and G were joined at the start, and FG links C and F to them), so stop. EG is no longer needed: it would make the cycle E–B–D–G–E.
Total: .
8. (Challenge) In Example 3, a sixth site U is added. The only possible pipelines to U are UP km and UT km. Find the new minimum total length, and explain why adding a site didn’t increase it.
Solution
Kruskal’s on the new graph: UP add, ST add, QR add, UT add, PR add. Now all sites are connected with edges, so stop. (RS would make the cycle R–S–T–U–P–R.)
New total: km, the same as before.
The new site gives a cheaper way to link the two halves of the network: P–U–T costs , and it replaces RS (also ). So the extra vertex adds two edges but lets you drop one, and the total stays at km.
9. (Challenge) In the graph in the first figure, the weight of edge CE is changed from to . For which values of is CE in every minimum spanning tree?
Solution
In the current MST, C and E are joined by the tree path C–F–G–E, with edges , and . If CE is added, it forms a cycle with this path. CE belongs in the MST exactly when it is cheaper than the heaviest edge on that cycle that it could replace, which is EG .
- If : swap EG for CE and the total drops, so CE is in every MST (the new total is ).
- If : CE and EG tie, so there are two MSTs, one with CE and one without.
- If : CE is not in the MST.
So CE is in every MST when .