Matroids & Exchange Arguments
The capstone of the greedy module: why and when a greedy algorithm is provably optimal. We recap the two correctness templates — greedy-stays-ahead and the exchange argument — then meet the matroid , an abstraction whose exchange property is exactly the structure greedy needs.
╌╌╌╌
Across this module we have watched greedy algorithms win (activity selection,
Huffman codes, Kruskal's and Prim's spanning trees) and watched greedy lose, on
the 0/1 knapsack. Each victory came with a proof: a guarantee that taking the
locally best option never forecloses the global optimum. This lesson is about the
proofs themselves. First we distill the two arguments every greedy correctness
proof is built from. Then we ask the deeper question: is there a structural
property that predicts when greedy will work, before we attempt the proof? The
answer is yes, and its name is the matroid. The matroid theory makes precise
the boundary we kept bumping into, why Kruskal is correct and 0/1 knapsack is
not, and reduces is greedy optimal here?
to is this structure a matroid?
Two templates for proving greedy optimal
Every greedy correctness proof in this module is an instance of one of two patterns. They are interchangeable in power but feel different to wield.
This is the activity-selection argument: greedy's -th activity finishes no later than the -th activity of any valid schedule, so greedy always has at least as much room left for future activities and ends up with at least as many.
This is the one you reach for: it proves the greedy-choice property by showing that whatever an optimal solution does first, we may exchange it for greedy's first choice without harm.1 The two templates differ in direction: stays-ahead pushes greedy forward and shows it never falls behind; exchange pulls an optimum toward greedy and shows the pull never hurts. The exchange argument is the one that generalizes into a theory, because its swap step is literally an axiom of the structure we are about to define.
Matroids
A matroid abstracts the notion of independence
: linear independence of
vectors, acyclicity of edges, freedom to add one more element without breaking a
rule.
The first axiom is mild; most natural notions of independence are closed under taking subsets. The exchange property is the engine: a smaller independent set can always be grown by stealing some element from any larger one. It forbids the situation where you get stuck early with a maximal-but-small independent set while a much larger one exists.
A first consequence of the exchange property is that all bases have the same
size: if two bases had , the exchange property would let us add
an element of to , contradicting that is maximal. So rank
is
well-defined: every base has exactly elements, exactly as every basis of a
vector space has the same dimension. (Indeed, the columns of a matrix, with
the linearly independent subsets, form the linear matroid; this
is where the vocabulary comes from.)
Because , some (here ) joins keeping it independent, so no maximal independent set is left stranded below the rank.
The matroid–greedy theorem
A weighted matroid attaches a positive weight to each element , with . The natural optimization problem: find a maximum-weight independent set. Since weights are positive and is hereditary, a maximum-weight independent set is always a base. The greedy algorithm is the obvious one: sort by weight descending, add each element if it keeps the set independent.
- 1
- 2sort into nonincreasing order by weight
- 3for each in sorted order do
- 4if thenindependence test
- 5
- 6return
The forward direction is a clean exchange argument; it is worth seeing in full, because it is the abstract skeleton of every concrete greedy proof in this module.
Laid out side by side, greedy's picks and the optimum's, both sorted by weight, make the contradiction visible. They agree on weight up to the first index where greedy falls behind (). The prefix is smaller than , so the exchange property hands us an element that keeps independent and is heavier than — which greedy, scanning in weight order, must have reached and accepted before . That is the contradiction.
The single step that does all the work is the exchange property gives an element .
That is the matroid axiom standing in for the ad-hoc swap we constructed
by hand for activity selection. The converse, that a non-matroid hereditary
structure has a weight function defeating greedy, is what makes the matroid the
exact characterization, not merely a sufficient condition: if the exchange
property fails for some , one can place weights that lure greedy into the
stranded maximal set and away from the heavier .
Examples that are matroids
The graphic matroid. Let be a graph. Take and let be the acyclic edge sets, the forests of . This is a matroid: a subset of a forest is a forest (hereditary), and if forests have , then touches more components, so some edge of joins two trees of without creating a cycle (exchange property). The bases are the spanning forests; for a connected graph, the spanning trees, all of size .
Now run on the graphic matroid with negated edge weights
(maximum-weight independent set becomes minimum-weight spanning tree): sort edges,
add each edge that does not form a cycle. That is Kruskal's algorithm,
exactly: the independence test stays acyclic
is the
union-find
cycle check from the minimum spanning trees
lesson. So Kruskal's correctness is not a special miracle; it is the
Rado–Edmonds theorem instantiated on the graphic matroid.
Edges are accepted in weight order (blue); edge is rejected because and are already connected, so adding it would close the cycle , leaving . The accepted set is a base: a spanning tree.
The uniform / partition matroid. Even simpler: fix and let
, so every set of size at most is
independent. This uniform matroid is plainly hereditary, and the
exchange property is trivial (any larger set has a spare element). Greedy reduces
to pick the heaviest elements,
which is obviously optimal; the matroid
machinery confirms the trivial.
The partition matroid generalizes it: partition
into groups and allow at most elements from group ; independence is
within every group's cap.
Scheduling and assignment constraints of the form no more than of this kind
are partition-matroid constraints, which is why greedy
solves so many of them.
Beyond greedy: matroid intersection. A single matroid yields to greedy; the
common independent sets of two matroids over the same ground set do not, in
general. Still, matroid intersection solves the maximum-weight common
independent set in polynomial time by augmenting-path methods (bipartite matching
is a special case). The intersection of three matroids is already NP-hard. So
the matroid is the precise frontier of greedy works
; one step beyond it you need
heavier machinery.
Why 0/1 knapsack and TSP are not matroids
The theorem cuts both ways, and it explains the failures we have already seen.
0/1 knapsack. Recall from the greedy method
lesson that greedy fails on the 0/1 knapsack, and from the
knapsack DP lesson that it
needs dynamic programming. The reason, in matroid language: let be the items
and call a set independent
if its total weight fits in capacity . This is
hereditary: drop items and it still fits. But the exchange property fails.
Take capacity and item sizes giving the independent sets and
. Both fit, and , yet no element of
can join , since . The smaller independent set is
stranded: it cannot grow toward the larger one, which is the exact pattern the
exchange property forbids.
The exchange property is violated; the structure is not a matroid; and by Rado–Edmonds some weighting must defeat greedy, which is exactly the value/density counterexample from the greedy-method lesson.
Traveling salesman. For the TSP, let be the edge sets that extend to a Hamiltonian tour (or the partial-tour fragments greedy builds). This is not even reliably hereditary, and the exchange property collapses badly: a cheap partial path can be a dead end that no edge of a longer, valid fragment can rescue without revisiting a vertex or exceeding degree . There is no matroid, so no weighting guarantee, and indeed nearest-neighbor greedy can be made arbitrarily bad. TSP's hardness is not an accident of greed; the independence structure simply isn't a matroid (and TSP is NP-hard regardless).
The pattern is uniform. When greedy is provably optimal, you can almost always exhibit a matroid behind it. When greedy fails, the exchange property is the axiom that breaks.
Takeaways
- Every greedy correctness proof is a greedy-stays-ahead induction (greedy's -th partial solution is never behind any rival's) or an exchange argument (transform any optimum into the greedy solution without loss). They are equivalent in power; exchange is the one that abstracts into a theory.
- A matroid is a ground set plus a hereditary family of independent sets satisfying the exchange property: any smaller independent set can absorb some element of any larger one. Maximal independent sets are bases; they all share size , the rank.
- Rado–Edmonds theorem: on a weighted matroid, sorting by weight and greedily keeping independence yields a maximum-weight base, and greedy is optimal on a hereditary structure iff it is a matroid. The proof's one nontrivial step is precisely the exchange axiom.
- The graphic matroid (acyclic edge sets) makes Kruskal's MST an exact
instance of matroid-greedy; the uniform / partition matroid captures
at most of each kind
constraints. Matroid intersection is the polynomial-time frontier just beyond single-matroid greedy. - 0/1 knapsack and TSP are not matroids (the exchange property fails), which is the structural reason greedy fails on them and dynamic programming or exact search is required instead.
Footnotes
- CLRS, Ch. 16 — Greedy Algorithms (§16.2): the greedy-choice property and optimal substructure, proved by exchanging an optimal solution's first choice for greedy's. ↩
- CLRS, Ch. 16 — Greedy Algorithms (§16.4): the Rado–Edmonds theorem — greedy returns a maximum-weight independent set exactly when the structure is a matroid; the proof rests on the exchange property. ↩
╌╌ END ╌╌