Why does the greedy algorithm work for trees? — What matroids reveal
Kruskal's algorithm for minimum spanning trees is absurdly simple: sort the edges by weight and add the cheapest one that does not create a cycle. Why does this greedy strategy produce an optimal solution? The answer lies in the cut property and, more deeply, in matroids.
In an algorithms course, the minimum spanning tree (MST) problem brings a moment of genuine wonder. Kruskal's algorithm is astonishingly simple: "examine edges in order of increasing weight; accept an edge if it does not create a cycle." That is all.
But a moment's reflection should make you uneasy. Greedy algorithms do not, in general, produce optimal solutions. So why does the greedy approach work for MSTs? This article gives an intuitive answer, first through the cut property, and then through the deeper structure of matroids.
1 The minimum spanning tree problem
In this example, choosing edges (weight 1), (weight 2), and (weight 3) yields an MST of total weight 6. Kruskal's algorithm produces exactly this selection.
2 Kruskal's algorithm – greedy in its purest form
The algorithm is disarmingly simple:
Sort all edges in non-decreasing order of weight.
Iterate through the sorted edges; accept an edge if adding it does not create a cycle.
Stop after accepting edges.
The real question, however, is why does this naive strategy produce an optimal solution?
3 When greedy fails – the knapsack problem
To appreciate why the MST result is remarkable, let us first see a case where greediness fails.
Item A: weight 6, value 8 (value density )
Item B: weight 5, value 7 (value density )
Item C: weight 5, value 6 (value density )
The fundamental reason greedy fails here is that the locally best choice need not be globally best. So why does this trap not ensnare the MST problem?
4 The cut property – the intuitive reason greedy works
The most intuitive explanation for the correctness of Kruskal's algorithm is the cut property.
At the moment Kruskal's algorithm considers an edge , if and lie in different connected components, those two components define a cut. Because edges are processed in non-decreasing order of weight, the first edge encountered that crosses this cut is the lightest such edge. The cut property therefore guarantees its inclusion in an MST.
5 Matroids – the structure behind greedy optimality
There is, in fact, a deeper mathematical reason that the greedy algorithm succeeds on MSTs.
(the empty set is independent).
If and , then (the hereditary property).
If and , then there exists such that (the exchange axiom).
6 The correctness of Kruskal's algorithm – a summary
We can now understand why Kruskal's algorithm is optimal at two levels:
Cut property level: Because edges are processed in non-decreasing order of weight, the lightest edge across each cut is always selected.
Matroid level: Forests (acyclic edge sets) form a matroid, and the greedy algorithm is always optimal on matroids.
In the next article, we examine the most dramatic complexity gap in graph theory: the chasm between Euler and Hamilton.
Mathematics "between the lines" — exploring the intuition textbooks leave out, written in LaTeX on Folio.