Sub topic 16
Matroids: Why Greedy Works When It Works
This chapter assumes you know Kruskal's algorithm, the exchange argument, and the coin-change and 0/1 knapsack counterexamples from earlier chapters. Each greedy proof so far was written for one problem. Matroids describe a whole class of problems where the simple greedy rule is always optimal, whatever the weights are.
The definition
A matroid is a pair (E, I). E is a finite set of elements, and I is a family of subsets of E called independent sets. The family must satisfy three rules:
- Empty set: the empty set is independent.
- Hereditary: every subset of an independent set is independent.
- Exchange: if A and B are independent and |A| < |B|, then some element x in B but not in A keeps A ∪ {x} independent.
The exchange rule does most of the work. It means every maximal independent set has the same size. A maximal independent set is called a basis. Because all bases are the same size, an early greedy choice can never leave you stuck with a smaller solution than you could have had.
The generic greedy algorithm
Sort the elements by weight, heaviest first. Add each element if the set stays independent. You only need an independence oracle, which is a function that answers "is this set independent?"
def matroid_greedy(elements, weight, is_independent):
chosen = []
for x in sorted(elements, key=weight, reverse=True):
if weight(x) > 0 and is_independent(chosen + [x]):
chosen.append(x)
return chosen
def is_forest(edges):
parent = {}
def find(x):
parent.setdefault(x, x)
while parent[x] != 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
edges = [(0, 1, 5), (1, 2, 4), (0, 2, 3), (2, 3, 2)]
print(matroid_greedy(edges, lambda e: e[2], is_forest))
# [(0, 1, 5), (1, 2, 4), (2, 3, 2)]In this example, the elements are edges and the independent sets are forests, which are sets of edges with no cycle. This structure is called the graphic matroid. Running the generic greedy on it gives Kruskal's algorithm. To get a minimum spanning tree, use the weight C - w for a constant C larger than every edge weight.
The Rado–Edmonds theorem
The theorem says that if a family of sets satisfies the first two rules, then greedy finds a maximum-weight independent set for every weight function exactly when the family also satisfies the exchange rule. So the exchange rule is necessary as well as sufficient. If it fails, some choice of weights makes greedy produce a wrong answer.
| Structure | Elements | Independent sets | Matroid? |
|---|---|---|---|
| Graphic | Graph edges | Forests | Yes |
| Uniform U(k, n) | n items | Any set of at most k items | Yes |
| Partition | Items in categories | At most a fixed number per category | Yes |
| Interval scheduling | Intervals | Pairwise non-overlapping sets | No |
| 0/1 knapsack | Items | Sets within the weight capacity | No |
Common mistakes
- Checking only the hereditary rule. Almost every "pick a feasible subset" problem is hereditary. The exchange rule is the one that has to be tested.
- Including negative weights. Greedy should stop adding elements once the weights become non-positive. Otherwise you are computing a maximum-weight basis, which is a different problem.
- Treating matroids as the only route to a greedy proof. Huffman coding and Dijkstra's algorithm are correct but are not matroid greedy algorithms. Prove them directly, as earlier chapters did.
Exercise
Show that non-overlapping interval sets violate the exchange rule, using A = {[0, 10]} and B = {[0, 4], [5, 10]} with closed intervals.
Show answer
Both sets are independent and |A| = 1 < |B| = 2. Each interval in B overlaps [0, 10], so adding either one to A breaks independence. No valid x exists, so the exchange rule fails and the structure is not a matroid.
Exercise
Is the family "all sets of at most 3 elements from a 10-element set" a matroid, and what does greedy return?
Show answer
Yes, it is the uniform matroid U(3, 10). Any smaller set can take an element from a larger one and stay within size 3. Greedy returns the 3 heaviest elements that have positive weight.