Q&A-17How do you prove that a greedy strategy is optimal, and what exchange argument technique is standardly used?
Answer
To prove a greedy strategy is optimal, you must establish two mathematical properties: optimal substructure (an optimal solution to the subproblem extends to an optimal solution to the whole problem) and the greedy-choice property (a globally optimal solution can be reached by making locally optimal choices without backtracking).
The standard technique to establish the greedy-choice property is an exchange argument. Rather than proving directly that the greedy choices are unique, you show that any hypothetical optimal solution $O$ that differs from the greedy solution $G$ can be transformed into another valid optimal solution $O'$ that shares one more greedy choice, without worsening the objective function.
The 4-step exchange argument recipe:
- State a hypothetical optimal solution $O = {o_1, o_2, \dots, o_k}$ and the greedy solution $G = {g_1, g_2, \dots, g_m}$. Assume without loss of generality that $O \neq G$.
- Identify the first point of divergence: the lowest index or step $i$ where $g_i \notin O$ (or where the choices differ).
- Define an exchange: swap an element $o_j \in O \setminus G$ with $g_i$ to form a candidate solution $O' = (O \setminus {o_j}) \cup {g_i}$.
- Prove validity and non-decreasing quality: show $O'$ satisfies all constraints (e.g. non-overlapping intervals, capacity, acyclicity) and that $\text{cost}(O') \le \text{cost}(O)$ (for minimization) or $\text{value}(O') \ge \text{value}(O)$ (for maximization). By induction, repeated exchanges transform $O$ into $G$ through a chain of optimal solutions, proving $|G| = |O|$ or $\text{value}(G) = \text{value}(O)$.
# Concrete example: Interval Scheduling (earliest finish time)
# Given intervals: (start, finish)
intervals = [(1, 4), (3, 5), (0, 6), (5, 7), (3, 8), (5, 9), (6, 10), (8, 11)]
# Greedy choice: pick interval with earliest finish compatible with current end.
# G = [g_1, g_2, ...], O = [o_1, o_2, ...], sorted by finish times.
# First divergence at step r: g_r != o_r.
# By greedy selection: finish(g_r) <= finish(o_r).
# Replacing o_r with g_r cannot conflict with o_{r+1} because:
# finish(g_r) <= finish(o_r) <= start(o_{r+1}).
# Thus O' = (O - {o_r}) U {g_r} is valid and |O'| = |O|.| Technique | How it proves optimality | Primary failure mode |
|---|---|---|
| Exchange Argument | Gradually transforms arbitrary optimal $O$ to greedy $G$ preserving feasibility and cost | Choosing an exchange that violates downstream constraints |
| Greedy Stays Ahead | Measures a metric $M(i)$ at step $i$ and proves $M(G_i) \ge M(O_i)$ by induction | Metric is ahead locally but dead-ends before global completion |
| Matroid Theory | Proves the constraint system forms an independent set system with exchange axioms | Assuming submodular/matroid structure on problems that are NP-hard (e.g., 0/1 knapsack) |
Failure modes occur when engineers assume locally superior margins guarantee feasible extensions. For example, in 0/1 knapsack, sorting by value-to-weight density breaks because discrete granularity prevents full capacity utilisation; the exchange $o_j \to g_i$ leaves residual capacity that cannot be filled, failing step 4.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.