Matroids & the Greedy Algorithm
The structure that makes greedy optimal: the Rado–Edmonds theorem.
Kruskal's algorithm sorts the edges by weight and keeps every edge that does not close a cycle. Replace "edges" by any elements and "does not close a cycle" by any notion of independence, and you have the greedy algorithm. The Rado–Edmonds theorem says exactly when it is optimal for every choice of weights: precisely when independence obeys one exchange axiom, the defining property of a matroid.
What you’ll learn
- State the matroid axioms, recognize uniform, partition, graphic and linear matroids, and show that matchings and knapsack sets are not matroids.
- Prove the Rado–Edmonds theorem in both directions: greedy is optimal for every non-negative weighting exactly on matroids.
- Model unit-time scheduling with deadlines and penalties as a matroid, and solve it greedily.
- Explain why the intersection of two matroids, which contains bipartite matching, is polynomial while three is NP-hard.
Independence systems and matroids
| Matroid | Independent sets | What greedy computes |
|---|---|---|
| Uniform | all subsets of at most of the elements | the heaviest elements |
| Partition | at most elements from each block of a partition of | the heaviest elements of every block |
| Graphic (of a graph ) | edge sets that contain no cycle (forests) | a maximum-weight spanning forest: Kruskal's algorithm |
| Linear (of vectors) | linearly independent sets of vectors | a maximum-weight basis, for example an XOR basis over GF(2) |
Exchange forces all bases to have the same size, the rank: if one basis were smaller, (I3) would extend it. That gives a quick test for non-matroids. In the path , the matchings and are both maximal but have different sizes, so the matchings of a graph do not form a matroid.
Other hereditary families fail the same way: sets of items within a knapsack's weight limit, and sets of pairwise non-adjacent vertices of a graph.
Which family of subsets is always a matroid?
- Sets of items whose total weight is at most a limit
- Sets of edges of a graph that contain no cycle
- Sets of edges of a graph that form a matching
- Sets of pairwise non-adjacent vertices of a graph
Show the answer
B. Sets of edges of a graph that contain no cycle
Forests satisfy exchange: a forest with more edges has fewer components, so one of its edges joins two components of the smaller forest and can be added without creating a cycle. This is the graphic matroid.
The greedy algorithm and the Rado–Edmonds theorem
GREEDY(S, independence oracle, w): # weights w(x) >= 0
A ← ∅
for x in S, in order of decreasing w(x):
if A ∪ {x} is independent:
A ← A ∪ {x}
return Afor the sort plus calls to the independence oracle. For a minimum-weight basis, scan in increasing order instead; on the graphic matroid that is Kruskal's algorithm.
Matroid ⇒ greedy is optimal. Let greedy pick in this order, so by decreasing weight, and let be a maximum-weight independent set sorted by decreasing weight. Since , may be extended to a basis, and all bases have the same size:
Suppose for a first index . Apply (I3) to and : some with keeps independent, and
So greedy examined before , when its set was a subset of ; by (I2) adding was allowed, and greedy would have taken it, contradicting . Hence for every :
Greedy always optimal ⇒ matroid. Suppose (I3) fails for with ; by (I2), shrink to elements (it still fails). Choose the weights
Greedy takes all of first, then no element of fits next to (nor next to any superset of , by (I2)), so it collects only weight 0 afterwards. Every element of weighs at least :
Scheduling with deadlines is a matroid
tasks each take one unit of time on a single machine, in slots . Task has an integer deadline and a penalty , paid if it finishes after . Minimize the total penalty. Call a set of tasks independent if all of them can finish on time, and let be the number of tasks in with deadline at most .
Characterization. On-time tasks with deadline at most all run in the first slots, so there are at most of them. Conversely, if for all , the -th task in deadline order has deadline at least (otherwise for its deadline ), so it finishes on time in slot :
Exchange. Let be independent with , and let be the largest with . It exists since , and it lies below the largest deadline, where . Then
So has more tasks with deadline exactly than has; pick one, . Adding to changes no count for , and for :
| Task | a | b | c | d | e | f |
|---|---|---|---|---|---|---|
| Deadline | 2 | 1 | 2 | 3 | 3 | 1 |
| Penalty | 40 | 35 | 30 | 25 | 20 | 10 |
Run greedy by decreasing penalty on the six tasks in the table. What is the minimum total penalty of the late tasks?
Show the answer
60
Accept a and b. Reject c: with a, b, c three tasks would have deadline at most 2. Accept d. Reject e: four tasks would have deadline at most 3. Reject f: two tasks would have deadline 1. The on-time schedule is b, a, d in slots 1, 2, 3, and the late tasks c, e, f cost . A brute force over all 64 subsets agrees.
def independent(tasks): # tasks: list of (deadline, penalty)
ds = sorted(d for d, _ in tasks)
return all(d >= k for k, d in enumerate(ds, start=1)) # k-th deadline >= k
def min_penalty(tasks):
chosen = []
for task in sorted(tasks, key=lambda t: -t[1]): # largest penalty first
if independent(chosen + [task]):
chosen.append(task) # can still all be on time
return sum(p for _, p in tasks) - sum(p for _, p in chosen)Each independence test sorts, so this version takes time. Giving each accepted task the latest free slot at or before its deadline, found with union-find over the slots, does the same in , dominated by the sort.
Two matroids, three matroids, and beyond
Many problems are the intersection of two matroids: the sets independent in both. In a bipartite graph with sides and , the edge sets with at most one edge at each vertex of form a partition matroid, those with at most one edge at each vertex of form another, and the common independent sets are exactly the matchings. The intersection of two matroids is usually not a matroid, and greedy fails on it: the path is bipartite, and greedy matching gets 3 where 4 is possible.
Three matroids are NP-hard. In a directed graph, take the arcs as and intersect the graphic matroid of the underlying undirected graph (no cycles), a partition matroid (in-degree at most 1) and another (out-degree at most 1). Common independent sets are disjoint directed paths, and one with arcs is a Hamiltonian path.
Greedoids (Korte & Lovász, 1981) drop heredity instead. Prim's algorithm lives there: the edge sets of trees containing a fixed root are not hereditary (removing a middle edge disconnects the tree), yet the greedy rule "add the lightest edge that keeps a tree" is optimal for them.
Bipartite matching is the intersection of two partition matroids, and greedy is optimal on each of them. Why does greedy still fail for maximum-weight bipartite matching?
- Partition matroids are not really matroids
- Greedy is optimal for bipartite matching; it only fails on general graphs
- The intersection of two matroids is generally not a matroid, so the Rado–Edmonds theorem does not apply to it
- Greedy works if the edges are sorted by increasing weight instead
Show the answer
C. The intersection of two matroids is generally not a matroid, so the Rado–Edmonds theorem does not apply to it
Rado–Edmonds characterizes when greedy works: exactly on matroids. The matchings of a bipartite graph violate exchange (two maximal matchings of sizes 1 and 2 in the path ), so some weighting defeats greedy. Edmonds's intersection algorithm or the Hungarian method is needed.
Key takeaways
- A matroid is a hereditary family of independent sets with the exchange axiom; uniform, partition, graphic and linear matroids are the standard examples, while matchings, knapsack sets and independent vertex sets are not matroids.
- Rado–Edmonds: "sort by weight, keep what stays independent" is optimal for every non-negative weighting exactly when the independence system is a matroid. Kruskal is the graphic case.
- Unit tasks with deadlines: the sets that can run on time form a matroid ( for all ), so greedy by penalty minimizes the total penalty.
- The intersection of two matroids is polynomial (Edmonds), which covers bipartite matching and König's theorem, but greedy no longer works there; three matroids are NP-hard.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
In the path , both and are maximal matchings. Which matroid axiom do the matchings of a graph violate?
- (I1): the empty set is not a matching
- (I2): a subset of a matching need not be a matching
- (I3): is smaller than , yet neither nor can be added to it
- None: matchings form a matroid
Show the answer
C. (I3): is smaller than , yet neither nor can be added to it
Matchings are hereditary and include the empty set, but exchange fails, as the two maximal matchings of different sizes show. That is exactly why greedy matching keeps (weight 3) and misses (weight 4) when and .
In the proof that "greedy is always optimal" implies the exchange axiom, , , and no element of can be added to . Why do the weights on and on defeat greedy?
- Greedy takes all of first, then can add nothing from , and
- Greedy takes first and misses
- Greedy takes and then all of , exceeding the optimum
- Greedy's weight is , which is less than
Show the answer
A. Greedy takes all of first, then can add nothing from , and
The elements of are the heaviest, and every subset of is independent, so greedy accepts all of them first. After that, every element of would make the set dependent, so greedy ends with weight , one less than , while is independent and weighs at least .
Unit-time tasks are given by their deadlines. Which set of tasks can all be finished on time on one machine?
- Deadlines 1, 1, 3
- Deadlines 2, 2, 2
- Deadlines 1, 2, 3, 3
- Deadlines 2, 3, 3
Show the answer
D. Deadlines 2, 3, 3
A set is schedulable iff, for every , at most of its tasks have deadline . For 2, 3, 3: one task by time 2 and three by time 3, fine. Running them in deadline order finishes them at times 1, 2, 3.
Why is finding a largest common independent set of three matroids NP-hard, when two matroids are polynomial?
- Three matroids have no rank function
- A Hamiltonian path in a directed graph is a common independent set of size of a graphic matroid and two partition matroids
- Greedy is optimal for two matroids but not for three
- It is not NP-hard: matroid intersection is polynomial for any number of matroids
Show the answer
B. A Hamiltonian path in a directed graph is a common independent set of size of a graphic matroid and two partition matroids
Arcs with no undirected cycle (graphic), in-degree at most 1 (partition) and out-degree at most 1 (partition) form disjoint directed paths; such arcs form a single path through every vertex. So deciding whether a common independent set of size exists decides Hamiltonian path.
Further reading
- J. Edmonds, "Matroids and the greedy algorithm," Mathematical Programming 1 (1971), 127–136.
- D. Gale, "Optimal assignments in an ordered set: an application of matroid theory," Journal of Combinatorial Theory (1968).
- J. Oxley, Matroid Theory, 2nd ed. (Oxford University Press, 2011).
- A. Schrijver, Combinatorial Optimization: Polyhedra and Efficiency (Springer, 2003), chapters 40–41.
- T. H. Cormen, C. E. Leiserson, R. L. Rivest & C. Stein, Introduction to Algorithms, 3rd ed. (MIT Press, 2009), §16.4–16.5 (removed from the 4th edition).