Trees and minimum spanning trees
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.
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.
A tree (and so any spanning tree) with \(n\) vertices has one edge fewer than it has vertices:
To turn a connected graph with \(e\) edges into a spanning tree, the number of edges you leave out is:
The weight of a spanning tree is the total of its edge weights:
Prim's algorithm
- Start at any vertex. It is the tree so far.
- 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.
- Repeat until every vertex is in the tree (\(n - 1\) edges).
- Total the weights of the chosen edges.
Kruskal's algorithm
- List the edges from lightest to heaviest.
- 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.
- 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.
Use \(n - 1\) for the edges in a tree, then check the red edges for a cycle and for connection.
| \(\text{(i) edges in a spanning tree}\) | \(=\) | \(7 - 1\) |
| \(\) | \(=\) | \(6\) |
| \(\text{(ii) edges left out}\) | \(=\) | \(10 - 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.
Take the edges from lightest to heaviest. Reject any edge whose ends are already joined.
| \(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\) |
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\).
At each step, look only at edges from the tree to a vertex not yet in it, and take 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{total weight}\) | \(=\) | \(3 + 4 + 4 + 5 + 6\) |
| \(\) | \(=\) | \(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.
(i) This is a minimum spanning tree problem. Use Kruskal's algorithm.
| \(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}\) |
(ii) Multiply the length by the cost per km:
| \(\text{cost}\) | \(=\) | \(114 \times 28000\) |
| \(\) | \(=\) | \(\$3\,192\,000\) |
(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
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.