Intuition The one-sentence idea
An exchange argument proves a greedy algorithm is optimal by showing: take ANY optimal solution, and you can swap (exchange) one of its choices for the greedy choice without making the solution worse . Repeat until the optimal solution looks exactly like the greedy one. Since greedy ≤ optimal at every swap and we never lose quality, greedy must itself be optimal.
Definition Exchange argument
A proof technique for greedy correctness. Let G G G = the solution greedy produces, O O O = an arbitrary optimal solution. We find the first place where they differ , then show we can modify O O O to agree with G G G at that spot while keeping O O O at least as good. By induction this transforms O O O into G G G with no loss, proving G G G is also optimal.
There are two flavours you must recognise:
Greedy-stays-ahead — show greedy is never behind optimal after k k k steps (a measure-based induction).
Exchange/swap — physically swap two elements in O O O and prove the objective doesn't get worse.
This note is about the swap flavour, the workhorse for ordering/scheduling problems.
Intuition Why "no worse after a swap" is enough
Suppose every optimal O O O that disagrees with greedy can be edited (one swap) into another solution that (a) is still feasible , (b) is no worse , and (c) agrees with greedy in one more place . Then start from O O O and keep swapping. Each swap reduces the number of disagreements by at least one, so after finitely many swaps you reach G G G itself — and it's still no worse than the original optimum. Hence G G G is optimal. ∎
The deep reason: you are building a chain
O = O 0 → O 1 → ⋯ → O m = G , cost ( O i + 1 ) ≤ cost ( O i ) . O = O_0 \;\to\; O_1 \;\to\; \cdots \;\to\; O_m = G,\qquad \text{cost}(O_{i+1}) \le \text{cost}(O_i). O = O 0 → O 1 → ⋯ → O m = G , cost ( O i + 1 ) ≤ cost ( O i ) .
Telescoping: cost ( G ) ≤ cost ( O ) = OPT \text{cost}(G) \le \text{cost}(O) = \text{OPT} cost ( G ) ≤ cost ( O ) = OPT . Since OPT \text{OPT} OPT is the best possible, cost ( G ) = OPT \text{cost}(G) = \text{OPT} cost ( G ) = OPT .
The whole game lives in step 5 — and it always boils down to the inequality that defines the greedy choice.
Problem. n n n jobs, job k k k has length p k p_k p k . Run one at a time. Minimise the (unweighted) total completion time ∑ k C k \sum_k C_k ∑ k C k , where C k C_k C k = finish time of job k k k . Greedy: Shortest Processing Time first (SPT) . (If each job also had a weight w k w_k w k and the objective were ∑ k w k C k \sum_k w_k C_k ∑ k w k C k , the correct greedy would sort by the ratio p k / w k p_k/w_k p k / w k — see the mistake box below.)
Worked example Exchange proof for SPT
Setup. Let O O O be optimal. Suppose O O O runs a job a a a immediately before a job b b b with p a > p b p_a > p_b p a > p b (an "inversion" — longer before shorter). Greedy forbids this.
Swap. Swap a a a and b b b in O O O to get O ′ O' O ′ . Why this step? All jobs scheduled before a a a and after b b b are unchanged, so only C a , C b C_a, C_b C a , C b change.
Let t t t = start time of the pair. Then:
In O O O : C a = t + p a C_a = t+p_a C a = t + p a , C b = t + p a + p b C_b = t+p_a+p_b C b = t + p a + p b . Sum = 2 t + 2 p a + p b = 2t+2p_a+p_b = 2 t + 2 p a + p b .
In O ′ O' O ′ : C b = t + p b C_b = t+p_b C b = t + p b , C a = t + p b + p a C_a = t+p_b+p_a C a = t + p b + p a . Sum = 2 t + 2 p b + p a = 2t+2p_b+p_a = 2 t + 2 p b + p a .
Why this step? The job that finishes second pays the other's length, so putting the shorter job first reduces the doubled term.
Difference: cost ( O ) − cost ( O ′ ) = ( 2 p a + p b ) − ( 2 p b + p a ) = p a − p b > 0. \text{cost}(O)-\text{cost}(O') = (2p_a+p_b)-(2p_b+p_a) = p_a-p_b > 0. cost ( O ) − cost ( O ′ ) = ( 2 p a + p b ) − ( 2 p b + p a ) = p a − p b > 0.
So O ′ O' O ′ is strictly better — contradicting optimality unless no inversion existed. Hence optimal has no inversions, i.e. it's sorted by SPT. ∎
Problem. Intervals [ s k , f k ) [s_k,f_k) [ s k , f k ) ; pick the max number of non-overlapping ones. Greedy: repeatedly take the compatible activity with the earliest finish time .
Worked example Exchange proof
Setup. Let O = ( o 1 , o 2 , … ) O=(o_1,o_2,\dots) O = ( o 1 , o 2 , … ) be optimal, sorted by finish time; g 1 g_1 g 1 = greedy's first pick (earliest finishing of all).
Claim/Swap. Replace o 1 o_1 o 1 by g 1 g_1 g 1 to form O ′ O' O ′ .
Why this step? g 1 g_1 g 1 finishes no later than o 1 o_1 o 1 (f g 1 ≤ f o 1 f_{g_1}\le f_{o_1} f g 1 ≤ f o 1 , since g 1 g_1 g 1 has the globally earliest finish). So whatever started after o 1 o_1 o 1 ended still starts after g 1 g_1 g 1 ends — feasibility is preserved .
Why no worse? O ′ O' O ′ has the same number of activities as O O O (we replaced one with one). So O ′ O' O ′ is also optimal, and now it agrees with greedy on the first pick.
Induct on the remaining subproblem (activities starting after f g 1 f_{g_1} f g 1 ). Greedy stays optimal at each step. ∎
Note the contrast with Example 1: here the swap gives equal cost (counting problem), there it gave strict improvement. Both are valid exchange arguments.
Worked example The "two least frequent are siblings" exchange
Claim. In some optimal prefix tree, the two lowest-frequency symbols x , y x,y x , y are sibling leaves at maximum depth.
Setup. Take optimal tree T T T . Let a , b a,b a , b be two deepest sibling leaves. WLOG f ( x ) ≤ f ( y ) f(x)\le f(y) f ( x ) ≤ f ( y ) and f ( a ) ≤ f ( b ) f(a)\le f(b) f ( a ) ≤ f ( b ) , with f ( x ) , f ( y ) f(x),f(y) f ( x ) , f ( y ) the smallest two frequencies, so f ( x ) ≤ f ( a ) f(x)\le f(a) f ( x ) ≤ f ( a ) , f ( y ) ≤ f ( b ) f(y)\le f(b) f ( y ) ≤ f ( b ) . Write depths d x = depth ( x ) d_x=\text{depth}(x) d x = depth ( x ) , etc.
Swap. Exchange x ↔ a x\leftrightarrow a x ↔ a and y ↔ b y\leftrightarrow b y ↔ b — this is two swaps, so the cost change is the sum of two terms , one per swap.
Why no worse? Cost = ∑ f ( c ) depth ( c ) = \sum f(c)\,\text{depth}(c) = ∑ f ( c ) depth ( c ) . The x ↔ a x\leftrightarrow a x ↔ a swap changes cost by ( f ( a ) − f ( x ) ) ( d x − d a ) (f(a)-f(x))(d_x-d_a) ( f ( a ) − f ( x )) ( d x − d a ) , and the y ↔ b y\leftrightarrow b y ↔ b swap changes it by ( f ( b ) − f ( y ) ) ( d y − d b ) (f(b)-f(y))(d_y-d_b) ( f ( b ) − f ( y )) ( d y − d b ) . The total change is
Δ = ( f ( a ) − f ( x ) ) ( d x − d a ) + ( f ( b ) − f ( y ) ) ( d y − d b ) . \Delta = (f(a)-f(x))(d_x-d_a) + (f(b)-f(y))(d_y-d_b). Δ = ( f ( a ) − f ( x )) ( d x − d a ) + ( f ( b ) − f ( y )) ( d y − d b ) .
Why this step? You must add both terms — accounting for only one swap would be incomplete.
Each term is ≤ 0 \le 0 ≤ 0 : in the first, f ( a ) ≥ f ( x ) f(a)\ge f(x) f ( a ) ≥ f ( x ) while a a a is a deepest leaf so d a ≥ d x d_a\ge d_x d a ≥ d x , giving (non-neg)× \times × (non-pos)≤ 0 \le 0 ≤ 0 ; the second is identical with f ( b ) ≥ f ( y ) f(b)\ge f(y) f ( b ) ≥ f ( y ) , d b ≥ d y d_b\ge d_y d b ≥ d y . Hence Δ ≤ 0 \Delta\le 0 Δ ≤ 0 — cost doesn't increase. ∎
Common mistake "Greedy works because it's locally best, so I don't need a proof."
Why it feels right: locally optimal moves often do win, and it's tempting to trust the pattern. The fix: locally best ≠ globally best (e.g. coin change with denominations { 1 , 3 , 4 } \{1,3,4\} { 1 , 3 , 4 } for 6 6 6 : greedy gives 4 + 1 + 1 = 3 4+1+1=3 4 + 1 + 1 = 3 coins, optimal is 3 + 3 = 2 3+3=2 3 + 3 = 2 ). The exchange argument is what certifies greedy — without it you have a heuristic, not a proof.
Common mistake "Sort by processing time always minimises completion-time objectives."
Why it feels right: SPT is correct for the unweighted ∑ C k \sum C_k ∑ C k , so it feels universal. The fix: for weighted completion time ∑ w k C k \sum w_k C_k ∑ w k C k , an inversion swap of adjacent jobs a , b a,b a , b changes cost by w b p a − w a p b w_b p_a - w_a p_b w b p a − w a p b ; this is non-negative exactly when p a / w a ≥ p b / w b p_a/w_a \ge p_b/w_b p a / w a ≥ p b / w b . So the right greedy sorts by the ratio p k / w k p_k/w_k p k / w k (Smith's rule), not by p k p_k p k alone. Unweighted is the special case w k = 1 w_k=1 w k = 1 .
Common mistake Forgetting to check
feasibility after the swap.
Why it feels right: we focus on the cost inequality and assume the swapped solution is still valid. The fix: every exchange proof has two obligations — (1) O ′ O' O ′ is still a legal solution, (2) O ′ O' O ′ is no worse. In activity selection the whole point of "earliest finish" is to guarantee (1). Skip it and the proof collapses.
Common mistake Swapping at the wrong place (arbitrary index instead of the
first difference).
Why it feels right: any difference looks swappable. The fix: you must swap at the first disagreement and use induction on the prefix that already matches greedy , otherwise you can't guarantee termination toward G G G .
Common mistake Accounting for only one swap when the proof does two (e.g. Huffman).
Why it feels right: the first swap's inequality looks like the whole story. The fix: if the construction performs two exchanges (x ↔ a x\leftrightarrow a x ↔ a and y ↔ b y\leftrightarrow b y ↔ b ), the cost change is the sum of both terms; you must show the combined expression is ≤ 0 \le 0 ≤ 0 , not just one half.
Recall Feynman: explain to a 12-year-old
Imagine the best possible way to line up kids by who finishes a chore fastest. I show you my "always-pick-the-quickest-next" plan. You say "but the real best plan might be different!" So I take your supposed best plan, find the first spot where it disagrees with mine, and swap two kids . I prove the total waiting time doesn't go up after the swap. I keep swapping until your plan looks exactly like mine — and since I never made it worse, my simple plan was the best all along. That swapping trick is the exchange argument.
Mnemonic Remember the recipe
"First Find Twin, Swap, Show" → FFTSS : find the First difference, Find the Twin of greedy's element in O O O , Swap it in, Show cost doesn't rise. (Pun: FaT SwapS keep the proof healthy.)
What does an exchange argument transform, and into what? Any optimal solution
O O O , into greedy's solution
G G G , via a chain of swaps that never increases cost.
What are the TWO obligations after every swap? (1) The swapped solution stays feasible; (2) its cost is no worse than before.
In the SPT proof, what is cost( O ) − (O)- ( O ) − cost( O ′ ) (O') ( O ′ ) when swapping an inversion a a a before b b b ? p a − p b > 0 p_a-p_b>0 p a − p b > 0 (longer-before-shorter is strictly worse) — for the
unweighted ∑ C k \sum C_k ∑ C k .
For weighted completion time ∑ w k C k \sum w_k C_k ∑ w k C k , what is the correct greedy order? Sort by the ratio
p k / w k p_k/w_k p k / w k (Smith's rule); SPT is the special case
w k = 1 w_k=1 w k = 1 .
Why must you swap at the FIRST index where G G G and O O O differ? To induct on the matching prefix and guarantee the swaps converge to
G G G in finitely many steps.
Counting problems (activity selection) give which kind of inequality after the swap? Equality (same number of activities) — you show an optimal solution matches greedy, not strict improvement.
Give a denomination set where greedy coin change fails. { 1 , 3 , 4 } \{1,3,4\} { 1 , 3 , 4 } making
6 6 6 : greedy =
4 + 1 + 1 4+1+1 4 + 1 + 1 (3 coins), optimal =
3 + 3 3+3 3 + 3 (2 coins).
In activity selection, why does replacing o 1 o_1 o 1 by greedy's g 1 g_1 g 1 preserve feasibility? f g 1 ≤ f o 1 f_{g_1}\le f_{o_1} f g 1 ≤ f o 1 , so any later activity compatible with
o 1 o_1 o 1 is still compatible with
g 1 g_1 g 1 .
What telescoping inequality finishes an exchange proof? cost
( G ) ≤ (G)\le ( G ) ≤ cost
( O ) = (O)= ( O ) = OPT, so cost
( G ) = (G)= ( G ) = OPT.
In Huffman's sibling lemma, why is the total cost change ≤ 0 \le 0 ≤ 0 ? It is the SUM of two swap terms
( f ( a ) − f ( x ) ) ( d x − d a ) + ( f ( b ) − f ( y ) ) ( d y − d b ) (f(a)-f(x))(d_x-d_a)+(f(b)-f(y))(d_y-d_b) ( f ( a ) − f ( x )) ( d x − d a ) + ( f ( b ) − f ( y )) ( d y − d b ) , each a (non-neg)
× \times × (non-pos) product.
Greedy Algorithms — general paradigm
Greedy-stays-ahead proof technique
Activity Selection Problem
Huffman Coding
Scheduling to minimise completion time
Smith's rule — weighted completion time
Matroids and the greedy theorem (the algebraic reason exchange works for whole classes of problems)
Proof by contradiction and Induction
Dynamic Programming (what to use when no exchange argument exists)
First difference at index i
Swap oi and oj into O prime
Telescoping cost G <= OPT
SPT example minimise sum Ck
Intuition Hinglish mein samjho
Dekho, greedy algorithm me hum har step pe "abhi ke liye sabse best" choice utha lete hain — jaise jobs ko sabse chhoti length wali pehle chalao, ya activities me jo sabse jaldi khatam ho usko pehle pakdo. Par sawaal yeh hai: kya yeh local lalach (greed) globally best answer dega? Iska proof dena padta hai, warna woh sirf ek heuristic hai. Exchange argument exactly yahi proof technique hai.
Idea simple hai: maan lo koi optimal solution O O O hai jo greedy se alag hai. Tum dono ko compare karo, aur first jagah dhoondo jahan dono differ karte hain. Wahan pe greedy ki choice ko O O O ke andar swap kar do. Ab tumhe do cheezein prove karni hain — (1) swap ke baad solution abhi bhi valid (feasible) hai, aur (2) uska cost pehle se kharab nahi hua. Agar yeh ho gaya, to ek-ek swap karte karte O O O dheere dheere greedy G G G jaisa ban jaayega, aur cost kabhi badha nahi — matlab greedy bhi optimal hai. Yeh telescoping logic hai: cost( G ) ≤ (G) \le ( G ) ≤ cost( O ) = (O) = ( O ) = OPT.
SPT example me, jab objective sirf ∑ C k \sum C_k ∑ C k hai (bina weights ke), to agar koi lambi job chhoti job se pehle chal rahi hai (inversion), to swap karne se total completion time p a − p b p_a - p_b p a − p b se kam ho jaata hai — isliye optimal me koi inversion ho hi nahi sakta, yaani woh SPT order me hoga. Par dhyan rakho: agar har job ka weight w k w_k w k ho aur objective ∑ w k C k \sum w_k C_k ∑ w k C k ho, to sahi greedy ratio p k / w k p_k/w_k p k / w k pe sort karta hai (Smith's rule), sirf p k p_k p k pe nahi! Activity selection me thoda alag — wahan swap ke baad count same rehta hai (equality), kyunki yeh counting problem hai. Aur Huffman me yaad rakho do swaps hote hain (x ↔ a x\leftrightarrow a x ↔ a aur y ↔ b y\leftrightarrow b y ↔ b ), to dono terms ka sum ≤ 0 \le 0 ≤ 0 dikhana padta hai.
Yaad rakho do common galtiyan: feasibility check bhulna mat, aur hamesha first difference pe swap karna, kisi bhi random jagah nahi. Counting problems me "<" ke chakkar mat pado, "≤" enough hai. Mnemonic: FFTSS — First difference, Find Twin, Swap, Show cost-no-worse. Bas yeh recipe ratt lo, koi bhi greedy proof crack ho jaayega.