DP & greedy problem clinic (design on a novel problem)
◈ 6 cardsThe four-step DP recipe applied to edit distance and weighted interval scheduling — including the case where greedy-by-finish is provably suboptimal — plus the exchange-argument template.
The DP recipe you apply to ANY new problem
The exam and the OilKnapsack project both want you to design a DP, not recall one. Always the same four steps:
- Characterize the structure of an optimal solution — what's the last/first choice, and what subproblem remains after it? Prove optimal substructure by cut-and-paste (a better subsolution would let you improve OPT — contradiction).
- Define the recurrence — write the optimal value as a max/min over the choices, in terms of smaller subproblems. Include base cases.
- Compute bottom-up (or top-down + memo) — fill a table in an order that has every dependency ready. Running time = (#subproblems) × (work per subproblem).
- Reconstruct the solution — store the choice made at each cell (not just the value) and walk it back.
Worked design 1 — edit distance (a novel DP)
Problem: minimum insert/delete/substitute operations to turn string into . Subproblem: = edit distance between the prefixes and . Recurrence:
The three inner options are delete , insert , substitute. Base cases: turning a prefix into the empty string costs its length. Time Θ(mn), space Θ(mn) (or Θ(min(m,n)) keeping two rows). This is the shape of almost every string-DP exam question.
Worked design 2 — weighted interval scheduling (where greedy fails)
Activity selection (Module 5) is greedy: pick the earliest-finishing compatible activity. But if each job has a weight and you want maximum total weight, greedy-by-finish is wrong — a single heavy job can beat several light ones. This is the canonical 'greedy fails, use DP' problem.
Sort jobs by finish time. Let = the largest index with (the last compatible job). Subproblem: = max weight using jobs . Recurrence:
The exercise below computes an instance where the DP scores 17 but greedy-by-finish only 16 — the concrete proof that weight breaks greedy.
The greedy exchange-argument template
When greedy does work (activity selection, Huffman, Kruskal, Prim), prove it with an exchange argument: take any optimal solution OPT; show you can swap OPT's first choice for the greedy choice without making it worse (equal or better); repeat, transforming OPT into the greedy solution while preserving optimality. This proves the greedy-choice property — that a globally optimal solution contains the greedy first choice.