Sub topic 6
The Generic Greedy MST Strategy
This chapter assumes you know the cut property from the earlier chapter. It also assumes basic Python, edge lists, and Big-O notation. Here we turn the cut property into one template algorithm. Kruskal's and Prim's algorithms are both instances of this template.
The invariant: a set of safe edges
The generic strategy grows a set of edges A one edge at a time. It keeps a single invariant true throughout:
- Invariant:
Ais a subset of some minimum spanning tree.
An edge is safe for A if adding it keeps the invariant true. The algorithm is then easy to state:
- Start with
Aempty. The empty set is trivially part of every MST. - While
Ahas fewer than V − 1 edges, find a safe edge and add it toA. - Return
A.
When the loop ends, A has V − 1 edges and lies inside some MST. Every spanning tree has exactly V − 1 edges, so A is that MST.
How the cut property finds safe edges
The hard part is step 2. The cut property gives a recipe for it:
- Pick any cut (S, V − S) that respects
A, meaning no edge ofAcrosses it. - Take a lightest edge that crosses the cut. That edge is safe.
The easiest cuts that respect A come from the components of the forest A. Take any one component as S. No edge of A leaves its own component, so the cut respects A. The lightest edge leaving that component is safe, and adding it merges two components into one.
A direct implementation
The code below follows the template literally. The pick argument chooses which component becomes S, so you can see that different choices still produce a valid MST.
def generic_mst(n, edges, pick=lambda labels: labels[0]):
# edges: list of (weight, u, v); vertices are 0..n-1
comp = list(range(n)) # comp[v] is the label of v's component in A
mst = []
while len(mst) < n - 1:
labels = sorted(set(comp))
S = pick(labels) # this component defines a cut that respects A
crossing = [(w, u, v) for w, u, v in edges
if (comp[u] == S) != (comp[v] == S)]
if not crossing:
raise ValueError("graph is disconnected")
w, u, v = min(crossing) # lightest crossing edge is safe
mst.append((u, v, w))
old, new = comp[v], comp[u]
comp = [new if c == old else c for c in comp] # merge components
return sum(w for _, _, w in mst), mst
edges = [(1, 0, 1), (4, 0, 2), (3, 1, 2), (2, 1, 3), (5, 2, 3)]
print(generic_mst(4, edges)) # (6, [(0, 1, 1), (1, 3, 2), (1, 2, 3)])
print(generic_mst(4, edges, pick=lambda labels: labels[-1])) # (6, [(1, 3, 2), (1, 2, 3), (0, 1, 1)])Both runs reach total weight 6, but they add the edges in different orders. Each iteration scans every edge and relabels every vertex, so this version costs O(V · (E + V)). The template is correct but slow. Real algorithms make the choice of cut and the search for the lightest edge cheap.
How Prim and Kruskal specialize the template
| Algorithm | Cut it uses | Data structure that makes it fast |
|---|---|---|
| Prim | The single growing tree versus every other vertex | Binary-heap priority queue of crossing edges |
| Kruskal | Whichever component contains an endpoint of the globally lightest edge that joins two different components | Sorted edge list plus a disjoint-set structure |
Kruskal never names its cut explicitly. Suppose the next edge in sorted order joins components C1 and C2. Every lighter edge has already been either added or rejected, so this edge is the lightest one leaving C1. That makes it safe.
Exercise
On the example graph, suppose A = {(0, 1, 1)}. Is S = {0, 2} a cut that respects A? If not, give a respecting cut and its safe edge.
Show answer
No. Edge (0, 1) has one endpoint in S and the other outside it, so it crosses the cut. One cut that respects A is S = {0, 1}. Its crossing edges are (0, 2, 4), (1, 2, 3), and (1, 3, 2), so the lightest crossing edge, (1, 3) with weight 2, is safe.
Exercise
Why does the generic loop always find a crossing edge when the graph is connected and A has fewer than V − 1 edges?
Show answer
With fewer than V − 1 edges, the forest A has at least two components. Pick any one of them as S. The graph is connected, so some edge joins S to a vertex outside it, and that edge crosses the cut.