Greedy Algorithms

Difficulty
Importance

The problem

Some optimization problems can be solved by always making the choice that looks best right now, never reconsidering it later — when this works, it's dramatically simpler and faster than exploring every possibility, but using it on a problem where it doesn't actually hold produces a confidently wrong answer.

Why now

Dynamic programming already showed one way to handle problems with optimal substructure — by exploring and caching every relevant subproblem. Greedy algorithms are the much cheaper alternative that works only when a stronger condition holds: proof techniques already gave the exchange-argument style of reasoning needed to actually justify that a locally-best choice never needs revisiting.

Mental model

A greedy algorithm makes the locally optimal choice at every step and never looks back — cheap, but only correct if the problem has the 'greedy choice property': committing to the best immediate option never rules out reaching the globally best solution. Proving that property usually takes an exchange argument — show that any optimal solution can be rearranged to include the greedy choice without getting worse, which is exactly the kind of argument proof-techniques already trained.

Requires

Used in

Projects

  • Solve the activity-selection problem (maximize non-overlapping intervals) greedily, then write out the exchange-argument proof for why choosing the earliest-finishing activity first is always safe
  • Find a coin denomination system where the standard greedy 'always pick the largest coin' approach fails to produce the minimum number of coins, and explain why

Examples

  • US coin denominations (25, 10, 5, 1) happen to make greedy coin-change always optimal; an arbitrary denomination set like (1, 3, 4) can make greedy give a suboptimal answer for a target like 6
  • Interval scheduling's greedy rule — always pick the activity that finishes earliest — is provably optimal via an exchange argument, not just empirically observed to work

Resources

Mastery checklist