Resources For Teachers For Tutors For Students & Parents AI New Books Pricing
Year 12 General (Unit 3 & 4) Networks and decision mathematics 1

Trees and minimum spanning trees

20 practice questions 0 video lessons Theory + worked examples

Find the cheapest way to connect every place in a network with minimum spanning trees in Year 12 General Mathematics for Queensland (QCAA). Councils linking towns with fibre, farmers running irrigation pipe across a cane farm and engineers wiring a new school all want the same thing: every site joined, with no wasted links.

You will learn what a tree and a spanning tree are, why a tree with \(n\) vertices has \(n - 1\) edges, and how to build a minimum spanning tree with Prim's algorithm and Kruskal's algorithm. You will practise real connector problems, including costs, before moving on to activity networks and project planning.

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

Theory

Joining every location for the least cost is a classic network problem. This Year 12 General Mathematics guide for Queensland students explains trees, spanning trees and minimum spanning trees, and shows step by step how Prim's and Kruskal's algorithms find the lightest network that links every vertex.

A tree is a connected graph with no cycles. Because there are no cycles, there is exactly one path between any two vertices of a tree. A tree with \(n\) vertices always has \(n - 1\) edges. Remove any edge and the tree falls into two pieces; add any edge and a cycle appears.

A spanning tree of a connected graph is a tree made from edges of that graph that reaches every vertex. Most graphs have many different spanning trees, and each one has \(n - 1\) edges.

In a weighted graph, the weight of a spanning tree is the total of its edge weights. A minimum spanning tree (MST) is a spanning tree whose total weight is as small as possible. It answers minimum connector questions: the least cable, pipe or track needed to join every location. Two step-by-step methods find it: Prim's algorithm and Kruskal's algorithm.

Key idea. A minimum spanning tree joins every vertex using the least total weight, with no cycles. Both Prim's and Kruskal's algorithms always pick the lightest edge that is allowed, and both give the same total weight.
A spanning tree of a graphGraph with six vertices and nine edges. Five red edges AF, BF, BC, CE and DE form a spanning tree; the other four edges are grey. A B C D E F
The five red edges reach all six vertices and make no cycle, so they form a spanning tree. Six vertices need \(6 - 1 = 5\) edges.
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 RT, PU, QU, RS, QT (total 24) is red. P Q R S T U 6 4 5 7 9 8 3 5 6
The minimum spanning tree (red) has total weight \(3 + 4 + 5 + 5 + 7 = 24\). No other spanning tree of this graph is lighter.

A tree (and so any spanning tree) with \(n\) vertices has one edge fewer than it has vertices:

\[\text{edges in a tree} = n - 1\]
edges=n−1

To turn a connected graph with \(e\) edges into a spanning tree, the number of edges you leave out is:

\[\text{edges removed} = e - (n - 1)\]
removed=e−(n−1)

The weight of a spanning tree is the total of its edge weights:

\[\text{weight of the tree} = \text{sum of the weights of its } n - 1 \text{ edges}\]
weight=∑edge weights
Check your answer. A minimum spanning tree must have exactly \(n - 1\) edges, reach every vertex and contain no cycle.

Prim's algorithm

  1. Start at any vertex. It is the tree so far.
  2. Look at every edge that joins a vertex in the tree to a vertex not yet in the tree. Add the lightest of these edges and its new vertex.
  3. Repeat until every vertex is in the tree (\(n - 1\) edges).
  4. Total the weights of the chosen edges.

Kruskal's algorithm

  1. List the edges from lightest to heaviest.
  2. Take the edges in that order. Add an edge if its two ends are not yet joined; reject it if they are already joined, because it would make a cycle.
  3. Stop when you have \(n - 1\) edges, then total their weights.

Prim's keeps a single tree growing from one vertex. Kruskal's may build several separate pieces that join up later. For a small graph you can also find the tree by inspection, which is really Kruskal's done by eye.

Example 1 β€” Counting edges in a spanning tree
The graph shown has \(7\) vertices and \(10\) edges. (i) How many edges does any spanning tree of this graph have? (ii) How many edges must be left out to make a spanning tree? (iii) Sam chose the six red edges. Is this a spanning tree?
Solution

Use \(n - 1\) for the edges in a tree, then check the red edges for a cycle and for connection.

Example 1 graphSeven vertices A to G and ten edges. Red edges chosen: AB, BC, CG, BG, DE, EF. A B C D E F G
\(\text{(i) edges in a spanning tree}\)\(=\)\(7 - 1\)
\(\)\(=\)\(6\)
\(\text{(ii) edges left out}\)\(=\)\(10 - 6\)
\(\)\(=\)\(4\)
6,4

(i) 6 edges. (ii) 4 edges. (iii) No. Sam has the right number of edges and touches every vertex, but \(B\), \(C\) and \(G\) form a cycle, and the piece \(D\text{-}E\text{-}F\) is not joined to the rest. A spanning tree must be connected with no cycle.

Example 2 β€” Kruskal's algorithm
Use Kruskal's algorithm to find a minimum spanning tree of the weighted graph shown, and state its weight.
Solution

Take the edges from lightest to heaviest. Reject any edge whose ends are already joined.

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
\(AF\ (2)\)\(:\)\(\text{add}\)
\(DE\ (3)\)\(:\)\(\text{add}\)
\(BC\ (4)\)\(:\)\(\text{add}\)
\(DF\ (4)\)\(:\)\(\text{add}\)
\(EF\ (5)\)\(:\)\(\text{reject (E and F already joined: a cycle)}\)
\(BF\ (6)\)\(:\)\(\text{add}\)

That makes \(5\) edges for \(6\) vertices, so the tree is complete.

\(\text{total weight}\)\(=\)\(2 + 3 + 4 + 4 + 6\)
\(\)\(=\)\(19\)
Example 2 minimum spanning treeMinimum spanning tree AF, DE, BC, DF, BF in red, total 19. A B C D E F 7 4 8 3 9 2 6 7 4 5
weight=19

The minimum spanning tree uses \(AF, DE, BC, DF\) and \(BF\), with weight 19. Edge \(EF\) was rejected because \(E\) and \(F\) were already joined through \(D\).

Example 3 β€” Prim's algorithm
Use Prim's algorithm, starting at \(A\), to find a minimum spanning tree of the graph shown. Give the order in which the edges are added and the total weight.
Solution

At each step, look only at edges from the tree to a vertex not yet in it, and take the lightest.

Example 3 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{total weight}\)\(=\)\(3 + 4 + 4 + 5 + 6\)
\(\)\(=\)\(22\)
Example 3 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
weight=22

The edges are added in the order \(AC, CB, BD, DE, EF\), and the total weight is 22. At step 2, \(BD\) is light but is not allowed yet, because neither \(B\) nor \(D\) is in the tree.

Example 4 β€” Linking towns with cable
A provider wants to join six towns on the Atherton Tablelands with fibre-optic cable: Kuranda \((K)\), Mareeba \((M)\), Atherton \((A)\), Yungaburra \((Y)\), Herberton \((H)\) and Malanda \((L)\). The possible links and their lengths in km are shown. (i) Find the least total length of cable that links every town. (ii) Laying cable costs $28,000 per km. Find the cost of this network.
Solution

(i) This is a minimum spanning tree problem. Use Kruskal's algorithm.

Possible cable links on the Atherton TablelandsTowns K Kuranda, M Mareeba, A Atherton, Y Yungaburra, H Herberton, L Malanda. Possible links in km: MK 35, MA 32, KY 50, AY 13, AH 17, AL 22, YL 17, HL 30, MH 45. K M A Y H L 35 32 50 13 17 22 17 30 45
\(AY\ (13\ \text{km})\)\(:\)\(\text{add}\)
\(AH\ (17\ \text{km})\)\(:\)\(\text{add}\)
\(LY\ (17\ \text{km})\)\(:\)\(\text{add}\)
\(AL\ (22\ \text{km})\)\(:\)\(\text{reject (A and L already joined: a cycle)}\)
\(HL\ (30\ \text{km})\)\(:\)\(\text{reject (H and L already joined: a cycle)}\)
\(AM\ (32\ \text{km})\)\(:\)\(\text{add}\)
\(KM\ (35\ \text{km})\)\(:\)\(\text{add}\)
\(\text{total weight}\)\(=\)\(13 + 17 + 17 + 32 + 35\)
\(\)\(=\)\(114\ \text{km}\)
Cheapest cable networkMinimum spanning tree AY, AH, YL, MA, MK in red, total 114 km. K M A Y H L 35 32 50 13 17 22 17 30 45

(ii) Multiply the length by the cost per km:

\(\text{cost}\)\(=\)\(114 \times 28000\)
\(\)\(=\)\(\$3\,192\,000\)
114×28000=3192000

(i) The least length is 114 km, using the links \(AY, AH, YL, MA\) and \(MK\). (ii) The cable costs $3,192,000. Prim's algorithm from any town gives the same five links.

Common pitfalls

Not just the lightest edges. Picking the \(n - 1\) lightest edges can make a cycle and leave a vertex out. Watch out for adding an edge without checking whether its ends are already joined.
Prim's needs a new vertex. In Prim's algorithm, only edges from the tree to a vertex outside it are allowed. Watch out for grabbing the lightest edge anywhere in the graph, which is Kruskal's rule, not Prim's.
Spanning tree or shortest path? A minimum spanning tree joins every vertex as cheaply as possible. It does not give the shortest route between two particular vertices. Watch out for using the tree to answer a travel question.

Frequently asked questions

What is a spanning tree?

A spanning tree of a connected graph is a tree, made from edges of the graph, that reaches every vertex. It has no cycles, and with n vertices it has n - 1 edges.

How many edges does a spanning tree have?

One fewer than the number of vertices. A graph with 8 vertices has spanning trees with 7 edges.

What is the difference between Prim's and Kruskal's algorithm?

Prim's algorithm grows one tree from a starting vertex, always adding the lightest edge to a new vertex. Kruskal's algorithm takes edges from lightest to heaviest anywhere in the graph and rejects any edge that would make a cycle. Both give a minimum spanning tree with the same total weight.

Can a graph have more than one minimum spanning tree?

Yes. If two edges have equal weight, there may be two different minimum spanning trees. They always have the same total weight.

Does it matter where I start Prim's algorithm?

No. Starting at a different vertex can change the order the edges are added, but the total weight of the minimum spanning tree is the same.

When do you use a minimum spanning tree in real life?

Whenever every location must be connected for the least total cost or length, such as laying cable, water pipes or irrigation lines between farms, towns or buildings.