Exercises — Greedy — exchange argument proof technique
3.7.3 · D4· Coding › Algorithm Paradigms › Greedy — exchange argument proof technique
Kisi bhi exercise se pehle, hum neeche use hone wale har symbol ko samjhenge:
Level 1 — Recognition
L1.1 — Kaunsi proof technique?
Tumhe dikhana hai: "greedy apne pehle picks mein se har ek ke baad optimal solution se kabhi peeche nahi hota." Kya yeh ek exchange/swap argument hai ya ek greedy-stays-ahead argument?
Recall Solution
Greedy-stays-ahead. Yeh ek measure track karta hai (tum picks ke baad kitna aage ho) aur dikhata hai ki greedy ka measure har par optimal ka measure dominate karta hai. Ek exchange argument instead optimal solution ko ek swap at a time physically edit karta hai. Contrast ke liye Greedy-stays-ahead proof technique dekho.
L1.2 — Inversion dhundho
Ek schedule jobs ko is order mein chalata hai (length brackets mein): . SPT rule (shortest first) ke under, har adjacent inversion list karo.
Recall Solution
Adjacent pairs aur kya pehle wala lamba hai (SPT rule ke under inversion):
- : → inversion ✔
- : → theek hai
- : → inversion ✔
Do adjacent inversions: aur .
Level 2 — Application
L2.1 — Ek SPT swap, numbers ke saath
Do jobs time par ek block ke roop mein start hoti hain: job mein hai, job mein hai, aur mein pehle chal raha hai se. mein compute karo, phir swapped schedule mein (jahan pehle ho), aur improvement do.
Recall Solution
mein ( phir ): ; . Sum . mein ( phir ): ; . Sum . Improvement . Formula se match karta hai.
Picture (Figure s01) "paid twice" idea ko visible banata hai. Ise aise padho: har row ek schedule hai, har block ki width uski processing time hai, aur red arrow us finish time par point karta hai jo dono -values ke andar count hoti hai (pehle job ki length dobara second job ki completion mein add hoti hai). Top row () mein woh doubled length lamba red bar ka hai; bottom row () mein woh chhota green bar ka hai. Toh figure geometrically dikhata hai kyun total exactly se girta hai: tum us length ko shrink karte ho jo twice charge hoti hai. Figure ke bina " vs " doubling sirf algebra hai; figure ke saath tum dekh sakte ho kaunsa bar doubled hai.

L2.2 — Ek Smith's-rule swap
Weighted completion time . Adjacent jobs phir : , . Kya swap karna chahiye? Cost change compute karo aur interpret karo.
Recall Solution
Formula kahan se aata hai. Maano pair time par start hota hai (chahe pehle koi bhi job chale). Order -then- mein: aur , toh pair contribute karta hai . Order -then- mein: contribution hai . Subtract karne par, wale har term cancel ho jaate hain (dono jobs abhi bhi se pehle start nahi karti), bacha rehta hai Isliye messy expression is clean cross-difference mein collapse hoti hai: yeh exactly mutual-waiting penalty ko isolate karta hai.
Plug in. . Positive matlab current order ( phir ) zyada cost karta hai — swap help karta hai se. Equivalently, aur ; kyunki hai, job ka ratio bura hai aur usse baad mein jana chahiye. Yeh hai Smith's rule — weighted completion time.
Tie case: agar toh — koi bhi order equally achha hai, toh ties arbitrarily break karo.
Level 3 — Analysis
L3.1 — Strict vs equal kyun?
SPT proof mein swap ek strict improvement deta hai, lekin activity-selection proof mein woh equal cost deta hai. Explain karo ki har problem ki kaunsi feature strict vs equal force karti hai, aur kyun dono phir bhi optimality prove karte hain.
Recall Solution
- SPT ek sum of real quantities () minimise karta hai. Ek genuine inversion () swap ko strictly se sasta banata hai. Strictness humein contradiction se argue karne deta hai: ek optimal schedule mein inversion nahi ho sakta, warna hum use beat kar lete. (Ek tie par , change hota hai — inversion nahi, kuch fix karne ki zaroorat nahi.)
- Activity selection activities count karta hai. ko earlier-finishing se replace karna count same rakhta hai (one-for-one), toh swap weakly no-worse hai. Yeh optimality preserve karta hai instead of improve karne ke, phir hum remainder par induct karte hain — dekho Activity Selection Problem.
Dono kaam karte hain kyunki exchange contract sirf "no worse" require karta hai. Strict improvement ek bonus hai (aur contradiction phrasing enable karta hai); equality swap chain ke saath optimality carry karne ke liye kaafi hai.
L3.2 — Huffman: dono terms zaroori hain
Optimal tree cost hai (frequency times depth). Hum aur swap karte hain. Given aur , har swap ka aur total compute karo. Confirm karo .
Recall Solution
Har kahan se aata hai. Cost hai (yahan symbol ki frequency hai aur tree mein uski depth). Symbols aur ko swap karne ka matlab hai ab depth par baithega aur depth par; har doosra symbol apni depth rakhe ga, toh unke saare terms difference mein cancel ho jaate hain. Sirf do moved symbols survive karte hain: Woh factored form isliye swap ko sign karna easy banata hai: yeh frequency gap aur depth gap ka product hai. Same algebra ke saath deta hai.
Plug in. Pehla swap: . Doosra swap: . Total . Cost strictly decrease hoti hai yahan. Har factor pattern: times non-positive product deta hai. Tie case: agar frequency gap ho (equal frequencies) ya depth gap ho, woh term exactly hai — phir bhi koi increase nahi. Dono ko sum karna mandatory hai — dekho Huffman Coding.
Level 4 — Synthesis
L4.1 — Full SPT exchange proof
Poora exchange argument likho ki SPT (shortest processing time first) minimise karta hai. 5-step skeleton use karo.
Recall Solution
1. Setup. Maano koi bhi optimal schedule hai, SPT schedule hai. 2. First difference. Agar , done. Warna sorted order se differ karta hai, toh usme kam se kam ek adjacent inversion hai: jobs phir with . (Agar koi adjacent pair inverted na ho, toh poora order sorted hota = .) 3–4. Swap. Adjacent pair ko exchange karke banao. se pehle aur ke baad ka har job apna exact start time rakhe ga, toh sirf change hote hain — feasibility holds (abhi bhi ek time par ek job). 5. No worse. Maano job ka start time hai mein (equivalently, adjacent block ka start time, kyunki immediately ke baad chalta hai). mein: , , toh unka sum hai . mein: , , sum . Hence Toh strictly sasta hai — impossible agar optimal tha aur usme inversion tha. Isliye optimal mein koi inversion nahi, yaani yeh SPT se sorted hai. Hence optimal hai. Tie case: agar toh difference hai — dono orders ka cost same hai, toh equal-length jobs kisi bhi order mein reh sakti hain aur SPT (ties arbitrarily break karke) phir bhi optimal hai. (Adjacency-only argument kaafi hai kyunki koi bhi inversion ek adjacent inversion imply karta hai — dekho L1.2.)
L4.2 — Full activity-selection first-step lemma
Prove karo: koi optimal solution include karta hai, woh activity jo globally sabse pehle khatam hoti hai.
Recall Solution
Is proof ke liye notation. Har activity ek interval hai, jahan uska start time hai (jab shuru hoti hai) aur uska finish time hai (jab khatam hoti hai). (Yeh finish-time hai — Huffman exercise L3.2 mein use hone wale frequency se alag hai.) Setup. Maano optimal hai, uski activities finish time se sorted hain. Maano woh activity hai jiska finish time sab activities mein sabse chhota hai, toh . Swap. ko se replace karke banao, rakhte hue. Feasibility. mein, activity start hoti hai par (woh overlap nahi karti). Kyunki , activity abhi bhi khatam hone ke baad start hoti hai. vs : compatible. Baad ki sab activities already ke baad thi, hence ke baad bhi hain. Toh ek valid (pairwise non-overlapping) selection hai. No worse. (one for one swap), toh bhi optimal hai aur usme hai. Tie case: agar do activities globally sabse pehli finish time share karein, koi bhi ka kaam kar sakti hai; wahi argument hold karta hai, toh choice free hai. Induct karo ke baad start hone wali activities ke subproblem par: same lemma apply hota hai, toh greedy ka har pick ek optimum tak extendable hai.
Level 5 — Mastery
L5.1 — Greedy key khud invent karo (deadline penalties)
unit-length jobs, sab time par available, har ek slot mein ek chale. Job ka weight hai aur, agar woh slot mein khatam ho, penalty pay karta hai . Adjacent-swap analysis use karke, sahi sorting key derive karo aur prove karo.
Recall Solution
Is proof ke liye notation. Ek "slot" ek unit time interval hai; slot number mein rakha job par khatam hota hai. Do adjacent jobs consider karo jahan mein slot mein aur slot mein chalta hai.
Swap change derive karo. Swap karne par slot mein aur slot mein aata hai. Sirf ye do jobs slots change karti hain, toh baki saari penalties difference mein cancel ho jaati hain: Expand karo: -terms dete hain (slot index completely cancel ho jaata hai, kyunki dono jobs abhi bhi same do slots occupy karti hain), aur terms dete hain . Toh
Key read off karo. Swap help karta hai (cost lower karta hai) jab , yaani jab : ek lighter job jo ek heavier job se pehle baitha hai usse swap karna chahiye taaki heavier job pehle aa sake. Isliye greedy rule hai weight se sort karo, descending — sabse bada weight pehle.
Proof (exchange argument). Koi bhi optimal schedule lo. Agar kabhi ek lighter job ko immediately ek heavier job se pehle chalaye ( with before ), toh upar wala adjacent swap cost se lower karta hai, jo optimality ko contradict karta hai. Hence optimal mein aisa koi inversion nahi hai, toh yeh descending weight se sorted hai. Isliye optimal hai.
Tie case: agar toh — equal-weight jobs kisi bhi slot mein ho sakte hain; ties arbitrarily break karo. (Yeh Smith's rule hai sab ke saath: ratio ascending weight descending.)
L5.2 — Exchange kab fail karta hai? (bridge to DP)
Coin change denominations se banane ke liye, greedy-by-largest kitne coins deta hai, aur true optimum kitne? Explain karo, ek sentence mein, kyun koi bhi adjacent-swap argument greedy ko yahan bacha nahi sakta — yeh connect karo is baat se ki hum Dynamic Programming par kyun escalate karte hain.
Recall Solution
Greedy-by-largest: coins. Optimum: coins. Greedy optimal nahi hai. Koi exchange argument kaam nahi karta kyunki problem mein exchange property nahi hai: ek single greedy coin ko doosre se swap karna monotonically improve nahi karta — optimum greedy ki pehli choice () ke saath koi coin share nahi karta, toh koi local swap chain nahi hai jo feasible aur no-worse rahe. Jab greedy/exchange (ya matroid) structure absent ho, tab hume DP ki overlapping-subproblem machinery chahiye.
Recall Self-test checklist
Mera har exchange proof objective state karta hai ::: haan — optimise hone wala number kisi bhi swap se pehle naam leta hai Main pehle disagreement par swap karta hoon aur matching prefix par induct karta hoon ::: haan Main ki feasibility check karta hoon, sirf uski cost nahi ::: haan Agar construction do swaps kare toh main dono terms sum karta hoon ::: haan Main jaanta hoon strict-improvement (contradiction) vs equal-cost (preserve) dono valid hain ::: haan Greedy key mein tie par swap change exactly hota hai, toh ties arbitrarily break hoti hain ::: haan