Sub topic 19
Spanning trees and the cut property
This chapter assumes you know basic graph terms (vertices, edges, weights, connectivity), how greedy proofs work, and how to read short Python. It builds the theory that Kruskal's and Prim's algorithms rely on. The next chapter covers the algorithms themselves.
Spanning trees
A spanning tree of a connected, undirected graph is a subgraph that contains every vertex, is connected, and has no cycle. For a graph with V vertices, every spanning tree has exactly V - 1 edges. Adding any edge to a spanning tree creates exactly one cycle. Removing any edge disconnects it.
A minimum spanning tree (MST) is a spanning tree whose total edge weight is smallest among all spanning trees. Three facts matter in exam questions:
- A connected graph always has at least one MST. A disconnected graph has a spanning forest instead.
- The MST need not be unique. If all edge weights are distinct, the MST is unique.
- All MSTs of a graph have the same total weight. They also share the same sorted multiset of edge weights.
A complete graph on V vertices has V^(V-2) spanning trees (Cayley's formula). For V = 4 that is 16, which is why you never enumerate them.
Cuts and crossing edges
A cut is a partition of the vertices into two non-empty sets, S and V \ S. An edge crosses the cut if one endpoint is in S and the other is not. A cut respects a set of edges A if no edge of A crosses it. A crossing edge with the smallest weight among all crossing edges is a light edge for that cut.
The cut property
Cut property. Let A be a subset of some MST. Let (S, V \ S) be any cut that respects A. Let e be a light edge crossing that cut. Then A ∪ {e} is also a subset of some MST.
This statement is the greedy-choice property for MST. If you have a partial answer that can still be extended to an optimal tree, you may add the cheapest edge leaving any cut that respects it, and the partial answer remains extendable.
Proof by exchange argument
Let T be an MST containing A. If e is in T, there is nothing to prove. Otherwise:
- Adding e to T creates exactly one cycle.
- The cycle contains e, which crosses the cut. A cycle crosses a cut an even number of times, so it contains at least one other edge f that also crosses the cut.
- Because the cut respects A, f is not in A.
- Since e is light, w(e) ≤ w(f).
- Let T' = T - {f} + {e}. It is still a spanning tree, with weight w(T) - w(f) + w(e) ≤ w(T).
- T is minimum, so T' is also an MST. It contains A ∪ {e}.
The cycle property
The cut property has a companion. In any cycle, if one edge is strictly heavier than all other edges on that cycle, that edge belongs to no MST. This property explains why Kruskal's algorithm may discard an edge that closes a cycle. A related exam fact: if an edge is the unique lightest edge crossing some cut, it belongs to every MST.
Checking the cut property in code
The following program finds an MST by brute force on a small graph. It then checks that the light edge of a cut belongs to an MST.
from itertools import combinations
def is_spanning_tree(n, edges):
parent = list(range(n))
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
for u, v, _ in edges:
ru, rv = find(u), find(v)
if ru == rv:
return False
parent[ru] = rv
return True
def all_msts(n, edges):
best, found = float('inf'), []
for subset in combinations(edges, n - 1):
if is_spanning_tree(n, subset):
total = sum(w for _, _, w in subset)
if total < best:
best, found = total, [subset]
elif total == best:
found.append(subset)
return best, found
edges = [(0, 1, 1), (0, 2, 5), (1, 2, 2), (1, 3, 7), (2, 3, 3)]
best, msts = all_msts(4, edges)
print(best, len(msts))
S = {0, 1}
crossing = [e for e in edges if (e[0] in S) != (e[1] in S)]
light = min(crossing, key=lambda e: e[2])
print(light, all(light in t for t in msts))The output is 6 1 and then (1, 2, 2) True. The MST has weight 6 and is unique because the weights are distinct. The light edge of the cut {0, 1} versus {2, 3} is (1, 2, 2), and it is in the MST.
Common errors
- Using a cut that does not respect A. If a chosen edge already crosses the cut, the exchange step may swap out an edge you need to keep.
- Assuming the MST gives shortest paths. An MST minimizes total weight. It does not minimize the distance between any particular pair of vertices.
- Assuming a unique MST under ties. With equal weights, several MSTs can exist. Questions asking "how many MSTs" need careful counting over tied edges.
- Forgetting the connectivity requirement. The V - 1 edge count only holds for connected graphs.
- Forgetting negative weights. MST algorithms still work with negative weights, since only the order of weights matters. Adding a constant to all weights does not change which trees are minimum, because every spanning tree has V - 1 edges.
Why this matters for the algorithms
Kruskal's algorithm applies the cut property to the cut formed by one component of the current forest against the rest of the graph. Prim's algorithm applies it to the cut between the growing tree and the remaining vertices. Both are correct for the same reason, so a single proof covers both.
Exercise
A connected graph has 6 vertices, and edge e = (u, v) of weight 9 lies on a cycle whose other edges have weights 3, 4, 4, and 7. Can e be in an MST? Explain.
Show answer
No. On that cycle, e is strictly heavier than every other edge (9 > 7). By the cycle property, e is in no MST. In an exchange argument, if an MST contained e, removing it splits the tree into two parts. Some other cycle edge reconnects the parts at lower weight, so the swap gives a lighter spanning tree, which contradicts minimality.
Exercise
A connected graph has 8 vertices and 15 edges. How many edges must be removed to obtain a spanning tree, and how many edges does each MST have?
Show answer
Every spanning tree has V - 1 = 7 edges, so 15 - 7 = 8 edges must be removed. Each MST has exactly 7 edges.