Skip to content
Family Table Math
Auto

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.

Recall that a tree is a connected graph with no cycles. A spanning tree of a connected graph GG is a subgraph that is a tree and includes every vertex of GG. If GG has nn vertices, every spanning tree has exactly n−1n - 1 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.

  1. List the edges in order of weight, smallest first.
  2. Go down the list. Add each edge unless it would make a cycle with the edges already chosen; if it would, reject it.
  3. Stop when you have n−1n - 1 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.

  1. Choose any starting vertex.
  2. 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.
  3. 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:

  1. Choose a starting vertex. Cross out its row, and label its column with 11.
  2. 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.
  3. Cross out that vertex’s row, and label its column with the next number.
  4. 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.

A weighted graph with vertices A to G and edges AB 7, AD 4, BD 6, BC 9, BE 5, CE 8, CF 3, DE 10, DG 12, EG 7, EF 11, FG 6. 7 4 6 9 5 8 3 10 12 7 11 6 A B C D E F G
The weighted graph used in Examples 1 and 2.

Use Kruskal’s algorithm to find a minimum spanning tree for the graph above. State its total weight.

Solution. There are 77 vertices, so the tree needs 66 edges. Work down the edges in order of weight:

EdgeWeightDecision
CF3Add
AD4Add
BE5Add
BD6Add
FG6Add
AB7Reject (makes cycle A–B–D–A)
EG7Add: that’s 66 edges, so stop

(BD and FG tie at 66, and AB and EG tie at 77; you can take either first and get the same result.)

The MST is CF, AD, BE, BD, FG, EG, with total weight

3+4+5+6+6+7=313 + 4 + 5 + 6 + 6 + 7 = 31
The weighted graph with vertices A to G. The minimum spanning tree edges are highlighted: CF 3, AD 4, BE 5, BD 6, FG 6 and EG 7, total weight 31. The other edges are faded and dashed. 7 4 6 9 5 8 3 10 12 7 11 6 A B C D E F G
The minimum spanning tree (blue) has total weight 3131.

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.

StepTree so farCheapest edges to new verticesChosen
1AAB 7, AD 4AD 4
2A, DAB 7, BD 6, DE 10, DG 12BD 6
3A, D, BBC 9, BE 5, DE 10, DG 12BE 5
4A, D, B, EBC 9, CE 8, EF 11, EG 7, DG 12EG 7
5A, D, B, E, GBC 9, CE 8, EF 11, FG 6FG 6
6A, D, B, E, G, FBC 9, CE 8, CF 3CF 3

The edges in order are AD, BD, BE, EG, FG, CF, with total weight 4+6+5+7+6+3=314 + 6 + 5 + 7 + 6 + 3 = 31. 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.

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.

PQRST
P–85–9
Q8–47–
R54–610
S–76–3
T9–103–

Solution. Follow the matrix method:

  • Cross out row P and label column P with 11. Smallest entry left in column P: 55 (row R). Circle PR =5= 5.
  • Cross out row R; label column R with 22. In columns P and R, the uncrossed entries are Q 88, T 99 (column P) and Q 44, S 66, T 1010 (column R). Smallest: 44 in row Q. Circle RQ =4= 4.
  • Cross out row Q; label column Q with 33. Uncrossed entries in columns P, R, Q: T 99; S 66, T 1010; S 77. Smallest: 66 in row S. Circle RS =6= 6.
  • Cross out row S; label column S with 44. Uncrossed entries: T 99, T 1010, and T 33 in column S. Smallest: 33. Circle ST =3= 3.

Every column is now labelled. The MST is PR, RQ, RS, ST, with total length

5+4+6+3=18 km5 + 4 + 6 + 3 = 18 \text{ km}

A graph has edges AB 44, BC 44, CA 44 and CD 22. Find the weight of a minimum spanning tree. Is the MST unique?

Solution. Kruskal’s: take CD 22, then any two of the three edges of weight 44 (the third would complete the triangle A–B–C). The total is 2+4+4=102 + 4 + 4 = 10.

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 1010. Different algorithms or starting points might pick different ones, but the minimum total is always 1010.

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 nn vertices has exactly n−1n - 1 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.

1. (Warm-up) A graph has edges AB 33, AC 55, BC 44, BD 66, CD 22, CE 77 and DE 88. Use Kruskal’s algorithm to find a minimum spanning tree and its weight.

Solution

In order: CD 22 add, AB 33 add, BC 44 add, AC 55 reject (cycle A–B–C–A), BD 66 reject (cycle B–C–D–B), CE 77 add. That’s 44 edges for 55 vertices, so stop.

MST: CD, AB, BC, CE, with weight 2+3+4+7=162 + 3 + 4 + 7 = 16.

2. (Warm-up) A connected graph has 99 vertices and 1515 edges. How many edges are in a spanning tree? How many edges of the graph are not used?

Solution

A spanning tree on 99 vertices has 9−1=89 - 1 = 8 edges, so 15−8=715 - 8 = 7 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.

ABCDE
A–1491712
B14–11815
C911–1310
D17813–16
E12151016–
Solution
  • From A: the smallest entry in column A is 99 (C). Add AC 99.
  • From A, C: candidates B 1414, D 1717, E 1212 (column A) and B 1111, D 1313, E 1010 (column C). Add CE 1010.
  • From A, C, E: smallest is 1111 (C to B). Add CB 1111.
  • From A, C, E, B: smallest to D is 88 (B to D). Add BD 88.

Order: AC, CE, CB, BD. Total weight 9+10+11+8=389 + 10 + 11 + 8 = 38.

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 1212, GE 77, GF 66. Add FG 66.
  • From G, F: CF 33 is smallest. Add CF 33.
  • From G, F, C: GD 1212, GE 77, EF 1111, CE 88, BC 99. Add EG 77.
  • From G, F, C, E: BE 55 is smallest. Add BE 55.
  • From G, F, C, E, B: BD 66, AB 77, DE 1010, DG 1212. Add BD 66.
  • Last: AD 44 (smaller than AB 77). Add AD 44.

Order: FG, CF, EG, BE, BD, AD. Total 6+3+7+5+6+4=316 + 3 + 7 + 5 + 6 + 4 = 31, 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 44, KM 66, LM 33, LN 77, MN 55, MO 88, NO 44, NP 66 and OP 55. 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 33 add, KL 44 add, NO 44 add, MN 55 add, OP 55 add. That’s 55 edges for 66 villages, so stop. (KM 66, NP 66, LN 77 and MO 88 would each make a cycle anyway.)

Total length: 3+4+4+5+5=213 + 4 + 4 + 5 + 5 = 21 km.

Cost: 21×15 000=315 00021 \times 15\,000 = 315\,000, 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 1212 already chosen, then apply Kruskal’s to the rest: CF 33 add, AD 44 add, BE 55 add, BD 66 add, FG 66 add. Now all 77 vertices are connected with 66 edges (D and G were joined at the start, and FG links C and F to them), so stop. EG 77 is no longer needed: it would make the cycle E–B–D–G–E.

Total: 12+3+4+5+6+6=3612 + 3 + 4 + 5 + 6 + 6 = 36.

8. (Challenge) In Example 3, a sixth site U is added. The only possible pipelines to U are UP 22 km and UT 44 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 22 add, ST 33 add, QR 44 add, UT 44 add, PR 55 add. Now all 66 sites are connected with 55 edges, so stop. (RS 66 would make the cycle R–S–T–U–P–R.)

New total: 2+3+4+4+5=182 + 3 + 4 + 4 + 5 = 18 km, the same as before.

The new site gives a cheaper way to link the two halves of the network: P–U–T costs 2+4=62 + 4 = 6, and it replaces RS (also 66). So the extra vertex adds two edges but lets you drop one, and the total stays at 1818 km.

9. (Challenge) In the graph in the first figure, the weight of edge CE is changed from 88 to ww. For which values of ww 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 33, 66 and 77. 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 77.

  • If w<7w \lt 7: swap EG for CE and the total drops, so CE is in every MST (the new total is 24+w24 + w).
  • If w=7w = 7: CE and EG tie, so there are two MSTs, one with CE and one without.
  • If w>7w \gt 7: CE is not in the MST.

So CE is in every MST when w<7w \lt 7.

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.

H1. (Foundations) Answer each question and give a reason.

  • (a) A connected graph has 1212 vertices and 2020 edges. How many edges does a spanning tree have, and how many edges of the graph are left out?
  • (b) Can a graph with 1010 vertices and 88 edges be connected?
  • (c) A student says they found a minimum spanning tree with 88 edges for a graph with 88 vertices. Explain how you know they made a mistake.

H2. (Foundations) A weighted graph has vertices A to F and edges AB 99, AC 44, AF 1111, BC 66, BD 88, CD 1212, CE 77, DE 33, DF 1010 and EF 1313. 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).

PLFCBR
P–450–380–700
L450–520–610–
F–520–340–290
C380–340–560480
B–610–560–430
R700–290480430–
  • (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 1414, GJ 2222, GK 1919, HJ 1111, HM 2525, JK 1616, JM 1313, JN 2727, KN 1818 and MN 2121. 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.

ABCDE
A–23311827
B23–152921
C3115–2634
D182926–24
E27213424–
  • (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 55, AC 33, BC 55, BD 44, CD 66, CE 44, DE 55, EF 22 and DF 77.

  • (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.

H7. (Challenge) A student uses Prim’s algorithm, starting at A, on the graph given by this table.

ABCDE
A–46––
B4–8912
C68–510
D–95–7
E–12107–

Their work reads: “Start at A. Cheapest edge from A is AB 44. Cheapest edge from B is BC 88. Cheapest edge from C is CD 55. Cheapest edge from D is DE 77. Total 2424.”

  • (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 66, AC 99, BD 77, CD 44, CE 88, DE 1010, and an edge BC whose weight is xx, where x>0x \gt 0.

  • (a) Find the weight of a minimum spanning tree when x=5x = 5.
  • (b) Find the weight of a minimum spanning tree in terms of xx, for every value of x>0x \gt 0.
  • (c) For which value of xx 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?