7D

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.

Six sites to be connected. Weights are costs in thousands of dollars.

Kruskal's algorithm

Kruskal's algorithm looks at the whole network at once:

  1. List all the edges from smallest weight to largest.
  2. Work down the list. Keep an edge if it does not make a cycle with the edges already kept. Skip it if it does.
  3. Stop when you have kept $n - 1$ edges.
Example 1●●

Use Kruskal's algorithm to find a minimum spanning tree for the network above, and its total cost.

TU $6$, ST $7$, QT $8$, PS $9$, SU $10$, QR $11$, RT $12$, PQ $13$, RU $14$, PT $15$
Sort the edges by weight.
Keep TU $6$, ST $7$, QT $8$
The three smallest edges never form a cycle with each other here, so keep them all.
Keep PS $9$
P isn't connected yet, so PS joins it on.
Skip SU $10$
S and U are already joined through T (S–T–U). Adding SU would close a cycle.
Keep QR $11$. Stop.
R isn't connected yet. That is the fifth edge, and 6 vertices need 5 edges, so stop.
Total $= 6 + 7 + 8 + 9 + 11 = 41$, so the minimum cost is $\$41\,000$
Add the kept edges.
The minimum spanning tree from Example 1 (orange), total weight 41.

Prim's algorithm

Prim's algorithm grows one tree outwards from a starting vertex:

  1. Choose any vertex to start.
  2. Look at every edge that joins a vertex in the tree to a vertex not yet in the tree. Add the smallest one.
  3. 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.

Example 2●●

Use Prim's algorithm, starting at P, on the same network.

Add PS $9$. Tree: P, S
From P, the edges are PS 9, PQ 13 and PT 15.
Add ST $7$. Tree: P, S, T
Edges leaving {P, S}: ST 7, SU 10, PQ 13, PT 15.
Add TU $6$. Tree: P, S, T, U
Edges leaving {P, S, T}: TU 6, QT 8, SU 10, RT 12, PQ 13.
Add QT $8$. Tree: P, Q, S, T, U
SU now joins two vertices already in the tree, so ignore it. Smallest edge to a new vertex: QT 8.
Add QR $11$. Done.
R is the last vertex. Edges to it: QR 11, RT 12, RU 14.
Total $= 41$
Same tree as Kruskal's, so the same total.
InteractiveCable up the school
Try this

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.

Flashcards
Tree
A connected network with no cycles
Edges in a tree with $n$ vertices
$n - 1$
Kruskal's algorithm
Smallest edge overall each time, skipping any that make a cycle
Prim's algorithm
Grow from a start vertex: smallest edge to a new vertex each time
Practice
  1. A spanning tree connects $12$ towns. How many edges does it have?

  2. A tree has $9$ edges. How many vertices does it have?

  3. In Example 1, why was SU skipped?

  4. Which is true of Prim's algorithm?

  5. 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?

  6. In Example 1, suppose the cost of PS rises from $9$ to $16$. Find the new total weight of the minimum spanning tree.

  7. A spanning tree must:

  8. 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?

  9. Exam style: Using Kruskal's algorithm on Example 1, which edge is kept immediately after PS?

Formula sheet