When the best step now is safe
A Socratic walk-through of when the best step now is safe — reasoned out one step at a time, not lectured.
The question we started with
THE QUESTION #When is taking whatever looks best at this moment guaranteed to end up best overall?
In life, taking whatever looks best right now is usually a mistake — the whole vocabulary of prudence exists to warn against it. In algorithms, it sometimes gives the provably optimal answer, computed in a single pass, with no backtracking and no search.
So the interesting question is not "is greed good?" — it is neither — but: what property must a problem have for a locally best choice to be guaranteed globally best? And why does that property hold for scheduling meetings and fail for packing a knapsack?
Reasoning it through
REASONING #Let us find the property by trying to prove greed correct on a case where it works, and watching what the proof needs.
Interval scheduling: you have a list of meetings with start and end times, one room, and you want to hold as many meetings as possible. The greedy rule is to repeatedly take the meeting that finishes earliest among those still compatible.
Why should that be optimal? Suppose it is not, so some optimal schedule O holds more meetings than greedy does. Take the earliest-finishing meeting overall, call it g, which greedy certainly picks first. Does O contain it? If not, look at O's first meeting and swap it for g. Is the result still a valid schedule? Yes — g finishes no later than the meeting it replaced, so it cannot conflict with anything that came after. And the count is unchanged. So there exists an optimal schedule containing greedy's first choice.
Now notice where that leaves us. After committing to g, what remains is the same kind of problem: schedule as many as possible from the meetings that start after g ends. Repeat the argument on that smaller instance, and by induction greedy matches the optimum all the way down.
Read back over that proof and pull out the two things it actually used. First: there exists an optimal solution containing the greedy choice — the greedy-choice property, established by the swap, which is why this style is called an exchange argument. Second: after making that choice, the leftover is itself an instance of the same problem, and an optimal solution to the whole must contain an optimal solution to the leftover — optimal substructure. Every correct greedy proof has both parts; having only the second is the common failure, since optimal substructure alone gives dynamic programming, not greed.
So test the property rather than the intuition. Coin change: make six units from coins of value one, three and four. Greedy takes the four, then two ones — three coins. But two threes make six in two coins. The exchange argument fails at the first step: no optimal solution contains the four. Optimal substructure still holds perfectly well; the greedy choice property does not. Whether greed works for a coin system depends entirely on the denominations; systems where it does are called canonical, and deciding whether a given set is canonical is itself non-trivial.
The knapsack pair is the cleanest contrast in the subject. In the fractional version you may take part of an item, so the greedy rule of highest value per unit weight is optimal — if a better solution used less of the best item, swap some in, which is exactly the exchange move. In the 0/1 version you cannot take a fraction, the swap is unavailable, and greed can be arbitrarily bad. The same objective, the same ordering, and the difference is only whether the exchange step is legal.
Is there a general characterisation, or only case-by-case proofs? For one important family, yes. If your problem is "choose a maximum-weight set that is independent" and the independence system is a matroid — meaning subsets of independent sets are independent, and any smaller independent set can always be extended from a larger one — then the greedy rule of taking the heaviest admissible element is optimal. The Rado-Edmonds theorem gives the converse too: if greed is optimal for every assignment of weights, the system must be a matroid. That is why Kruskal's algorithm for minimum spanning trees is correct — forests form a matroid — and it is the closest thing to a general answer we have. It does not cover interval scheduling or Dijkstra's method, which have their own arguments.
The analogy
THE ANALOGY #Think of packing a suitcase with a strict weight limit, deciding item by item, never reopening the case. If what you are packing is liquid — sand, grain — then always pouring in the densest thing available is exactly right, because whatever you regret later you can pour out in any quantity. If you are packing rigid boxes, that reasoning collapses: the densest box may leave an awkward gap no combination fills, and the only way to know is to consider combinations you have already ruled out.
the suitcase makes the obstacle look like shape, when it is really the availability of the exchange — what licenses greed for the sand is that any early choice can be partially undone in favour of a later one, and no proof about geometry is doing the work.
Clarifying the model
THE MODEL #Two clarifications.
First, "greedy" describes the algorithm, not its quality. A greedy algorithm is any that commits irrevocably to the locally best option; whether that is optimal, approximately optimal, or terrible is a separate question answered by proof. Greedy set cover, for instance, is provably not optimal but always within a factor of about the natural logarithm of the number of elements — and Feige showed in 1998 that no polynomial-time algorithm does asymptotically better unless P equals NP. So greed can be the best available answer while being wrong.
Second, beware of the pattern where greed happens to work on your examples. The counterexamples in this subject are small and unobvious — three coin denominations, two knapsack items — and passing tests is not evidence. What settles it is attempting the exchange argument: take an arbitrary optimal solution, try to transform it into one containing your first greedy choice without making it worse. If that move goes through, you have a proof. If you cannot make it go through, the obstruction you hit is usually the counterexample itself.
A picture of it
THE PICTURE #How to readthe top box is the claim you want. The two boxes it derives from are the conditions that must both hold — and the two elements below show the same condition being met and being violated: the earliest-finish rule satisfies the greedy-choice property by an exchange argument, while the coin set of one, three and four traces to it as a refutation. Optimal substructure has no counterexample attached because it is rarely the part that fails.
What became clearer
WHAT CLEARED #Greed is safe exactly when an exchange is legal: when any optimal solution can be edited to begin with your choice without getting worse. That single move is the whole test, and it explains every case at once — fractional knapsack allows it, 0/1 does not; earliest-finish allows it, largest-coin-first does not; and for weighted independent sets, matroids are precisely the structures where it always does.
Where to go next
ONWARD #- Matroid intersection, where greed stops working but the problem stays tractable by other means.
- The scheduling problems where greed is optimal only after a non-obvious sort, such as minimising total weighted completion time.
Key terms
TERMS #| Term | What it means |
|---|---|
| Greedy-choice property | the guarantee that some optimal solution contains the locally best first choice. |
| Optimal substructure | the property that an optimal solution is built from optimal solutions of the remaining subproblem. |
| Exchange argument | the proof technique of editing an arbitrary optimal solution to include the greedy choice without worsening it. |
| Matroid | an independence system closed under subsets and satisfying the exchange axiom, for which greed is optimal under every weighting. |
| Canonical coin system | a set of denominations for which the largest-coin-first rule happens to be optimal. |
Every term the collection defines is gathered in the glossary.