When a greedy rule is valid
Two useful proof ideas are the greedy-choice property—some optimal solution begins with the locally best choice—and optimal substructure—after that choice, the remaining task is a smaller instance of the same problem. A proof may use an exchange argument: replace the first choice in an optimal solution with the greedy choice and show the solution is no worse.
| Problem | Greedy rule | Result |
|---|
| Activity selection | Choose the compatible activity that finishes earliest | Optimal maximum-size schedule |
| Fractional knapsack | Take the greatest value per unit weight | Optimal when items can be split |
| Minimum spanning tree | Add a cheapest safe edge | Kruskal’s and Prim’s algorithms are optimal |
| Shortest paths | Finalise the nearest unsettled vertex | Dijkstra is correct only with non-negative edges |
| 0/1 knapsack | Take the greatest value-to-weight ratio | Can fail because items cannot be split |
These are problem-specific proofs. “Always take the largest” or “always choose the cheapest” is not a general correctness argument.
A worked success: choosing activities
Suppose activities have start and finish times, and only one can run at once. Sort by finish time; repeatedly select the first activity that starts after the last selected one finishes. This leaves as much time as possible for whatever follows.
Why is that safe? Consider an optimal schedule whose first activity finishes later than the earliest-finishing compatible activity. Replacing it with the earlier finisher cannot reduce the number of later activities that fit. Repeating the exchange establishes an optimal schedule that follows the greedy rule. The sorting costs O(n log n); the scan costs O(n).
Choosing the earliest start or shortest duration does not give the same guarantee. One long early-starting activity can block several short non-overlapping activities, and a short activity can straddle two that could otherwise both be chosen.
A counterexample: coin change
Greedy coin change repeatedly takes the largest coin no bigger than the remaining amount. With coins {1, 3, 4} and amount 6, it chooses 4, then 1, then 1: three coins. The optimum is 3 + 3: two coins. The local choice is legal but not globally best.
The result depends on the denomination system. Some systems have a proof that greedy is optimal; arbitrary systems do not. Dynamic programming can solve the minimum-coin version for a bounded integer amount by comparing choices across subamounts; see dynamic programming.
Try the visualiser
- Choose Problem “Activity selection (greedy works)” and step through the schedule. Check that each accepted activity starts after the previous one finishes.
- Choose Problem “Coin change — greedy FAILS” and set amount to 6. With coins {1, 3, 4}, compare the greedy choices with the two-coin optimum.
The experiment makes a useful distinction visible: the same simple pattern can be optimal on one problem and fail on another.
Where greedy appears
Huffman coding repeatedly merges the two least-frequent symbols to build an optimal prefix code. Kruskal and Prim use safe-edge choices to build a minimum spanning tree. Dijkstra selects the nearest unsettled vertex, provided every edge weight is non-negative. Fractional knapsack works because taking part of an item is allowed. For each algorithm, its correctness depends on its own invariant or exchange proof.