← back to stream

Greedy algorithms

A greedy algorithm makes the locally optimal choice at each step and never backtracks. It's faster and simpler than dynamic programming, but it only finds the true optimum when the problem has the "greedy-choice property" — local optima composing into a global optimum. When that holds: activity scheduling, Huffman coding (data compression), Dijkstra's shortest path, minimum spanning trees. When it doesn't: the classic counterexample is making change with coins for 6 — greedy picks 4 then two 1s (three coins), optimum is two 3s. The practical skill is recognising when to trust greedy; if you can't prove the greedy-choice property, DP is the safer default.