3.7.3Algorithm Paradigms

Greedy — exchange argument proof technique

2,434 words11 min readdifficulty · medium

WHAT is an exchange argument?

There are two flavours you must recognise:

  • Greedy-stays-ahead — show greedy is never behind optimal after kk steps (a measure-based induction).
  • Exchange/swap — physically swap two elements in OO and prove the objective doesn't get worse.

This note is about the swap flavour, the workhorse for ordering/scheduling problems.


WHY does swapping prove optimality?

The deep reason: you are building a chain O=O0    O1        Om=G,cost(Oi+1)cost(Oi).O = O_0 \;\to\; O_1 \;\to\; \cdots \;\to\; O_m = G,\qquad \text{cost}(O_{i+1}) \le \text{cost}(O_i). Telescoping: cost(G)cost(O)=OPT\text{cost}(G) \le \text{cost}(O) = \text{OPT}. Since OPT\text{OPT} is the best possible, cost(G)=OPT\text{cost}(G) = \text{OPT}.


HOW to run an exchange proof (recipe)

The whole game lives in step 5 — and it always boils down to the inequality that defines the greedy choice.


Worked Example 1 — Minimise total completion time

Problem. nn jobs, job kk has length pkp_k. Run one at a time. Minimise the (unweighted) total completion time kCk\sum_k C_k, where CkC_k = finish time of job kk. Greedy: Shortest Processing Time first (SPT). (If each job also had a weight wkw_k and the objective were kwkCk\sum_k w_k C_k, the correct greedy would sort by the ratio pk/wkp_k/w_k — see the mistake box below.)


Worked Example 2 — Activity selection (earliest finishing time)

Problem. Intervals [sk,fk)[s_k,f_k); pick the max number of non-overlapping ones. Greedy: repeatedly take the compatible activity with the earliest finish time.

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 3 — Huffman coding (sibling lemma)

Figure — Greedy — exchange argument proof technique

Steel-manning the mistakes


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.


Active-recall flashcards

What does an exchange argument transform, and into what?
Any optimal solution OO, into greedy's solution GG, 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)-cost(O)(O') when swapping an inversion aa before bb?
papb>0p_a-p_b>0 (longer-before-shorter is strictly worse) — for the unweighted Ck\sum C_k.
For weighted completion time wkCk\sum w_k C_k, what is the correct greedy order?
Sort by the ratio pk/wkp_k/w_k (Smith's rule); SPT is the special case wk=1w_k=1.
Why must you swap at the FIRST index where GG and OO differ?
To induct on the matching prefix and guarantee the swaps converge to GG 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\} making 66: greedy = 4+1+14+1+1 (3 coins), optimal = 3+33+3 (2 coins).
In activity selection, why does replacing o1o_1 by greedy's g1g_1 preserve feasibility?
fg1fo1f_{g_1}\le f_{o_1}, so any later activity compatible with o1o_1 is still compatible with g1g_1.
What telescoping inequality finishes an exchange proof?
cost(G)(G)\le cost(O)=(O)= OPT, so cost(G)=(G)= OPT.
In Huffman's sibling lemma, why is the total cost change 0\le 0?
It is the SUM of two swap terms (f(a)f(x))(dxda)+(f(b)f(y))(dydb)(f(a)-f(x))(d_x-d_a)+(f(b)-f(y))(d_y-d_b), each a (non-neg)×\times(non-pos) product.

Connections

  • 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)

Concept Map

compares

compares

find

perform

justifies

prove

repeat gives

reaches

implies

therefore

instance of

uses criterion

Exchange argument

Greedy solution G

Arbitrary optimal O

First difference at index i

Swap oi and oj into O prime

Greedy choice criterion

cost O prime <= cost O

Chain O to G by swaps

Telescoping cost G <= OPT

Greedy is optimal

SPT example minimise sum Ck

Hinglish (regional understanding)

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 OO 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 OO 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 OO dheere dheere greedy GG jaisa ban jaayega, aur cost kabhi badha nahi — matlab greedy bhi optimal hai. Yeh telescoping logic hai: cost(G)(G) \le cost(O)=(O) = OPT.

SPT example me, jab objective sirf Ck\sum 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 papbp_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 wkw_k ho aur objective wkCk\sum w_k C_k ho, to sahi greedy ratio pk/wkp_k/w_k pe sort karta hai (Smith's rule), sirf pkp_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 (xax\leftrightarrow a aur yby\leftrightarrow b), to dono terms ka sum 0\le 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.

Go deeper — visual, from zero

Test yourself — Algorithm Paradigms

Connections