Resources For Teachers For Tutors For Students & Parents AI New Books Pricing
Year 12 Maths - General (Unit 3 & Unit 4) Trees, minimum connectors and shortest paths

Minimum spanning trees and Prim's algorithm

20 practice questions 0 video lessons Theory + worked examples

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.

Create a free accountTrack your progress and save your work as you go.
Create free account

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.

Key idea. A minimum spanning tree on \(n\) vertices has \(n - 1\) edges. Prim’s algorithm adds one of them at each of \(n - 1\) steps: always the cheapest edge from the tree to a new vertex.
A minimum spanning treeWeighted graph: PQ 6, PU 4, QU 5, QT 7, UT 9, QR 8, RT 3, RS 5, ST 6. The minimum spanning tree PU, QU, QT, RT, RS (total 24) is red. P Q R S T U 6 4 5 7 9 8 3 5 6
The red edges join all \(6\) vertices with no cycle, using \(5\) edges, and their total \(4 + 5 + 7 + 3 + 5 = 24\) is the smallest possible.
Prim's algorithm part-wayTree so far PU and QU in red joining P, U, Q. Edges from the tree to a new vertex in amber: QT 7, QR 8, UT 9. Other edges grey. P Q R S T U 6 4 5 7 9 8 3 5 6
Prim’s algorithm from \(P\) after two steps: the tree (red) holds \(P\), \(U\) and \(Q\). The amber edges reach new vertices; the lightest is \(QT\) (\(7\)), so it is added next.

For a connected graph with \(n\) vertices:

\[\text{edges in a minimum spanning tree} = n - 1\]
n−1
\[\text{weight of the tree} = \text{sum of the weights of its } n - 1 \text{ edges}\]
weight=∑edge weights

When each unit of length has a cost:

\[\text{total cost} = \text{weight of the tree} \times \text{cost per unit}\]
cost=weight×rate
Ties. If two candidate edges have the same weight, either may be chosen. The tree may change, but its total weight does not.

Prim’s algorithm

  1. Start at any vertex (or the one the question names). It is the tree so far.
  2. List the candidates: every edge with one end in the tree and the other end not in the tree.
  3. Add the lightest candidate and its new vertex to the tree.
  4. Repeat steps 2 and 3 until every vertex is in the tree (\(n - 1\) edges).
  5. 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.

Example 1 — Prim’s algorithm step by step
Use Prim’s algorithm, starting at \(A\), to find a minimum spanning tree of the network shown, and its weight.
Solution

At each step, list the edges from the tree to a new vertex and add the lightest.

Example 1 weighted graphEdges AB 8, AC 4, BC 6, BD 4, CD 9, CE 7, DE 3, DF 6, EF 5. A B C D E F 8 4 6 4 9 7 3 6 5
StepTree so farEdges to a new vertexAdd
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\)
Example 1 minimum spanning treeMinimum spanning tree AC, BC, BD, DE, EF in red, total 22. A B C D E F 8 4 6 4 9 7 3 6 5
22

The edges are added in the order \(AC, CB, BD, DE, EF\). The minimum spanning tree has weight \(22\).

Example 2 — A different start, the same tree
Use Prim’s algorithm on the network shown, (i) starting at \(A\), (ii) starting at \(D\). Compare the trees.
Solution

Each step adds the lightest edge from the tree to a new vertex.

Example 2 weighted graphEdges AB 7, BC 4, CD 8, DE 3, AE 9, AF 2, BF 6, CF 7, DF 4, EF 5. A B C D E F 7 4 8 3 9 2 6 7 4 5
\(\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\)
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.

Example 3 — A minimum connector problem
Six towns in the Macedon Ranges are to be linked by fibre-optic cable. The possible links and their lengths in km are shown. (i) Use Prim’s algorithm, starting at \(K\), to find the shortest total length of cable that connects every town. (ii) Cable costs \(\$25\) thousand per km to lay. Find the total cost.
Solution

The cheapest network that connects every town is a minimum spanning tree.

Possible cable links between six townsTowns K, W, T, G, L, R. Possible links in km: LR 35, LK 32, RW 50, KW 13, KT 17, KG 22, WG 17, TG 30, LT 45. R L K W T G 35 32 50 13 17 22 17 30 45
StepTree so farEdges to a new vertexAdd
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}\)
Cheapest cable networkMinimum spanning tree KW, KT, WG, KL, LR in red, total 114 km. R L K W T G 35 32 50 13 17 22 17 30 45
114km

(i) 114 km of cable. (ii) \(\$2\,850\) thousand, which is \(\$2\,850\,000\).

Example 4 — Prim’s algorithm from a table
The table gives the lengths (km) of possible water pipes between five towns on the Bellarine Peninsula. A dash means no pipe is possible. Use Prim’s algorithm, starting at \(L\), to find the minimum total length of pipe.
\(L\)\(D\)\(P\)\(Q\)\(O\)
\(L\)–715–14
\(D\)7–91811
\(P\)159–16–
\(Q\)–1816–10
\(O\)1411–10–
Solution

Look along the rows of the towns already connected, in the columns of towns not yet connected.

StepTree so farEdges to a new vertexAdd
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}\)
37km

The pipes \(LD, DP, DO, OQ\) connect all five towns with 37 km of pipe.

Common pitfalls

Always reach a new vertex. In Prim’s algorithm only edges from the tree to a vertex not in the tree are candidates. An edge joining two tree vertices would make a cycle.
Not the lightest edge overall. Prim’s algorithm picks the lightest edge touching the tree so far, not the lightest edge anywhere in the network.
Minimum spanning tree is not shortest path. The tree connects every vertex as cheaply as possible; it does not give the shortest route between two particular vertices.

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.