Minimum spanning trees
Trees and spanning trees, and finding a minimum spanning tree with Kruskal's algorithm and Prim's algorithm.
A shortest path joins two places as cheaply as possible. Often you need something different: join every place, using as little cable, pipe or road as possible. The answer is always a special shape called a tree.
Trees and spanning trees
- A tree is a connected network with no cycles. There is exactly one path between any two of its vertices.
- A tree with $n$ vertices always has exactly $n - 1$ edges. Start with one vertex, and every edge you add brings in exactly one new vertex.
- A spanning tree of a network is a tree that uses all the vertices of the network and some of its edges.
- In a weighted network, a minimum spanning tree (MST) is a spanning tree whose total weight is as small as possible.
Why must the cheapest way to connect everything be a tree? If your design had a cycle, you could delete the most expensive edge on that cycle and everything would still be connected, for less. So the cheapest design never has a cycle.
Kruskal's algorithm
Kruskal's algorithm looks at the whole network at once:
- List all the edges from smallest weight to largest.
- Work down the list. Keep an edge if it does not make a cycle with the edges already kept. Skip it if it does.
- Stop when you have kept $n - 1$ edges.
Use Kruskal's algorithm to find a minimum spanning tree for the network above, and its total cost.
Prim's algorithm
Prim's algorithm grows one tree outwards from a starting vertex:
- Choose any vertex to start.
- Look at every edge that joins a vertex in the tree to a vertex not yet in the tree. Add the smallest one.
- Repeat until every vertex is in the tree.
Prim's can never make a cycle, because each new edge always reaches a brand-new vertex. That makes it easier to use on a big diagram where cycles are hard to spot.
Use Prim's algorithm, starting at P, on the same network.
In Build your own, connect all seven buildings with as little cable as you can, then press Is mine the minimum?
Watch Kruskal's. Which edge gets skipped, and which cycle would it have made?
Watch Prim's. Does it choose the same edges in the same order? Does it end with the same total?
Your turn
Use Prim's algorithm on the Example 1 network, starting at R this time. List the edges in the order you add them.
Show the working
From R: QR $11$ (beats RT $12$ and RU $14$). From {R, Q}: QT $8$. From {R, Q, T}: TU $6$. Then ST $7$. Then PS $9$. Order: QR, QT, TU, ST, PS. Total $41$, the same tree as before.
A spanning tree connects $12$ towns. How many edges does it have?
A tree with $n$ vertices has $n - 1$ edges: $12 - 1 = 11$.
A tree has $9$ edges. How many vertices does it have?
Edges $= n - 1$, so $n = 9 + 1 = 10$.
In Example 1, why was SU skipped?
S–T and T–U were already kept, so S and U were connected. SU would close the cycle S–T–U.
Which is true of Prim's algorithm?
Prim's grows the tree one new vertex at a time. Any start vertex gives a minimum spanning tree, so the total weight is the same.
A network has a cycle with edges of weight $4$, $7$ and $9$. Which edge can never be in a minimum spanning tree, if all weights in the network are different?
The heaviest edge on a cycle can always be removed without disconnecting anything, and removing it lowers the total. So it is never in the MST.
In Example 1, suppose the cost of PS rises from $9$ to $16$. Find the new total weight of the minimum spanning tree.
Kruskal's: TU $6$, ST $7$, QT $8$ kept, SU $10$ skipped (cycle), QR $11$ kept, RT $12$ skipped (cycle Q–R–T), PQ $13$ kept, which brings in P. Total $= 6 + 7 + 8 + 11 + 13 = 45$.
A spanning tree must:
"Spanning" means it reaches every vertex. It uses only some of the edges, and as a tree it has no cycles.
Exam style: The weights in Example 1 are in thousands of dollars. What is the minimum cost of connecting all six sites, in thousands of dollars?
The minimum spanning tree is TU, ST, QT, PS, QR: $6 + 7 + 8 + 9 + 11 = 41$, so $\$41\,000$.
Exam style: Using Kruskal's algorithm on Example 1, which edge is kept immediately after PS?
The next edges in order are SU $10$ (skipped, cycle) and then QR $11$, which is kept.
Coaching Centre