Search

Search papers and lessons

Minimum Spanning Tree

Level 2 · 12 min read

A minimum spanning tree links every point of a weighted network using the least total edge weight, with no loops.

On this page

  • Colour = topic mix
  • Algorithms
  • Discrete math
  • Calculus
  • ReadReady to readNot yet

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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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

Definition: Spanning tree

Let G=(V,E)G = (V, E) be a connected graph, where VV is the set of vertices and EE the set of edges, with a weight w(e)w(e) on each edge ee (a number, such as a length or a cost). A spanning tree is a subset T⊆ET \subseteq E such that the graph (V,T)(V, T) 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 nn vertices has exactly n−1n - 1 edges.

The weight of a tree is w(T)=∑e∈Tw(e)w(T) = \sum_{e \in T} w(e), the sum of its edge weights. A minimum spanning tree is a spanning tree T∗T^\ast with w(T∗)≤w(T)w(T^\ast) \le w(T) for every spanning tree TT.

Step 2 in symbols: the loop test

Adding an edge e=(u,v)e = (u, v) to a forest creates a cycle exactly when uu and vv are already connected through chosen edges. The procedure keeps track of which vertices are connected using the disjoint-set union structure, which answers "are uu and vv in the same piece?" quickly. If the answer is yes, the edge is skipped.

Step 3 in symbols: the growing forest

After kk accepted edges the chosen set FkF_k is a forest with n−kn - k connected pieces. Each step adds one edge that joins two different pieces, so FkF_k always has kk edges. In the example, after step 3, F2={A-B,B-C}F_2 = \{A\text{-}B, B\text{-}C\}, with n−k=4−2=2n - k = 4 - 2 = 2 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 SS and V∖SV \setminus S. An edge crosses the cut when one end is in SS and the other is in V∖SV \setminus S.

Cut property. Suppose a set of edges FF belongs to some minimum spanning tree, and let ee be the cheapest edge that crosses a cut no edge of FF crosses. Then F∪{e}F \cup \{e\} also belongs to some minimum spanning tree.

Sketch of the proof. Let TT be a minimum spanning tree containing FF but not ee. Adding ee to TT creates a cycle. That cycle crosses the cut at least twice, so it contains some other edge ff that crosses the cut. Because ee is the cheapest crossing edge, w(e)≤w(f)w(e) \le w(f). Swapping ff for ee gives a spanning tree with weight at most w(T)w(T), so it is also minimal, and it contains F∪{e}F \cup \{e\}.

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

Example
1. A triangle

Three vertices X, Y and Z, with edges XY = 1, YZ = 2 and XZ = 3. A spanning tree needs 2 edges.

  1. Sort: XY (1), YZ (2), XZ (3).
  2. Take XY. No loop. Total 1.
  3. Take YZ. Z was alone, so no loop. Total 3.
  4. 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.

Example
2. A standard five-vertex network

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.

  1. Sort: BC (1), AB (2), DE (2), AC (3), BD (4), CD (5), CE (6).
  2. Take BC. Total 1. Pieces: {B, C}, {A}, {D}, {E}.
  3. Take AB. A joins {B, C} without a loop. Total 3. Pieces: {A, B, C}, {D}, {E}.
  4. Take DE. Total 5. Pieces: {A, B, C}, {D, E}.
  5. Skip AC. A and C are already in the same piece.
  6. 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.
  7. 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.

Example
3. Cabling five buildings

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.

  1. Sort: ST (80), QR (90), PQ (120), QS (150), RT (170), PR (200), RS (300).
  2. Take ST. Total 80. Pieces: {S, T}, {P}, {Q}, {R}.
  3. Take QR. Total 170. Pieces: {S, T}, {P}, {Q, R}.
  4. Take PQ. P joins {Q, R} without a loop. Total 290. Pieces: {P, Q, R}, {S, T}.
  5. Take QS. It joins the two pieces with no loop. Total 440. Now all five buildings are connected.
  6. 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 Elog⁡EE \log E for EE 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 n−1n - 1 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 nn vertices always has n−1n - 1 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.

Sources