Sub topic 3
The Minimum Spanning Tree Problem
This chapter assumes you know graph terminology (vertices, edges, paths, cycles), can read an edge list, and are comfortable with basic Python and Big-O notation. It also builds on the previous chapter's result that every spanning tree of a connected graph with V vertices has exactly V − 1 edges.
Stating the problem
You are given a connected, undirected graph G = (V, E) and a weight function w that assigns a real number to each edge. A spanning tree T is a subset of E that connects all vertices and contains no cycle. Its weight is the sum of its edge weights:
- w(T) = Σ w(e) for every edge e in T
A minimum spanning tree (MST) is a spanning tree whose weight is as small as possible among all spanning trees of G. The problem is to find one.
Some points about this definition:
- Undirected. On a directed graph, the matching problem is the minimum spanning arborescence. It needs a different algorithm (Chu–Liu/Edmonds).
- Connected. If G is disconnected, no spanning tree exists. A later chapter covers the minimum spanning forest.
- "A" minimum, not "the" minimum. Several spanning trees can share the minimum weight. A later chapter covers when the MST is unique.
- Negative weights are allowed. Every spanning tree has exactly V − 1 edges, so negative weights do not let a tree "cheat" by adding extra edges. Adding a constant to every weight changes every tree's total by the same amount and leaves the set of MSTs unchanged.
A worked example
The tutorial uses this four-vertex graph throughout.
Five edges give C(5, 3) = 10 ways to choose three of them. Two of those choices are triangles: {0-1, 0-2, 1-2} and {1-2, 1-3, 2-3}. Each triangle contains a cycle and leaves one vertex out, which leaves 8 spanning trees. The brute-force code below checks every one of them.
from itertools import combinations
edges = [(1, 0, 1), (4, 0, 2), (3, 1, 2), (2, 1, 3), (5, 2, 3)] # (weight, u, v)
n = 4
def connects_all(n, chosen):
adj = {v: [] for v in range(n)}
for _, u, v in chosen:
adj[u].append(v)
adj[v].append(u)
seen, stack = {0}, [0]
while stack:
x = stack.pop()
for y in adj[x]:
if y not in seen:
seen.add(y)
stack.append(y)
return len(seen) == n
# With exactly n-1 edges, "connects all vertices" implies "is a tree".
trees = [c for c in combinations(edges, n - 1) if connects_all(n, c)]
best = min(trees, key=lambda t: sum(w for w, _, _ in t))
print(len(trees)) # 8
print(sum(w for w, _, _ in best), best) # 6 ((1, 0, 1), (3, 1, 2), (2, 1, 3))The MST is {0-1, 1-2, 1-3}, with weight 1 + 3 + 2 = 6.
Why brute force does not scale
Cayley's formula says a complete graph on V vertices has V^(V−2) spanning trees. For V = 20 that is about 2.6 × 10²³ trees. Enumerating them is hopeless. The chapters that follow show that a greedy approach finds an MST in O(E log V) time. The approach works because the problem has a special structure, which the cut and cycle properties describe.
Common confusions
| Mistake | Why it is wrong |
|---|---|
| Treating the MST as a shortest-path tree | An MST minimizes total edge weight. A shortest-path tree minimizes each vertex's distance from one source. In the example, the MST path from 0 to 2 costs 1 + 3 = 4, which ties the direct edge. On other graphs the MST path can be strictly longer. |
| Picking the V − 1 lightest edges | Those edges can form a cycle and leave a vertex out. |
| Running MST algorithms on directed graphs | Edge direction breaks the theory behind them. Use an arborescence algorithm instead. |
| Assuming a disconnected graph has an MST | It has a minimum spanning forest instead. |
Exercise
You multiply every edge weight in a connected graph by 2 and then add 10. Does the set of minimum spanning trees change? What happens to the MST weight in the example graph?
Show answer
The set of MSTs does not change. Every spanning tree has V − 1 edges, so each tree's new weight is 2·w(T) + 10(V − 1). This transformation preserves the order of the trees by weight. In the example the MST weight becomes 2·6 + 10·3 = 42.
Exercise
In the example graph, the 3 lightest edges are 0-1 (1), 1-3 (2) and 1-2 (3). Here they happen to form the MST. Give a small graph where the V − 1 lightest edges do not form a spanning tree.
Show answer
Take vertices 0, 1, 2, 3 with edges 0-1 (1), 1-2 (1), 0-2 (1) and 2-3 (10). The 3 lightest edges form the triangle 0-1-2 and leave vertex 3 disconnected. The MST must include 2-3, giving, for example, {0-1, 1-2, 2-3} with weight 12.