Minimum spanning trees and Prim's algorithm
Learn to connect every site for the least cost with minimum spanning trees in Year 12 VCE General Mathematics. When a water authority pipes supply to Bellarine towns, or an internet provider runs fibre between Macedon Ranges communities, every extra kilometre costs money. The cheapest network that still reaches everyone is a minimum spanning tree.
You will use Prim’s algorithm to grow a minimum spanning tree one edge at a time, from a diagram or from a table of distances, and record each step clearly. You will total lengths and costs, see why the starting vertex does not change the answer, and then turn to finding shortest paths between two places.
Theory
A minimum spanning tree connects every vertex of a network with the least total weight. This Year 12 General Mathematics guide for students in Victoria shows how Prim’s algorithm builds one step by step, from a network diagram or a table, and how to use it to solve minimum connector problems involving length and cost.
In a weighted graph, the weight of a spanning tree is the total of the weights of its edges. A minimum spanning tree is a spanning tree with the smallest possible total weight. Finding one solves a minimum connector problem: joining every town, house or pump to a network (with cable, pipe or road) using the least length or cost.
Prim’s algorithm grows the tree one edge at a time. Start at any vertex. At each step, look at every edge that joins a vertex already in the tree to a vertex not yet in the tree, and add the lightest one. Stop when every vertex is in the tree. Because each new edge always reaches a new vertex, the tree can never contain a cycle.
For a small network, a minimum spanning tree can also be found by inspection, but Prim’s algorithm shows clearly that nothing has been missed. Whichever vertex you start from, the total weight comes out the same.
For a connected graph with \(n\) vertices:
When each unit of length has a cost:
Prim’s algorithm
- Start at any vertex (or the one the question names). It is the tree so far.
- List the candidates: every edge with one end in the tree and the other end not in the tree.
- Add the lightest candidate and its new vertex to the tree.
- Repeat steps 2 and 3 until every vertex is in the tree (\(n - 1\) edges).
- Total the weights of the chosen edges, and multiply by any cost per unit.
From a table: the tree so far is a set of rows; look along those rows (in the columns of vertices not yet in the tree) for the smallest entry.
If a particular edge must be included (for example, the owners insist on one pipe), start the tree with that edge and both of its vertices, then carry on with Prim’s algorithm as usual. Compare the new total with the minimum to find the extra length or cost. If a new edge is added to the network, run Prim’s algorithm again on the new network.
At each step, list the edges from the tree to a new vertex and add the lightest.
| Step | Tree so far | Edges to a new vertex | Add |
|---|---|---|---|
| 1 | \(A\) | \(AC\ 4,\ AB\ 8\) | \(AC\) (4) |
| 2 | \(A, C\) | \(CB\ 6,\ CE\ 7,\ AB\ 8,\ CD\ 9\) | \(CB\) (6) |
| 3 | \(A, C, B\) | \(BD\ 4,\ CE\ 7,\ CD\ 9\) | \(BD\) (4) |
| 4 | \(A, C, B, D\) | \(DE\ 3,\ DF\ 6,\ CE\ 7\) | \(DE\) (3) |
| 5 | \(A, C, B, D, E\) | \(EF\ 5,\ DF\ 6\) | \(EF\) (5) |
| \(\text{weight}\) | \(=\) | \(4 + 6 + 4 + 3 + 5\) |
| \(\) | \(=\) | \(22\) |
The edges are added in the order \(AC, CB, BD, DE, EF\). The minimum spanning tree has weight \(22\).
Each step adds the lightest edge from the tree to a new vertex.
| \(\text{(i) from } A\) | \(:\) | \(AF\ (2),\ FD\ (4),\ DE\ (3),\ FB\ (6),\ BC\ (4)\) |
| \(\text{(ii) from } D\) | \(:\) | \(DE\ (3),\ DF\ (4),\ FA\ (2),\ FB\ (6),\ BC\ (4)\) |
| \(\text{weight}\) | \(=\) | \(2 + 4 + 3 + 6 + 4\) |
| \(\) | \(=\) | \(19\) |
The edges are added in a different order, but both starts give the same tree \(AF, DF, DE, BF, BC\) with weight \(19\). Note that \(EF\) (\(5\)) is never chosen: from either start, \(E\) and \(F\) both join the tree through cheaper edges, and after that \(EF\) would join two tree vertices.
The cheapest network that connects every town is a minimum spanning tree.
| Step | Tree so far | Edges to a new vertex | Add |
|---|---|---|---|
| 1 | \(K\) | \(KW\ 13,\ KT\ 17,\ KG\ 22,\ KL\ 32\) | \(KW\) (13 km) |
| 2 | \(K, W\) | \(KT\ 17,\ WG\ 17,\ KG\ 22,\ KL\ 32,\ WR\ 50\) | \(KT\) (17 km) |
| 3 | \(K, W, T\) | \(WG\ 17,\ KG\ 22,\ TG\ 30,\ KL\ 32,\ TL\ 45,\ WR\ 50\) | \(WG\) (17 km) |
| 4 | \(K, W, T, G\) | \(KL\ 32,\ TL\ 45,\ WR\ 50\) | \(KL\) (32 km) |
| 5 | \(K, W, T, G, L\) | \(LR\ 35,\ WR\ 50\) | \(LR\) (35 km) |
| \(\text{length}\) | \(=\) | \(13 + 17 + 17 + 32 + 35\) |
| \(\) | \(=\) | \(114 \text{ km}\) |
| \(\text{cost}\) | \(=\) | \(114 \times 25\) |
| \(\) | \(=\) | \(2850 \text{ thousand dollars}\) |
(i) 114 km of cable. (ii) \(\$2\,850\) thousand, which is \(\$2\,850\,000\).
| \(L\) | \(D\) | \(P\) | \(Q\) | \(O\) | |
|---|---|---|---|---|---|
| \(L\) | – | 7 | 15 | – | 14 |
| \(D\) | 7 | – | 9 | 18 | 11 |
| \(P\) | 15 | 9 | – | 16 | – |
| \(Q\) | – | 18 | 16 | – | 10 |
| \(O\) | 14 | 11 | – | 10 | – |
Look along the rows of the towns already connected, in the columns of towns not yet connected.
| Step | Tree so far | Edges to a new vertex | Add |
|---|---|---|---|
| 1 | \(L\) | \(LD\ 7,\ LO\ 14,\ LP\ 15\) | \(LD\) (7 km) |
| 2 | \(L, D\) | \(DP\ 9,\ DO\ 11,\ LO\ 14,\ LP\ 15,\ DQ\ 18\) | \(DP\) (9 km) |
| 3 | \(L, D, P\) | \(DO\ 11,\ LO\ 14,\ PQ\ 16,\ DQ\ 18\) | \(DO\) (11 km) |
| 4 | \(L, D, P, O\) | \(OQ\ 10,\ PQ\ 16,\ DQ\ 18\) | \(OQ\) (10 km) |
| \(\text{length}\) | \(=\) | \(7 + 9 + 11 + 10\) |
| \(\) | \(=\) | \(37 \text{ km}\) |
The pipes \(LD, DP, DO, OQ\) connect all five towns with 37 km of pipe.
Common pitfalls
Frequently asked questions
What is a minimum spanning tree?
It is a spanning tree of a weighted graph whose edges have the smallest possible total weight. It connects every vertex as cheaply as possible, with no cycles.
How does Prim's algorithm work?
Start at any vertex. Repeatedly add the lightest edge that joins a vertex in the tree to a vertex not yet in the tree, until every vertex is included.
Does it matter where Prim's algorithm starts?
No. A different starting vertex can add the edges in a different order, but the total weight of the minimum spanning tree is always the same.
What is a minimum connector problem?
It is a real problem where every place must be linked, such as by cable, pipe or road, using the least total length or cost. The answer is a minimum spanning tree.
What if two edges have the same weight in Prim's algorithm?
Choose either one. There may then be more than one minimum spanning tree, but they all have the same total weight.