Sub topic 3
The Minimum Spanning Tree Problem
This chapter assumes you know graph vocabulary (vertices, edges, paths, cycles), can read edge lists and adjacency lists, know Big-O notation, and can follow basic Python. It also builds on the two previous chapters, which covered weighted undirected graphs and spanning trees. In particular, it uses the fact that a spanning tree of a graph with V vertices has exactly V − 1 edges.
The goal here is to state the problem precisely, solve one small instance by exhaustive search, and look at the details that trip people up. The cut and cycle properties and the actual algorithms come in later chapters.
The formal statement
The input is a connected, undirected graph G = (V, E) with a weight function w that assigns a real number w(e) to each edge e.
The output is a spanning tree T ⊆ E that minimizes the total weight:
w(T) = Σ w(e) for all e in T
Any spanning tree with the smallest possible total weight is a minimum spanning tree (MST). The definition says "a" minimum spanning tree because one graph can have several trees that tie for the minimum. A later chapter covers when the MST is unique.
The constraints come in two parts:
- Feasibility. T must touch every vertex, stay connected, and contain no cycle. Equivalently, T has V − 1 edges and no cycle.
- Optimality. No other feasible T′ has w(T′) < w(T).
Why ask for a tree at all?
In the usual setting the requirement is "connect everything as cheaply as possible," and with positive weights the cheapest connected spanning subgraph is automatically a tree. Suppose a connected subgraph contained a cycle. Removing any edge of that cycle keeps the subgraph connected and lowers its weight, so the subgraph could not have been the cheapest.
With zero or negative weights that argument fails. If an edge has weight −3, adding it to a connected subgraph makes the subgraph cheaper even when it closes a cycle. The MST problem still asks for a tree, so negative weights are allowed, but the answer is then the cheapest tree and no longer the cheapest connected subgraph. If you actually need the cheapest connected subgraph, take every negative edge and then find an MST of what remains, with the components formed by the negative edges treated as single vertices.
A worked instance by exhaustive search
This chapter uses the same four-vertex graph as the rest of the tutorial:
| Edge | Endpoints | Weight |
|---|---|---|
| a | 0–1 | 1 |
| b | 0–2 | 4 |
| c | 1–2 | 3 |
| d | 1–3 | 2 |
| e | 2–3 | 5 |
A spanning tree needs V − 1 = 3 edges. There are C(5, 3) = 10 ways to choose 3 of the 5 edges. Two of those choices are triangles: {a, b, c} on vertices 0-1-2 and {c, d, e} on vertices 1-2-3. Neither is a tree, which leaves 8 spanning trees:
| Tree | Edges | Total weight |
|---|---|---|
| a, c, d | 0–1, 1–2, 1–3 | **6** |
| a, b, d | 0–1, 0–2, 1–3 | 7 |
| a, d, e | 0–1, 1–3, 2–3 | 8 |
| a, c, e | 0–1, 1–2, 2–3 | 9 |
| b, c, d | 0–2, 1–2, 1–3 | 9 |
| a, b, e | 0–1, 0–2, 2–3 | 10 |
| b, d, e | 0–2, 1–3, 2–3 | 11 |
| b, c, e | 0–2, 1–2, 2–3 | 12 |
The MST is {a, c, d} with weight 6. Nothing else ties with it, so for this graph the MST is unique.
The same search in code is a useful reference implementation for testing faster algorithms. It tries every subset of V − 1 edges and keeps the ones with no cycle. A set of V − 1 edges with no cycle on V vertices has to be a spanning tree, so the code doesn't need a separate connectivity check.
from itertools import combinations
def is_spanning_tree(n, chosen):
# chosen: tuple of (weight, u, v) edges; checks for cycles with a tiny union-find
parent = list(range(n))
def find(x):
while parent[x] != x:
x = parent[x]
return x
for _, u, v in chosen:
ru, rv = find(u), find(v)
if ru == rv:
return False # this edge closes a cycle
parent[ru] = rv
return len(chosen) == n - 1
def brute_force_mst(n, edges):
count = 0
best = None
for chosen in combinations(edges, n - 1):
if is_spanning_tree(n, chosen):
count += 1
total = sum(w for w, _, _ in chosen)
if best is None or total < best[0]:
best = (total, chosen)
return count, best # best is None if the graph is disconnected
edges = [(1, 0, 1), (4, 0, 2), (3, 1, 2), (2, 1, 3), (5, 2, 3)]
print(brute_force_mst(4, edges))
# (8, (6, ((1, 0, 1), (3, 1, 2), (2, 1, 3))))Why brute force does not scale
The number of spanning trees grows very fast. Cayley's formula says the complete graph on n vertices has n^(n−2) spanning trees. With 10 vertices that is 10^8 trees. With 20 vertices it is about 2.6 × 10^23. The code above is worse still, because it examines C(E, V − 1) subsets and most of them are not trees.
Kruskal's and Prim's algorithms solve the problem in O(E log V) time. They never list trees. They build one tree an edge at a time and rely on structural facts that guarantee each chosen edge belongs to some MST. The next two chapters cover those facts.
What the objective is sensitive to
A useful fact about MSTs is that which tree is minimal depends only on the relative order of the edge weights, not on their actual values. You will see why once you have the cut property. Kruskal's algorithm only ever sorts and compares weights, and it always produces an MST.
This fact has practical consequences:
- Adding a constant to every weight leaves the MST unchanged. Every spanning tree has exactly V − 1 edges, so every tree's total goes up by the same amount.
- Any strictly increasing transform also leaves it unchanged, such as multiplying by a positive number, taking logs of positive weights, or squaring non-negative weights. The totals change, but the set of optimal trees stays the same.
- Negative weights are fine. Dijkstra's algorithm breaks on negative edges. MST algorithms do not.
- Maximum spanning tree. Negate every weight, or sort in descending order, and run any MST algorithm.
- Bottleneck bonus. Every MST is also a minimum bottleneck spanning tree: no other spanning tree has a smaller maximum edge weight. The reverse does not hold.
MST versus shortest-path tree
The most common confusion is treating an MST as a shortest-path structure. The two problems optimize different things:
- An MST minimizes the sum of all edges in the tree.
- A shortest-path tree from a source s, which is what Dijkstra's algorithm builds, minimizes each vertex's distance from s.
Take a triangle with edges A–B = 2, B–C = 2, A–C = 3:
| Structure | Edges | Total weight | Distance A→C in tree |
|---|---|---|---|
| MST | A–B, B–C | 4 | 4 |
| Shortest-path tree from A | A–B, A–C | 5 | 3 |
Neither structure is better in general. The MST is the cheapest way to connect everything, while the shortest-path tree gives the fastest routes from one source. The path between two vertices inside an MST can be much longer than their shortest path in the graph.
Common errors in stating or applying the problem
| Mistake | What actually happens |
|---|---|
| Running an MST algorithm on a directed graph | MSTs are defined for undirected graphs. The directed version is the minimum spanning arborescence, which needs a different algorithm (Chu–Liu/Edmonds). |
| Assuming the input is connected | A disconnected graph has no spanning tree. Kruskal quietly returns a forest, and Prim covers only the start vertex's component. A later chapter covers minimum spanning forests. |
| Expecting one specific tree as "the" answer | When weights tie, several MSTs can exist. Test with total weight, or break ties deterministically. |
| Treating MST paths as shortest paths | See the triangle example above. |
| Ignoring self-loops and parallel edges | A self-loop is a cycle by itself and is never in any spanning tree. Of several parallel edges, only the lightest can be in an MST. Kruskal and Prim both handle these correctly without preprocessing. |
| Using the cheapest connected subgraph and the MST interchangeably with negative weights | They coincide only when all weights are positive. |
Exercises
Exercise
In the example graph, change the weight of edge 0–2 from 4 to 2. Which spanning tree or trees are now minimum, and what is the total weight?
Show answer
Tree {0–1, 0–2, 1–3} now weighs 1 + 2 + 2 = 5. Every other tree containing 0–2 weighs more: {a, b, e} = 8, {b, c, d} = 7, {b, d, e} = 9, {b, c, e} = 10. The best tree without 0–2 is still {a, c, d} = 6. So the unique MST is {0–1, 0–2, 1–3} with total weight 5.
Exercise
A colleague squares every edge weight before running Kruskal's algorithm, because their weights are distances and they want to "penalize long links more." The weights are all positive. Does the chosen tree change?
Show answer
No. Squaring is strictly increasing on positive numbers, so it keeps the order of the edges, and the MST depends only on that order. The reported total changes, but the set of minimum trees stays the same. To penalize long links in a way that changes the tree, they need a different objective, not a monotone transform of the weights.
Exercise
A graph has 6 vertices and every pair of vertices is joined by an edge. How many spanning trees does it have, and how many 5-edge subsets would the brute-force code examine?
Show answer
By Cayley's formula there are 6^4 = 1296 spanning trees. The complete graph has C(6, 2) = 15 edges, so the code examines C(15, 5) = 3003 subsets and rejects the 1707 that contain a cycle.
Exercise
Give a graph with an edge of negative weight where the cheapest connected spanning subgraph is not a spanning tree.
Show answer
Use a triangle with edges A–B = 1, B–C = 1, A–C = −1. Its MST is {A–C, A–B} or {A–C, B–C}, each with weight 0. Taking all three edges gives a connected subgraph of weight 1 + 1 − 1 = 1, which is more expensive, so that version doesn't work. Change A–B to −1 instead, giving A–B = −1, B–C = 1, A–C = −1. The MST is {A–B, A–C} with weight −2. A 4-cycle with edges −1, −1, −1, 5 is still not enough, since dropping the 5 gives −3, which is cheaper than all four edges at 2. The simplest valid example has all edges negative: a triangle with every edge −1. Every MST weighs −2, but the full triangle, which contains a cycle, weighs −3.