Lecture Notes — Lecture 23: Greedy and Dynamic Programming

Companion to the lecture plan and the worked examples. Read the Definitions before class, the Explanation after it; the practice questions and the lecture quiz close the loop.

Definitions

Explanation

Two strategies for optimisation problems. Greedy: sort by a rule, take while legal, never look back. It is fast and often right - but 'often' is not 'always', so each greedy claim needs an exchange argument (any optimal answer can be re-arranged into the greedy one) or falls to a counterexample. Interval scheduling (earliest end wins) survives; coin systems with weird denominations do not. Dynamic programming is the fallback when greedy fails: if a recursive definition keeps recomputing the same cases, cache them (memoisation) or fill a table bottom-up. The four declarations - state (what dp[j] means), transition (how answers compose), base case, evaluation order - turn any DP from magic into bookkeeping. Knapsack is the standard bearer: greedy by points-per-hour fails a tiny example, the dp-over-budget table succeeds. Rule of thumb: try greedy (with proof), fall back to DP when the greedy counterexample appears.

Examples

The lecture's worked examples live in examples.md; the case studies this lecture builds on are worked in class from the projector. The case sequence with difficulties:

Common misconceptions

Summary and key takeaways

  1. Greedy needs a proof (exchange argument) or a counterexample - then retreat to DP.
  2. Earliest-end wins interval scheduling; the same data defeats start-first and shortest-first.
  3. Memoisation fixes recomputation: cache the answer, halve the tree.
  4. DP in four declarations: state, transition, base, order.
  5. 0/1 knapsack iterates the budget descending - ascending silently reuses items.

Practice questions

Where this leads

Next lecture: Graphs as Models. The quiz below checks this lecture's essentials before we build on them.