Greed is a design pattern, not a guarantee
It produces an answer quickly. Whether that answer is optimal is a separate theorem about the specific problem.
A greedy algorithm never reconsiders. Sometimes that is provably optimal — minimum spanning trees, interval scheduling, Huffman codes — and usually it is not, so the proof is the whole content.
Core ideas
Greedy algorithms are easy to write and hard to justify, and the justification is what separates a method from a heuristic.
It produces an answer quickly. Whether that answer is optimal is a separate theorem about the specific problem.
Take any optimal solution and show it can be modified to agree with the greedy choice without getting worse.
Coin change with {1,3,4} fails at 6. Nothing in the algorithm signals it, which is why the proof matters.
The material
Three where it works, one where it fails, and the proof technique.
Given intervals with start and finish times, select as many non-overlapping ones as possible. The greedy rule is: repeatedly take the interval that finishes earliest among those compatible with what is already chosen.
The greedy algorithm selects a maximum-size set of pairwise compatible intervals.
Let g₁, g₂, … be the greedy selection in order and o₁, o₂, … an optimal one, both sorted by finish time. We show by induction that gᵢ finishes no later than oᵢ.
For i = 1, greedy chose the earliest-finishing interval overall. For the step, if gᵢ finishes no later than oᵢ, then o_{i+1} is available to greedy at step i+1, so g_{i+1} finishes no later.
If the optimum had more intervals than greedy, its next interval would be compatible with everything greedy chose, so greedy would not have stopped. Hence the sizes are equal.
□
Other plausible rules fail. Shortest interval first fails on a short interval overlapping two long disjoint ones. Earliest start first fails on one very long interval that begins first. Only earliest finish is optimal, and the difference is not visible without the proof.
With {1, 5, 10, 25} the greedy rule — largest coin first — is optimal for every amount. With {1, 3, 4} it fails at 6: greedy gives 4 + 1 + 1 (three coins) while 3 + 3 uses two. "Greedy works for coin change" is a property of the coin system, not of the strategy.
Nearest-neighbour tour construction can be arbitrarily bad: instances exist where it exceeds the optimum by any factor you like. Greedy gives a tour quickly and offers no guarantee at all.
The standard proof technique has three steps. Assume an optimal solution differing from the greedy one. Find the first place they differ. Show the optimum can be modified to agree with greedy there without becoming worse. Repeating turns the optimum into the greedy solution, so greedy is optimal too.
This is exactly the argument used for the cut property in “Spanning Trees and Minimum Spanning Trees” in Graphs and Trees: swapping the heavier edge for the lighter one in a minimum spanning tree does not increase the total. The technique is the same wherever greedy is correct.
Repeatedly merging the two least frequent symbols into a combined node produces a prefix-free binary code of minimum expected length.
The greedy choice is merging the two rarest symbols, and the exchange argument shows an optimal tree can always be modified to place them as siblings at the deepest level. It is the compression method behind several standard formats, and its optimality is a genuine theorem rather than an observation.
Interactive
Counts from running, not from quoting
Method
Write it, then prove it, then test it.
The locally best option: earliest finish, lightest edge, largest coin, rarest symbol.
Can any optimal solution be modified to include the greedy choice without getting worse? If yes, greedy is optimal.
Small instances usually reveal it. Coin change fails at 6 with a three-coin system.
"Optimal, by exchange argument" or "a heuristic with no guarantee". Both are legitimate; conflating them is not.
Intervals A(0,10), B(1,3), C(4,6), D(7,9).
Three rules, all plausible, and only one is optimal in general. The second happened to work on the first instance and fails on the second, which is exactly why a counterexample search is not a substitute for a proof.
For the fractional knapsack problem — items may be split — show that taking items in decreasing value-per-weight order is optimal. Then observe that the 0/1 version, where items cannot be split, defeats the same rule, and “Dynamic Programming” handles it.
Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.
No recommendations yet for this lesson.