Part III · trunk B · combinatorial algorithms · spine
Greedy is optimal for every choice of weights exactly when the feasible sets form a matroid, which answers "when does this heuristic work?" completely.
needs 00~2 h slides + 2½ h lablab: uv run co lab 13
13 · overview
Independence systems, the matroid axioms, the Rado–Edmonds theorem, and Kruskal as a special case.
Matroid intersection: bipartite matching and more, by augmenting paths. Three matroids: NP-hard.
Submodularity, and greedy's 1 − 1/e guarantee when it is no longer exact.
Words with a dotted underline are glossary terms: click one for its definition. G lists every term in the unit, N shows the notes. Optional extra: interactive figures, linked from the slides they support by a small ▸ chip; skip them freely.
section 1
An axiom that makes the obvious algorithm correct.
13 · greedy
A ground set $E$ and a family $\mathcal I$ of "independent" subsets, containing $\emptyset$ and closed under taking subsets.
Sort $E$ by decreasing weight. Take each element with positive weight if the chosen set stays independent.
| system | greedy |
|---|---|
| forests of a graph | optimal (Kruskal) |
| at most $k$ elements | optimal |
| linearly independent vectors | optimal |
| matchings of a graph | can fail |
| knapsack-feasible sets | can fail |
The lab's greedy is one function, and the independence oracle decides which row you are in.
Matchings failing: path with edge weights 2, 3, 2. Greedy takes the 3 and blocks both ends: 3. Taking both ends gives 4.
13 · greedy
Consequence of exchange: all maximal independent sets (bases) of any subset have the same size, its rank $r(S)$.
| matroid | independent sets |
|---|---|
| graphic | forests of a graph |
| uniform $U_{k,n}$ | sets of size $\le k$ |
| partition | at most $c_b$ elements from each block $b$ |
| linear | linearly independent sets of vectors |
| transversal | sets matchable into a bipartite graph's other side |
Linear algebra is where the axioms come from: Whitney (1935) abstracted exactly the exchange property of bases of a vector space.
13 · greedy · example
Edge weights on the edges. Thick: the maximum-weight forest greedy finds, weight 18.
| edge | weight | still a forest? |
|---|---|---|
| 34 | 6 | yes, take |
| 23 | 5 | yes, take |
| 01 | 4 | yes, take |
| 12 | 3 | yes, take: now spanning |
| 13, 24, 02 | 2, 2, 1 | no: each closes a cycle |
$A = \{01, 12\}$ (2 edges), $B = \{02, 13, 34\}$ (3 edges). Try each edge of $B$ in $A$: $02$ closes the triangle 0-1-2 ✗; $13$ ✓; $34$ ✓. At least one works, as the axiom promises.
13 · greedy
For an independence system $(E, \mathcal I)$: greedy finds a maximum-weight independent set for every weight function if and only if $(E, \mathcal I)$ is a matroid.
If some better set had a heavier $i$-th element than greedy's, exchange would hand greedy an element it could have taken earlier. Greedy never skips anything it could use.
full proof in the notes (N)
Find $A, B$ where exchange fails, give $A$ slightly more weight than $B$. For matchings on the path $a\!-\!b\!-\!c\!-\!d$: $A = \{bc\}$, $B = \{ab, cd\}$, neither edge of $B$ fits next to $bc$. Weights $2, 3, 2$ are exactly such weights: greedy 3, optimum 4.
Lab step 3 finds the $(A, B)$, an exchange witness. Step 2's greedy on the graphic oracle is Kruskal, matched against networkx on 25 graphs.
13 · greedy
The convex hull of incidence vectors of independent sets, the matroid polytope, is
$$\begin{aligned} P(M) = \{x \ge 0 :\ & \textstyle\sum_{e \in S} x_e \le r(S) \\ & \text{for all } S \subseteq E\}\end{aligned}$$Greedy's output is an optimal vertex, and a dual solution built from the same order proves it by LP duality (unit 03): the greedy dual certificate.
Why unit 06 and unit 09 meet here:
section 2
Greedy stops working, and augmenting paths start.
13 · intersection
Schematic. Arcs out of I-vertices are M1-exchanges; arcs into them are M2-exchanges. A shortest source→sink path is flipped, and |I| grows by one.
Matroid intersection: the largest set independent in both $M_1$ and $M_2$.
Algorithm (lab step 4): the exchange graph, then a shortest augmenting path, then flip. The next slide does one. Polynomial in the number of oracle calls.
13 · intersection · example
Bipartite graph: left $\ell_0, \ell_1$, right $r_0, r_1$, edges $e_0 = \ell_0 r_0$, $e_1 = \ell_1 r_0$, $e_2 = \ell_1 r_1$ (a path). $M_1$: one edge per left vertex. $M_2$: one per right vertex. A common independent set is a matching.
Start from $I = \{e_1\}$. It is maximal: $e_0$ clashes at $r_0$, $e_2$ at $\ell_1$. Greedy-style "add what fits" is stuck at size 1.
| question | answer |
|---|---|
| sources: $I + y \in M_1$? | $e_0$ ✓ ($\ell_0$ free); $e_2$ ✗ |
| sinks: $I + y \in M_2$? | $e_2$ ✓ ($r_1$ free); $e_0$ ✗ |
| $e_1 \to y$ if $I - e_1 + y \in M_1$ | $e_1 \to e_0$, $\ e_1 \to e_2$ |
| $y \to e_1$ if $I - e_1 + y \in M_2$ | $e_0 \to e_1$, $\ e_2 \to e_1$ |
Source $e_0 \to e_1 \to e_2$ sink. Flip membership along it: $e_0$ in, $e_1$ out, $e_2$ in.
$I = \{e_0, e_2\}$: one edge at each left vertex, one at each right vertex. Size 2, and no larger matching exists on 2 + 2 vertices.
This is the augmenting path of bipartite matching (unit 15), found by asking two independence oracles instead of looking at the graph.
13 · intersection
A directed Hamiltonian path is a set of $n - 1$ arcs that is independent in three matroids at once:
So a size-$(n-1)$ common independent set exists iff a Hamiltonian path does. That is NP-complete: three-matroid intersection is NP-hard.
This is where the islands end. The sequence runs:
Unit 22 turns reductions like this one into code.
section 3
When greedy is no longer exact, but still provably close.
13 · submodular
$f : 2^E \to \mathbb R$ with $\;f(A + e) - f(A) \ge f(B + e) - f(B)\;$ for $A \subseteq B$, $e \notin B$: the marginal gain shrinks.
Adding $e$ helps a small set at least as much as a big one. Matroid rank functions and coverage functions are submodular.
For monotone submodular $f$ and a cardinality limit $k$, greedy (add the element of largest marginal gain) achieves $f(G) \ge (1 - \tfrac1e)\,\mathrm{OPT}$.
Sets $\{1,2,3,4\}$, $\{1,2,5\}$, $\{3,4,6\}$, $\{7\}$.
Greedy: takes $\{1,2,3,4\}$ (4 new), then any other set adds only 1: covers 5.
Optimum: $\{1,2,5\} + \{3,4,6\}$ covers 6. Ratio $\tfrac56 = 0.83$, above the guarantee for $k = 2$, $1 - (\tfrac12)^2 = 0.75$.
why greedy is never worse than 1 − 1/e: notes (N)
For max coverage, no polynomial algorithm beats $1 - 1/e$ unless P = NP (Feige 1998).
13 · submodular
Lab then: 40 random sets over 120 elements, exact optimum by HiGHS MIP.
| $k$ | mean ratio | worst ratio | guarantee |
|---|---|---|---|
| 3 | 0.987 | 0.950 | 0.632 |
| 6 | 0.992 | 0.944 | 0.632 |
| 10 | 0.980 | 0.926 | 0.632 |
A worst-case guarantee is a floor, not a forecast. Random instances rarely come near the tight examples, which have to be constructed.
Unit 23 makes the same point for set cover's $H_n$ bound, and unit 28 turns "what did you actually measure?" into method.
13 · lab
uv run co test 13
uv run co then 13 # greedy vs Kruskal · intersection vs Hopcroft–Karp · coverage ratios vs MIP optimum
13 · checkpoint
Korte & Vygen, ch. 13.
Schrijver, Combinatorial Optimization, vol. B, ch. 39–41 (consulted).
Krause & Golovin, "Submodular function maximization" (2014), §1–3.
14 network flows · then 15 matching and 16 DP. 23 uses this unit's MST inside Christofides.