In an exchange argument for a greedy algorithm, what do you swap and what must the swap show?
answer
- start from someone else's optimal solution
- find where it first disagrees with greedy
- put greedy's choice in, keep it legal
- the edited solution must be no worse
- induct until the whole prefix matches
basics
~20 sAn exchange argument starts from any optimal solution, finds the first point where it disagrees with greedy, and swaps greedy's choice in. The swap must stay feasible and no worse, so some optimal solution agrees with greedy.
solid answer
~40 sYou never compare greedy's total against the optimum's total directly. You assume an arbitrary optimal solution, locate the *first* position where it disagrees with the greedy run, and modify it there so it takes greedy's choice instead. The swap owes two things: the modified solution must still be legal, and its objective value must be **no worse** than before — never strictly better, since that would contradict the assumption that you started from an optimum. What you get back is another optimal solution that agrees with greedy one step further; induction on the number of steps then yields an optimal solution agreeing with greedy everywhere. The conclusion is *greedy's output is optimal*, not *greedy's output is the unique optimum*.
go deeper
Be ready to say the four beats out loud: take any optimal solution, find the first place it differs from greedy, swap greedy's choice in, show it stays legal and no worse. Knowing the shape beats improvising.
Explain why 'no worse' is the right obligation and why strict improvement would contradict the starting assumption, and show where feasibility is actually at risk when you perform the swap.
Demonstrate that you notice when the exchange fails. The position where the swap breaks feasibility or loses value is where a counterexample lives, and saying so is worth more than forcing a proof that is not there.
Own the standard your team applies: which greedy rules ship with a written exchange argument, which ship as stated approximations, and how much proof effort a given correctness risk actually deserves.
## What a greedy correctness proof actually has to do A greedy algorithm commits to one irrevocable choice at a time and never reconsiders. That is why it is fast and why it is suspicious: nothing in the algorithm looks ahead, so nothing in the algorithm rules out the possibility that an early choice quietly destroys the best final answer. A correctness proof must close exactly that gap. It must show that committing to greedy's choice never costs you the optimum. An exchange argument is the standard way to close it, and its shape is worth memorising because interviewers ask you to reproduce the shape, not to invent a new proof on the spot. ## The invariant you are maintaining Before the mechanics, fix the statement being proved by induction: > After iteration *i*, the choices greedy has made so far extend to **some** optimal solution. Every word earns its place. "Extend to" means: there exists a complete optimal solution whose first *i* choices are exactly greedy's. "Some" means: there may be many optimal solutions, and greedy only has to be consistent with one of them. Candidates who state this as *the* optimal solution have already made the classic error — when several distinct answers achieve the same optimal value, greedy cannot be expected to match a particular one. When the invariant survives to the last iteration, greedy's complete output *is* an optimal solution, and the algorithm is correct. ## The exchange step The induction's base case is trivial: before any choice is made, the empty prefix extends to every optimal solution, and at least one exists. The inductive step is the exchange. Assume greedy's first *i* choices extend to an optimal solution *S*. Greedy now makes choice number *i+1*. Either *S* already contains that choice — nothing to do, the invariant carries over unchanged — or it does not. In the second case, build a new solution *S'* from *S* by removing whatever *S* used at that position and putting greedy's choice in its place, adjusting the rest of *S* as little as necessary. Two obligations, and a proof that skips either one is not a proof: 1. **Feasibility.** *S'* must still satisfy every constraint of the problem. This is where most exchange proofs actually do their work: showing that greedy's choice cannot conflict with the parts of *S* you kept. 2. **No worse.** The objective value of *S'* must be at least as good as *S*'s. Since *S* was optimal, *S'* is then optimal too. Notice the direction of that second obligation. You are *not* trying to prove greedy's choice is strictly better — that would contradict *S*'s optimality and is unprovable. You are proving greedy's choice is *free*: taking it costs nothing. Requiring strict improvement is the most common way a candidate ties themselves in knots at the whiteboard. ## Why the *first* disagreement Swapping at an arbitrary disagreeing position is legal but useless, because it gives no progress measure. By swapping at the earliest disagreement you keep the agreement prefix intact and lengthen it by exactly one, so the induction has something that strictly decreases: the number of positions where the optimal solution still differs from greedy. Finitely many exchanges therefore transform an arbitrary optimum into greedy's output without ever losing value. ## What the conclusion does and does not say It says greedy's output achieves the optimal objective value. It does not say the optimum is unique, does not say greedy is the only correct algorithm, and does not say anything about greedy's running time. It also says nothing about problems where the exchange fails — and when the exchange *does* fail, that failure is informative: the position where you cannot swap without breaking feasibility or losing value is usually exactly where a counterexample lives. ## Failure modes to avoid out loud - Comparing final totals ("greedy picked the biggest items, so its sum is biggest") — that is an assertion, not an argument. - Assuming the optimal solution is unique, then claiming greedy must equal it. - Forgetting feasibility: an exchange that improves the objective while violating a constraint proves nothing. - Proving only that the *first* choice is safe and stopping. One safe move is a base case; the induction is what turns it into a theorem. - Offering test cases in place of the argument. Any finite set of passing inputs is compatible with an algorithm that is wrong on the input you did not try.
- Why must the swap only be 'no worse' rather than strictly better?Because you started from an optimal solution. If inserting greedy's choice made it strictly better, the solution you began with was not optimal — a contradiction, not a proof. The point of the exchange is that greedy's choice is free: it costs nothing. Demanding strict improvement is both unnecessary and, on problems with ties, impossible.
- Why swap at the first disagreement rather than at any disagreeing position?Because the first disagreement gives the induction a measure that decreases. Everything before it already matches greedy, so after the swap the agreement prefix is one longer and the number of remaining disagreements drops by one. Swapping somewhere in the middle leaves earlier mismatches untouched, so the argument never terminates.
- State the loop invariant an exchange argument maintains.After iteration i, the choices greedy has committed to so far extend to some optimal solution. The word 'some' is load-bearing: when several different solutions share the optimal value, greedy need only be consistent with one of them. Saying 'the optimal solution' claims uniqueness you have not proved and usually do not have.
You are handed someone else's perfect itinerary and asked to prove yours is just as good. Rather than re-plan, you edit theirs one stop at a time to match yours, checking each edit keeps the trip legal and no slower.
saying these in an interview costs you the question
- Compares greedy's final total to the optimum's total and calls that a proof
- Tries to show greedy's choice is strictly better than the alternative
- Swaps at an arbitrary position instead of the first disagreement
- Forgets to check the modified solution is still feasible
- Claims greedy produces the unique optimal solution
- Proves only the first choice is safe and skips the induction
- Substitutes passing test cases for the argument