Decision Mathematics 1 (Paper 3D or 4D), topic 2 (Algorithms on graphs), spec point 2.1
The problem
A minimum spanning tree (minimum connector) joins every vertex using the least total weight. A spanning tree on vertices always has edges.
The network used below
| Edge | AB | AC | AD | BC | BE | CD | CE | DE | DF | EF |
|---|---|---|---|---|---|---|---|---|---|---|
| Weight | 7 | 5 | 9 | 3 | 6 | 4 | 8 | 5 | 11 | 6 |
Kruskal's algorithm
- List the edges in order of weight.
- Add the shortest edge that does not make a cycle.
- Repeat until there are edges.
Order: BC 3, CD 4, AC 5, DE 5, BE 6, EF 6, AB 7, CE 8, AD 9, DF 11.
Accept BC, CD, AC and DE. Reject BE (it would make cycle B-C-D-E-B). Accept EF. That is 5 edges, so stop.
Tree: BC, CD, AC, DE, EF, with weight .
Prim's algorithm
- Start at any vertex.
- Add the shortest edge joining a vertex in the tree to one not in it.
- Repeat until every vertex is in the tree.
Starting at A: AC 5, CB 3, CD 4, DE 5, EF 6. The vertices join in the order A, C, B, D, E, F. Same total, 23.
Prim's algorithm on a matrix
- Choose a starting vertex. Cross out its row and number its column 1.
- Look down all the numbered columns for the smallest entry not crossed out. Circle it. Its row gives the new vertex.
- Cross out that row and number its column with the next number.
- Repeat until every row is crossed out.
The circled entries are the edges of the tree. Matrices make Prim's algorithm easy to run by computer.
Kruskal or Prim?
Kruskal's algorithm needs the edges sorted and a check for cycles at every step. Prim's algorithm never makes a cycle, and works directly from a matrix.
Common mistake: in Kruskal's algorithm, rejecting an edge only because both its vertices are already used. Reject it only if it makes a cycle; two separate parts of the tree may still need joining.
Exam tip: write the order in which edges are accepted or rejected, and say why each rejection makes a cycle. In Prim's algorithm on a matrix, state the order in which vertices join.
Check yourself
How many edges are in a spanning tree of a 9-vertex network?
.
Which algorithm can be used on a matrix?
Prim’s.
Can Prim’s algorithm create a cycle?
No: each new edge joins a new vertex to the tree.