A spanning tree connects every vertex of a network with no cycles. The minimum spanning tree is the one with the smallest total edge weight, the cheapest way to keep everything connected. For n vertices it always has n − 1 edges.
Five towns, cable costs: A–B 4, A–C 8, B–C 3, B–D 6, C–D 5, C–E 7, D–E 2. Find the MST.
| Edge (sorted) | Weight | Add? |
|---|---|---|
| D–E | 2 | ✓ |
| B–C | 3 | ✓ |
| A–B | 4 | ✓ |
| C–D | 5 | ✓ (4th → done) |
| B–D, C–E, A–C | 6, 7, 8 | cycle / not needed |
MST total = 2 + 3 + 4 + 5 = 14. With 5 towns, 4 edges.
• Skip any edge that closes a cycle, even if it's small.
• Stop at exactly n − 1 edges, don't keep going.
• MST connects all vertices, shortest path links just two, different question.
• The total is the sum of the chosen edge weights.
Gold edges (A–B, B–C, C–D, D–E) reach every town with the least total, 14. The grey chords were skipped: each would close a loop.
Any loop has one edge you don't need, you could already reach those vertices the other way round. Drop it and you save weight while keeping everyone connected. That's why a minimum spanning tree never contains a cycle.
Don't read yet, just have a go in your head:
Sorted: D–E 2, B–C 3, A–B 4, C–D 5, … Add D–E, B–C, A–B, C–D = 4 edges. Total = 2+3+4+5 = 14.
After D–E (2), B–C (3) and A–B (4), the running total is ?. Adding C–D (5) makes the final total ?
Four towns, edges P–Q 5, P–R 9, Q–R 4, Q–S 8, R–S 6. Find the MST and its total. Check below.
In one sentence, out loud: when do you skip an edge in Kruskal's algorithm?