Greedy Pattern
Imagine you're at a pizza party and you always grab the biggest slice available. Sometimes that's the perfect strategy — you end up with the most pizza. Other times, a huge slice is so awkward to hold that you'd have done better grabbing two medium ones.
That's greedy in a nutshell: make the locally best choice at every step, and hope those local wins add up to a global win. When they do, you get a beautifully simple, fast algorithm. When they don't — you need Dynamic Programming.
When Does Greedy Actually Work?
A greedy algorithm is guaranteed to work when the problem has two properties:
Greedy-choice property: There's always a globally optimal solution that includes the greedy choice. You never need to second-guess yourself — the best-looking local option is safe to commit to.
Optimal substructure: After making the greedy choice, the remaining problem has the same structure, and an optimal solution to it combines with your choice to give a globally optimal answer.
Both of these together mean you can march forward greedily without ever backtracking.
A Tale of Two Knapsacks
Fractional Knapsack (greedy works): You have gold dust, silver flakes, copper powder. You can take any fraction. Always grab the most valuable material per gram first. Because you can perfectly fill every remaining gram with whatever is best, the greedy choice is always safe.
0/1 Knapsack (greedy fails): Same items, but now they're solid gold bars — all or nothing. Taking the highest value-per-kilo bar might leave a gap in your backpack that nothing else fits into. Two cheaper bars together might have been worth more. Greedy gets tricked by the gaps it creates.
Proving Greedy Correct: The Exchange Argument
The standard proof template for greedy correctness is the exchange argument. It goes like this:
Suppose an optimal solution OPT makes a different first choice than greedy. Find the first place they differ — greedy picks G, OPT picks something else A. Show that swapping A for G in OPT doesn't make things worse. Now OPT and greedy agree on the first choice. Keep swapping until OPT becomes identical to the greedy solution. Since each swap was non-worsening, greedy is also optimal.
Greedy vs Dynamic Programming
Both need optimal substructure. But DP explores all choices and combines sub-solutions — it's like a chess grandmaster thinking 10 moves ahead. Greedy commits to one choice and never looks back — it's like an expert who instantly knows the right move without calculating.
A practical rule of thumb: if the problem involves a continuous resource or a natural ordering (finish times, value ratios, deadlines), try greedy first. If the choices interact in complex, all-or-nothing ways, reach for DP.
Greedy vs DP — Coin Change Counterexample
Complexity
Quick check
Continue reading