TL;DR
A minimum spanning tree is a set of edges that connects every vertex of a weighted network, uses no loops, and has the smallest possible total weight among all such sets. It answers the question "what is the cheapest way to connect everything?" when each link has a cost.
It matters because connecting places is a common task: cables between buildings, roads between towns, and links between computers all have a cost per link, and the cheapest full connection is exactly this tree.
Builds on: Graph, trees, Greedy Algorithm · Leads to: Single-Linkage Clustering and the HDBSCAN paper
Where it's used
Laying cable and pipe. A utility company that must connect several buildings with fibre wants the least total length of cable that still lets every building reach every other. The minimum spanning tree of the graph whose vertices are buildings and whose edge weights are cable lengths is that layout.
Approximating travelling routes. A common way to get a rough, quick route through many cities is to start from the minimum spanning tree and walk around it. The tree is not the best route, but its total length is a known lower bound on the shortest tour, which helps judge how good a route is.
Clustering data. Single-linkage clustering builds the minimum spanning tree of a set of points, where each edge weight is the distance between two points. Cutting the heaviest edges of that tree splits the points into groups that are far apart from one another. The HDBSCAN method uses a related tree to find clusters of varying density.
Image segmentation. Graph-based image methods treat each pixel as a vertex and weight edges by how different neighbouring pixels look. A minimum spanning tree over the image then gives a segmentation into regions that are internally similar.
How it works
Take four buildings, A, B, C and D. The possible links and their lengths in metres are: A to B 1, B to C 2, A to C 3, B to D 4, and C to D 5. A spanning tree needs one fewer edge than there are buildings, so here it needs 3 edges.
We will use Kruskal's method. It sorts the edges from cheapest to most expensive and then goes through them in order, adding an edge only if it does not close a loop. Throughout, a piece means a group of buildings that are already linked to each other, directly or through other buildings. At the start every building is its own piece.
- Sort the edges by weight. Sorting a list is a standard sorting task; here it is only the list of five lengths. The order is A-B (1), B-C (2), A-C (3), B-D (4), C-D (5). Check: each weight is at least as large as the one before it.
- Take A-B. The buildings are not yet connected to each other, so adding A-B creates no loop. The chosen edges are {A-B}, with total 1. Check: A and B are now in the same connected piece, and C and D are still alone.
- Take B-C. C is alone, so joining it to the piece containing A and B creates no loop. The chosen edges are {A-B, B-C}, with total 3. Check: now A, B and C are all connected, and D is still alone.
- Skip A-C. A and C are already in the same piece. Adding A-C would close a loop A to B to C to A. Check: a loop is exactly a case where both ends are already connected.
- Take B-D. D is alone, so this joins it to the rest without a loop. The chosen edges are {A-B, B-C, B-D}, with total 7. Check: four buildings and three edges, which is one fewer edge than buildings.
- Stop. Every building is now connected, so C-D is not needed. It would close a loop anyway.
The result is a tree with total weight 7. It is minimal because each step took the cheapest edge that did not create a loop.
flowchart LR A -- "1" --- B B -- "2" --- C A -. "3 skipped" .- C B -- "4" --- D C -. "5 skipped" .- D
The math
Let be a connected graph, where is the set of vertices and the set of edges, with a weight on each edge (a number, such as a length or a cost). A spanning tree is a subset such that the graph is a tree: it connects every vertex and contains no cycle, where a cycle is a closed loop of edges that returns to its starting vertex. Every spanning tree of a graph with vertices has exactly edges.
The weight of a tree is , the sum of its edge weights. A minimum spanning tree is a spanning tree with for every spanning tree .
Step 2 in symbols: the loop test
Adding an edge to a forest creates a cycle exactly when and are already connected through chosen edges. The procedure keeps track of which vertices are connected using the disjoint-set union structure, which answers "are and in the same piece?" quickly. If the answer is yes, the edge is skipped.
Step 3 in symbols: the growing forest
After accepted edges the chosen set is a forest with connected pieces. Each step adds one edge that joins two different pieces, so always has edges. In the example, after step 3, , with pieces: {A, B, C} and {D}.
Step 4 in symbols: why the cheapest safe edge is always right
A cut splits the vertices into two sets and . An edge crosses the cut when one end is in and the other is in .
Cut property. Suppose a set of edges belongs to some minimum spanning tree, and let be the cheapest edge that crosses a cut no edge of crosses. Then also belongs to some minimum spanning tree.
Sketch of the proof. Let be a minimum spanning tree containing but not . Adding to creates a cycle. That cycle crosses the cut at least twice, so it contains some other edge that crosses the cut. Because is the cheapest crossing edge, . Swapping for gives a spanning tree with weight at most , so it is also minimal, and it contains .
Kruskal's method is the repeated use of this property. Each accepted edge is the cheapest edge joining some piece to the rest.
Worked examples
Three vertices X, Y and Z, with edges XY = 1, YZ = 2 and XZ = 3. A spanning tree needs 2 edges.
- Sort: XY (1), YZ (2), XZ (3).
- Take XY. No loop. Total 1.
- Take YZ. Z was alone, so no loop. Total 3.
- Skip XZ. X and Z are already connected through Y, so it would close a loop.
Result: the tree {XY, YZ} with total weight 3. The interpretation is that the most expensive edge of the triangle is the one left out.
Vertices A, B, C, D and E, with edges AB = 2, AC = 3, BC = 1, BD = 4, CD = 5, CE = 6 and DE = 2. A spanning tree needs 4 edges.
- Sort: BC (1), AB (2), DE (2), AC (3), BD (4), CD (5), CE (6).
- Take BC. Total 1. Pieces: {B, C}, {A}, {D}, {E}.
- Take AB. A joins {B, C} without a loop. Total 3. Pieces: {A, B, C}, {D}, {E}.
- Take DE. Total 5. Pieces: {A, B, C}, {D, E}.
- Skip AC. A and C are already in the same piece.
- Take BD. It joins {A, B, C} with {D, E} and creates no loop. Total 9. Now there is one piece with five vertices and four edges.
- Skip CD and CE. Both would close loops, and the tree is complete.
Result: the tree {BC, AB, DE, BD} with total weight 9. The interpretation is that the cheap edges BC and AB do the work of linking the first three vertices, while the expensive edges are only needed where pieces must be joined.
Five buildings, P, Q, R, S and T, can be linked by these cables, with lengths in metres: ST 80, QR 90, PQ 120, QS 150, RT 170, PR 200 and RS 300. Four cables are needed.
- Sort: ST (80), QR (90), PQ (120), QS (150), RT (170), PR (200), RS (300).
- Take ST. Total 80. Pieces: {S, T}, {P}, {Q}, {R}.
- Take QR. Total 170. Pieces: {S, T}, {P}, {Q, R}.
- Take PQ. P joins {Q, R} without a loop. Total 290. Pieces: {P, Q, R}, {S, T}.
- Take QS. It joins the two pieces with no loop. Total 440. Now all five buildings are connected.
- Skip RT, PR and RS. Each would close a loop, and the tree is complete.
Result: cables ST, QR, PQ and QS, with total length 440 m. The interpretation is that laying these four cables is the cheapest way to make every building reachable from every other.
Try it: suppose QS were 160 m instead of 150 m. Which cable does the algorithm take in step 5, and what is the new total?
Answer: QS is now 160 m, which still comes before RT at 170 m in the sorted order, so step 5 takes QS. It joins {P, Q, R} with {S, T} and creates no loop. The new total is 80 + 90 + 120 + 160 = 450 m.
Common mistakes and tradeoffs
It is tempting to think a minimum spanning tree is the same as the shortest paths from one vertex. In fact the shortest-path tree from one start, which Dijkstra's algorithm builds, minimises the distance from that start to each vertex. The minimum spanning tree minimises the total weight of all edges. The two trees can be different even in the same graph.
It is tempting to think you should always pick the single cheapest edge left and be done. In fact the cheapest edge can close a loop, and taking it then wastes an edge. The loop test is what keeps the result a tree.
It is tempting to think the algorithm needs non-negative weights, as shortest-path methods do. In fact the cut property holds for any real weights, so negative weights are fine for minimum spanning trees.
It is also tempting to think that the running time of the method is just "a few passes over the edges". In fact the sorting step dominates; the big-O cost is set by the sort, not by the loop test.
It is tempting to think the method works on directed graphs. In fact the tree idea used here is for undirected edges. A directed analogue needs a different algorithm.
When all edge weights are different, the minimum spanning tree is unique. With ties, several trees can have the same minimal weight.
Kruskal's method sorts the edges and uses the disjoint-set structure, so it runs in time about for edges. It suits sparse graphs, where there are few edges compared with vertices. Prim's method grows one tree from a start vertex and always adds the cheapest edge leaving it, using a priority queue. It suits dense graphs, where there are many edges. Both give a minimum spanning tree, so the choice is about speed on your graph, not correctness.
History
Engineers have long needed the cheapest way to connect towns and power stations with wire. In 1926 Otakar Borůvka described a method for the efficient wiring of a region in Moravia, the first recorded algorithm for this problem. Vojtěch Jarník found the same method for the minimum tree in 1930, and it is sometimes named after him.
Joseph Kruskal published his edge-sorting method in 1956, with the observation that the cheapest edge that does not close a loop is always safe. Robert Prim published the growing-tree method in 1957. The problem is now a standard part of algorithms courses, and the cut property behind it is the same greedy idea that appears in many other optimisation methods.
Recap
- A spanning tree connects all vertices with edges and no loops; the minimum one has the least total weight.
- Kruskal's method sorts the edges and takes each one that joins two different pieces, which the loop test checks.
- The cut property explains why the cheapest edge crossing a cut is always safe to take.
- Kruskal suits sparse graphs and Prim suits dense ones, and both give the same minimal weight.
- Next, read Single-Linkage Clustering to see the tree used for clustering, and the HDBSCAN paper for a density-based extension.
Check yourself
QuestionRecall: how many edges does a spanning tree of a graph with 7 vertices have?Show answer
It has 6 edges, one fewer than the number of vertices. A spanning tree connects all vertices and has no loops, and a tree on vertices always has edges. Each edge added beyond that would close a loop.
QuestionCompute: a graph has edges AB = 4, BC = 1, AC = 2 and CD = 3, with vertices A, B, C and D. What is the total weight of its minimum spanning tree?Show answer
Sort: BC (1), AC (2), CD (3), AB (4). Take BC, total 1. Take AC, total 3, which joins A to {B, C}. Skip AB, since A and B are now connected through C. Take CD, total 6, which joins D. The tree has 3 edges for 4 vertices, and its total weight is 6.
QuestionWhy is it safe to skip an edge that would close a loop, even if that edge is cheap?Show answer
The cheapest edge that closes a loop is not needed to connect its two ends, because they are already connected through edges that are at most as expensive as it. Taking it would add weight without connecting anything new. The cut property guarantees the edges that are taken are the cheapest safe choices.
QuestionApplied: a network planner has 6 sites and wants the cheapest set of links. Why might the planner prefer Kruskal's method over Prim's on a network with few links and many sites?Show answer
Kruskal's method sorts only the edges that exist, so with few links the sorting is cheap and the disjoint-set checks are quick. Prim's method is designed around a priority queue over the frontier, which pays off when there are many edges around each vertex. Both give the same minimum total, so the planner chooses based on the shape of the network and the cost of running each method.