Exercises — When greedy fails — 0 - 1 knapsack counter-example
3.7.5 · D4· Coding › Algorithm Paradigms › When greedy fails — 0 - 1 knapsack counter-example
Shuru karne se pehle, ek reminder ek picture ke roop mein, kyunki har exercise isi pe lean karti hai:

Recall Picture kya kehti hai
Bag ek fixed-width box hai jiska capacity hai. Greedy left se densest items se fill karta hai (tall amber bars = high value-per-width). Jab agla dense item leftover slot se zyada wide hota hai, greedy use skip karta hai aur slot khali rehti hai — woh khali slot (amber-outlined gap) exactly woh value hai jo greedy throw away karta hai.
Level 1 — Recognition
Exercise 1.1
Ek-ek sentence mein batao, wo do structural properties kya hain jo ek problem mein honi chahiye taaki greedy algorithm provably optimal ho.
Recall Solution
- Greedy-choice property — koi na koi globally optimal solution locally best (greedy) choice ko contain karta hai, isliye us choice pe commit karna kabhi optimum tak pahunchne ki raah band nahi karta. (Dekho Greedy-Choice Property.)
- Optimal substructure — poore problem ka optimal solution subproblems ke optimal solutions se bana hota hai (dekho Optimal Substructure). Dono ko typically ek Exchange Argument se prove kiya jaata hai. 0/1 knapsack mein optimal substructure hai lekin greedy-choice property nahi hai, isliye greedy fail karta hai.
Exercise 1.2
Parent ke items ke liye — A, B, C — har compute karo aur greedy sort order likho.
Recall Solution
Descending order: . Greedy is order mein list walk karta hai.
Level 2 — Application
Exercise 2.1
Parent instance () pe density-greedy run karo. Chosen set, total value, aur wasted capacity batao.
Recall Solution
walk karo remaining capacity ke saath, start :
- A lo (needs 10 ≤ 50): value , .
- B lo (needs 20 ≤ 40): value , .
- C try karo (needs 30 > 20): fit nahi hota, skip. Chosen , total value , wasted capacity .
Exercise 2.2
Ab ke liye har feasible subset enumerate karo aur true optimum dhundho. Confirm karo ki greedy optimal nahi hai.
Recall Solution
Saare subsets, weight phir value (feasible = weight ≤ 50):
| Subset | weight | value | feasible? |
|---|---|---|---|
| 0 | 0 | ✓ | |
| A | 10 | 60 | ✓ |
| B | 20 | 100 | ✓ |
| C | 30 | 120 | ✓ |
| A,B | 30 | 160 | ✓ |
| A,C | 40 | 180 | ✓ |
| B,C | 50 | 220 | ✓ |
| A,B,C | 60 | — | ✗ (over 50) |
Optimum . Greedy ko mila, isliye greedy yahaan optimal nahi hai. (Yahi Fractional Knapsack–vs–0/1 contrast hai: sirf 0/1 capacity strand kar sakta hai.)
Level 3 — Analysis
Exercise 3.1
Parent instance ke liye DP table fill karo, lekin sirf un capacities ke liye jo actually chahiye: . read off karo.
Recall Solution
Recurrence: agar , warna . Base row .
Yeh do cases sab kuch kyun cover karte hain: item ke sirf do possible fates hain — ya toh woh chosen set mein hai ya nahi hai. Yeh saare solutions ka complete, non-overlapping split hai.
- Out (skip): agar choose nahi kiya, toh best jo hum kar sakte hain woh hai baaki items ka best usi capacity mein — exactly .
- In (take): agar choose kiya, toh woh capacity consume karta hai aur contribute karta hai; jo bhi uske saath pack hoga woh items ka optimal pack hona chahiye reduced capacity mein — yeh hai , aur yeh sirf tab legal hai jab . Kyunki "in" aur "out" saari possibilities cover karte hain, dono ka lena guaranteed true best hai — aur yeh fact ki har branch ek optimal chote answer ko reuse karta hai exactly Optimal Substructure hai. Isliye DP kabhi early commit nahi karta: woh ek guess karne ki jagah dono fates evaluate karta hai.
Items order A, B, C.
| 0 | 10 | 20 | 30 | 40 | 50 | |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | |
| (A) | 0 | 60 | 60 | 60 | 60 | 60 |
| (B) | 0 | 60 | 100 | 160 | 160 | 160 |
| (C) | 0 | 60 | 100 | 160 | 180 | 220 |
Sample step, : C skip karo → ; C lo → . Max hai . ✓ B+C se match karta hai.
Exercise 3.2
Table mein exact cell point karo jahan DP greedy ko beat karta hai, aur explain karo kyun greedy wahan pahunch nahi sakta tha.
Recall Solution
Cell (aur uska parent , matlab "size-20 slot mein B use karo"). DP ne branch "A ko bilkul skip karo" consider kiya — yehi cheez hai jo B (20) aur C (30) dono ke liye room chhod deti hai taaki woh exactly 50 sum karein. Greedy ne pehle A pe commit kiya (highest density) aur kabhi un-commit nahi kar sakta, isliye usne kabhi "skip A" universe explore nahi kiya. Yahi Optimal Substructure ka payoff hai: DP har capacity ke liye best answer rakhta hai, un capacities ke liye bhi jo sirf tab sense banaati hain jab tum ek dense item skip karo.
Level 4 — Synthesis
Exercise 4.1
Ek nayi 3-item instance banao jahan density-greedy optimum ka aadhe se bhi kam achieve kare. Apne numbers prove karo.
Recall Solution
Capacity . Items (listed X, Y, Z — yeh order tie-breaks bhi fix karta hai):
- X: , , (densest, lekin tiny).
- Y: , , .
- Z: , , .
Greedy: X pehle lo (density 2.0) → . Ab Y aur Z dono ko weight 100 chahiye, lekin sirf 99 units bacha hai, isliye dono fit nahi hote. Greedy value . Optimum: sirf Y lo (ya sirf Z), weight , value . Toh greedy , optimum . Ratio — density-greedy yahaan 50 guna bura hai. Mechanism exactly parent ki warning hai: X ka single unit of weight remaining capacity ko 100 se 99 kar deta hai, jo heavy 100-value items ko lock out karne ke liye kaafi hai. X ki value ko ki taraf push karne se wahi blocking effect rehti hai aur ratio → 0 ho jaata hai, yeh dikhata hai ki density-greedy arbitrarily bad ho sakta hai.
Exercise 4.2
Parent se guaranteed cheap heuristic state karo aur use Exercise 4.1 instance pe apply karo. Dikhao ki ab woh ½-bound hit karta hai.
Recall Solution
Heuristic: return karo.
- Density-greedy value (4.1 se).
- Best single item jo mein fit ho: Y ya Z, value .
- . Optimum hai , toh heuristic return karta hai. ✓ Yeh ½-approximation guarantee meet karta hai (dekho Approximation Algorithms). "Best single item" clause hi woh case rescue karta hai jahan ek heavy item poora answer hai.
Level 5 — Mastery
Exercise 5.1
DP mein run karta hai. Precisely explain karo kyun yeh proof nahi hai ki 0/1 knapsack "easy" hai (usual sense mein polynomial-time), aur sahi vault concept se link karo.
Recall Solution
pseudo-polynomial hai: yeh ki numeric value mein polynomial hai, likhne ke liye lagte bits ki sankhya mein nahi. likhne mein bits lagte hain, toh — runtime input length mein exponential hai. Agar hum (aur weights) ko jaisa badhne dein, toh table mein cells honge aur DP intractable ho jaayega. 0/1 knapsack actually NP-hard hai; koi known algorithm input ki bit-length mein polynomial nahi hai. Isliye genuine, useful bound sirf tabhi hai jab chota ho.
Exercise 5.2
Exchange Argument recall karo jo Fractional Knapsack ke liye greedy optimal prove karta hai. Woh single line point out karo jo 0/1 mein illegal ho jaati hai, aur apne words mein consequence explain karo.
Recall Solution
Sliver ki definition. ek arbitrarily small positive weight increment hai, matlab hum limit consider karte hain, aur hum hamesha exactly utna hi remove karte hain jitna add karte hain, toh swap weight-preserving hai by construction. Fractional exchange: ek optimal solution diya jo densest available item ka kuch miss karta hai, ka weight ka ek sliver lo aur ek equal mass kisi lower-density item ka remove karo. Total weight unchanged hai (humne add kiya aur remove kiya), aur value strictly badhti hai (higher density in, lower density out) — ek contradiction jab tak optimum already ka jitna ho sakta tha utna use na kar le. 0/1 mein illegal line: "mass ka ek sliver lo." 0/1 mein sirf allowed swap hai poora item, isliye ab 0 ki taraf shrink nahi ho sakta — woh ek full weight pe jump karta hai. Woh whole swap total weight change karta hai aur violate kar sakta hai — exchange guaranteed feasible nahi hai. Consequence: koi feasible weight-preserving exchange nahi hone se yeh proof nahi ho sakta ki greedy choice safe hai, isliye greedy-optimality collapse ho jaati hai aur hum Dynamic Programming pe fall back karte hain.
Exercise 5.3 (capstone)
Ek 4-item instance design karo jahan (a) density-greedy fail kare, (b) value-greedy bhi fail kare, lekin woh alag-alag sets choose karke fail karein. Dono greedy answers aur true optimum do.
Recall Solution
. Items listed P, Q, R, S (listing order ties bhi fix karta hai, page-wide rule ke according):
- P: , ()
- Q: , ()
- R: , ()
- S: , ()
Note karo R aur S tie karte hain pe; tie-breaking rule R ko S se pehle consider karta hai (R pehle listed hai). (a) Density-greedy order : P lo (), Q lo (), R ko 30 chahiye skip, S ko 50 chahiye skip → set , value . (b) Value-greedy (value se sort karo: S R Q P): S lo (, ) → kuch fit nahi hota → set , value . True optimum: ≤ 50 wale feasible sets enumerate karo. Candidates: , (wt 50), , (wt 40). Best hai . Toh density-greedy → (set ), value-greedy → (set ) — alag-alag galat sets — aur optimum hai set , jo kisi bhi heuristic ne choose nahi kiya. Yeh parent ke lesson ka sharpest form hai: koi bhi single sort key optimum nahi dhundh sakta.
Recall Poore page ka ek-line summary
Greedy (kisi bhi key se) capacity strand kar sakta hai ya hog kar sakta hai; fix hai DP ke saath, jo parent instance pe true optimum deta hai aur un har instances ko correctly handle karta hai jo tumne yahaan banaye.
Connections
- When greedy fails — 0 - 1 knapsack counter-example (index 3.7.5) — parent topic
- 3.7.05 When greedy fails — 0 - 1 knapsack counter-example (Hinglish) — Hinglish version
- Greedy Algorithms · Fractional Knapsack · Dynamic Programming
- Optimal Substructure · Greedy-Choice Property · Exchange Argument
- NP-hardness · Approximation Algorithms