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 .
Homework
Section titled “Homework”Try these independently after the practice questions. Show your setup and explain your reasoning. Use exact answers where possible, and check that your answers fit the question. Homework solutions are not provided on the published site.
In every question, show the order of your choices: for Kruskal’s, list each edge with “add” or “reject” (and the cycle it would make); for Prim’s, list the edges in the order they join the tree.
Foundations
Section titled “Foundations”H1. (Foundations) Answer each question and give a reason.
- (a) A connected graph has vertices and edges. How many edges does a spanning tree have, and how many edges of the graph are left out?
- (b) Can a graph with vertices and edges be connected?
- (c) A student says they found a minimum spanning tree with edges for a graph with vertices. Explain how you know they made a mistake.
H2. (Foundations) A weighted graph has vertices A to F and edges AB , AC , AF , BC , BD , CD , CE , DE , DF and EF . Use Kruskal’s algorithm to find a minimum spanning tree. List every edge you consider, say whether it is added or rejected, and give the total weight.
H3. (Core) A provincial park in Alberta plans to gravel some of its trails so that six sites are all linked: the parking lot P, a lookout L, a waterfall F, the campground C, a beach B and the ranger station R. The table gives the lengths, in metres, of the possible trails (hypothetical data; – means no trail).
| P | L | F | C | B | R | |
|---|---|---|---|---|---|---|
| P | – | 450 | – | 380 | – | 700 |
| L | 450 | – | 520 | – | 610 | – |
| F | – | 520 | – | 340 | – | 290 |
| C | 380 | – | 340 | – | 560 | 480 |
| B | – | 610 | – | 560 | – | 430 |
| R | 700 | – | 290 | 480 | 430 | – |
- (a) Use Prim’s algorithm, starting at P, to find which trails should be gravelled to use the least total length. State the order in which the sites join the network.
- (b) Find the total length of gravelled trail.
- (c) A hiker wants to walk from the lookout to the beach using only gravelled trails. How far is that walk? Use your answer to explain why a minimum spanning tree is not the same as a set of shortest routes.
H4. (Core) Six small communities in northern British Columbia, labelled G, H, J, K, M and N, are to be linked by fibre-optic cable. The possible cable routes, in km, are GH , GJ , GK , HJ , HM , JK , JM , JN , KN and MN . Laying cable costs $28 000 per km (hypothetical).
- (a) Use Kruskal’s algorithm to find the minimum total length of cable needed, and the cost.
- (b) The route GJ runs along a highway that is being rebuilt, so the regional district insists it is included. Find the minimum total length of a network that includes GJ, and the extra cost compared with part (a).
H5. (Core) (IB style) The table shows the weights of the edges of the complete graph on vertices A, B, C, D and E.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | – | 23 | 31 | 18 | 27 |
| B | 23 | – | 15 | 29 | 21 |
| C | 31 | 15 | – | 26 | 34 |
| D | 18 | 29 | 26 | – | 24 |
| E | 27 | 21 | 34 | 24 | – |
- (a) Use the matrix method for Prim’s algorithm, starting at C, to find a minimum spanning tree. Show the order in which the columns are labelled and the entries circled.
- (b) State the total weight of the tree.
- (c) Without running Kruskal’s algorithm, explain why it must give exactly the same tree.
H6. (Core) A graph has edges AB , AC , BC , BD , CD , CE , DE , EF and DF .
- (a) Use Kruskal’s algorithm to find a minimum spanning tree. When two edges have the same weight, consider them in alphabetical order. Give the total weight.
- (b) How many different minimum spanning trees does this graph have? List them, and explain how you know there are no others.
Challenge
Section titled “Challenge”H7. (Challenge) A student uses Prim’s algorithm, starting at A, on the graph given by this table.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | – | 4 | 6 | – | – |
| B | 4 | – | 8 | 9 | 12 |
| C | 6 | 8 | – | 5 | 10 |
| D | – | 9 | 5 | – | 7 |
| E | – | 12 | 10 | 7 | – |
Their work reads: “Start at A. Cheapest edge from A is AB . Cheapest edge from B is BC . Cheapest edge from C is CD . Cheapest edge from D is DE . Total .”
- (a) Is the student’s answer a spanning tree? Is it a minimum spanning tree?
- (b) Describe the mistake in their method.
- (c) Carry out Prim’s algorithm correctly from A and give the correct total weight.
H8. (Challenge) A graph has edges AB , AC , BD , CD , CE , DE , and an edge BC whose weight is , where .
- (a) Find the weight of a minimum spanning tree when .
- (b) Find the weight of a minimum spanning tree in terms of , for every value of .
- (c) For which value of does the graph have more than one minimum spanning tree? Explain.
H9. (Challenge) Explain each statement in a few sentences. (Hint: imagine an MST that breaks the rule, then swap one edge.)
- (a) If one edge of a connected graph is strictly lighter than every other edge, it belongs to every minimum spanning tree.
- (b) If a cycle in a connected graph has one edge that is strictly heavier than every other edge of that cycle, that edge belongs to no minimum spanning tree.
H10. (Challenge) Return to the network in H4 (without the highway condition). Before construction starts, the route JK is found to be impossible because of a protected wetland.
- (a) Find the new minimum spanning tree and its total length.
- (b) Explain why the new tree only needs to differ from the old one by a single edge, and how you can find that edge without rerunning the whole algorithm.
- (c) How much more does the network cost now?