Topic 2 · Lesson 1 of 4
Minimum spanning trees
Minimum spanning trees
A spanning tree of a network connects every vertex with no cycles; with vertices it has arcs. A minimum spanning tree (MST, or minimum connector) is a spanning tree of least total weight. It answers "connect everything as cheaply as possible". If all the weights are different, the minimum spanning tree is unique; if some are equal, there may be several.
Kruskal's algorithm
- List the arcs in ascending order of weight.
- Take the arcs in this order. Include an arc unless it would make a cycle with arcs already included; if it would, reject it.
- Stop when arcs have been included.
The arcs chosen need not be connected to each other until the end. In an exam, list every arc you consider, with "include" or "reject" (a tick or a cross), in order.
Worked example
Kruskal's algorithm
In the network above, the arcs in order are DE 3, BC 4, AC 5, BE 6, AB 7, DF 8, ...
Show solutionHide solution
- DE ✓, BC ✓, AC ✓, BE ✓ (joining {A, B, C} to {D, E}).
- AB ✗: A and B are already connected through C.
- DF ✓. That is 5 arcs for 6 vertices, so stop.
- Weight .
Prim's algorithm
- Choose a starting vertex.
- Look at every arc that joins a vertex in the tree to a vertex not yet in the tree. Add the one with the least weight.
- Repeat until every vertex is in the tree.
Prim's tree grows as one connected piece, and there is never a need to check for cycles, because each new arc brings in a new vertex.
Worked example
Prim's algorithm
Start at A in the network above.
Show solutionHide solution
- Arcs from A: AB 7, AC 5. Choose AC.
- From {A, C}: AB 7, BC 4, CE 10. Choose BC.
- From {A, B, C}: BD 10, BE 6, CE 10. Choose BE.
- From {A, B, C, E}: BD 10, DE 3, EF 12. Choose DE.
- From {A, B, C, D, E}: DF 8, EF 12. Choose DF.
The same tree as Kruskal's, weight 26, found in the order AC, BC, BE, DE, DF.
Step through Kruskal's and Prim's algorithms on this network, or a new one, and check each arc you would include or reject.
Try it: simulation
Network algorithms
Loading the simulation…
Prim's algorithm on a distance matrix
When the network is a table, Prim's algorithm works directly on it:
- Choose a starting vertex. Cross out its row and number its column 1.
- Look down every numbered column for the smallest entry that is not crossed out. That entry gives the next arc; the row it is in is the new vertex.
- Cross out that row and number its column with the next number.
- Repeat until every row is crossed out.
This is why Prim's algorithm suits computers and tables: Kruskal's needs every arc sorted first and a check for cycles at every step, whereas Prim's only ever scans the numbered columns.
Adapting a minimum spanning tree
- An arc must be included (for example, a pipe already exists): include it first, then run Kruskal's algorithm as usual. Equivalently, add the arc to the tree and remove the heaviest arc on the cycle it makes.
- An arc has unknown weight : the arc is in the tree when it is lighter than the heaviest arc on the path that already joins its ends in the tree built without it.
Practise this topic
Both are part of Premium.