Greedy Algorithms/Matroids & Exchange Arguments

Lesson 7.42,038 words

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 M=(S,I)M=(S,\mathcal{I}), 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.)

The exchange property — any smaller independent set can absorb some from a larger one

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.

Algorithm:Greedy(M=(S,I),w)\textsc{Greedy}(M=(S,\mathcal I),\, w) — maximum-weight independent set
  1. 1
    AA \gets \varnothing
  2. 2
    sort SS into nonincreasing order by weight ww
  3. 3
    for each xSx \in S in sorted order do
  4. 4
    if A{x}IA \cup \{x\} \in \mathcal{I} then
    independence test
  5. 5
    AA{x}A \gets A \cup \{x\}
  6. 6
    return AA

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 Rado–Edmonds exchange step. Greedy's picks and an allegedly better base's picks , both in nonincreasing weight, agree until index where (red). The exchange property on and yields a heavier independent extension greedy would have taken first.

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.

Acyclic edge sets form a matroid — Kruskal is matroid-greedy, taking edges by weight and rejecting any that close a cycle

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 uniform matroid : independent means . Greedy sorts by weight descending and takes the first (blue), which is trivially the maximum-weight base; the exchange property holds because any larger set always has a spare element.

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.

0/1 knapsack is not a matroid (). The independent set and the larger both fit, yet adding either to gives . No element of the larger set can grow the smaller, so the exchange property fails.

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

  1. 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.
  2. 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.
Practice

╌╌ END ╌╌