3.7.3 · D3 · HinglishAlgorithm Paradigms

Worked examplesGreedy — exchange argument proof technique

4,458 words20 min read↑ Read in English

3.7.3 · D3 · Coding › Algorithm Paradigms › Greedy — exchange argument proof technique

Tumne recipe parent note (3.7.3) mein dekhi hai. Yeh page drill hall hai: hum har tarah ki situation se guzarte hain jo ek exchange argument face kar sakta hai, taaki jab koi naya problem tumhare saamne aaye toh tum uski shape pehle se pehchaan lo.

Koi bhi symbol use hone se pehle, yeh hai woh notation jo poore page mein use hoti hai, simple words mein:

Recall Do facts jo hum har jagah reuse karte hain (recap kiya gaya taaki yeh page akele kaam kare)
  • Completion time. Agar jobs ek ek karke run hoti hain, toh kisi job ki finish time = uski start plus uske aane tak run ki gayi har cheez ki total length, khud usse including. Unweighted total completion time hai .
  • ke liye adjacent-swap identity. Maano do adjacent jobs (pehle) phir time par start karti hain. Tab aur hai, toh unka combined contribution hai . Unhe swap karo aur yeh ban jaata hai. Poore schedule cost mein sirf in do terms mein change hota hai, toh ke saath swap ka difference hai . Shorter se pehle Longer rakhna cost mein exactly zyada deta hai.

Scenario matrix

Neeche har cell ek distinct behaviour hai jo ek exchange swap dikha sakta hai. Cell column ek letter ID () hai jise hum "cell A", "cell B", etc. kehte hain; har worked example un cells ke saath tagged hai jo woh cover karta hai.

Cell Scenario class Swap cost ke saath kya karta hai Example
A Strict improvement (minimisation) : cost strictly drop hoti hai → contradiction Ex 1
B Equal cost (counting / maximisation) : cost unchanged → still optimal Ex 2
C Ratio / weighted objective ka sign ek ratio rule se tay hota hai Ex 3
D Two-swap construction dono terms sum karne zaroori hain Ex 4
E Degenerate input (ties, equal keys) , greedy phir bhi theek Ex 5
F Zero / empty / single element base case, koi swap possible nahi Ex 6
G Jahan greedy FAIL karta hai (koi exchange lemma nahi) swap cost increase kar sakta hai () Ex 7
H Real-world word problem A ya C mein translate karo Ex 8
I Exam twist (non-adjacent swap, chain prove karo) door items swap karo Ex 9

Prerequisites jo tumhare paas open hone chahiye: Proof by contradiction, Induction, Greedy Algorithms — general paradigm.


Example 1 — Strict improvement (cell A)


Example 2 — Equal cost (cell B)


Example 3 — Ratio / weighted objective (cell C)


Example 4 — Two-swap construction (cell D)


Example 5 — Degenerate input: ties (cell E)


Example 6 — Zero / single element (cell F)


Example 7 — Jahan greedy FAIL karta hai (cell G)


Example 8 — Real-world word problem (cell H)


Example 9 — Exam twist: non-adjacent swap (cell I)


Recall

Recall Cell ko behaviour se match karo

Kis cell mein (equal cost) hai kyunki woh objects count karta hai? ::: Cell B (activity selection, maximisation). Cell C mein, kaun si quantity decide karti hai kaun pehle jaata hai? ::: Ratio (Smith's rule), raw length nahi. Cell G (coins , amount 6) koi exchange proof admit kyun nahi karta? ::: Greedy ke coin ki taraf ek swap count increase kar sakta hai (), toh "swap ke baad no worse" false hai (). Ek non-adjacent swap (cell I) ko kaise justify kiya jaata hai? ::: Adjacent swaps ki ek chain ke roop mein, har ek apni khud ki "no worse" inequality ke saath; gains sum hote hain. ka kya matlab hai, se alag? ::: = greedy ka -th choice in order; = optimal list ki position mein element.