What property must a problem have for greedy to be provably optimal?
A greedy-choice property (a locally optimal choice is part of some globally optimal solution) plus optimal substructure, provable by an exchange argument.
Why does greedy solve fractional knapsack but not 0/1?
Fractional allows slivers, so you can always fill the bag exactly and the exchange argument holds; 0/1's integrality means a whole item can waste capacity and block a better combo. :::
State the 0/1 knapsack greedy counter-example.
W=50; A(10,60), B(20,100), C(30,120). Greedy by density takes A+B = 160; optimum is B+C = 220.
What is the value-density greedy heuristic?
Sort items by vi/wi descending, take each if it fits in remaining capacity.
Write the 0/1 knapsack DP recurrence.
K(i,c)=max(K(i−1,c),vi+K(i−1,c−wi)) if wi≤c, else K(i−1,c); base K(0,c)=0.
Runtime of the DP solution?
O(nW) (pseudo-polynomial — depends on the numeric value of W).
What approximation guarantee can you get cheaply for 0/1 knapsack?
A ½-approximation: take max(density-greedy value, best single item that fits).
Why does sorting by value alone fail?
A high-value heavy item can monopolize capacity and block a higher-total-value subset of lighter items.
Recall Feynman: explain to a 12-year-old
Imagine a small backpack and three toys. The "grab the best-bang-for-its-size toy first" trick works if you could cut toys into pieces — you'd always fill every empty corner. But real toys can't be cut! So sometimes grabbing the "best per size" toy first leaves an awkward empty space, and you miss out on two other toys that together would've fit perfectly and been worth more. To find the truly best haul, you have to try both choices for each toy — "take it" or "leave it" — and remember the best result for every possible amount of leftover space. That careful try-both method is dynamic programming.
Dekho, greedy algorithm ka funda simple hai: har step pe jo "abhi sabse achha" lag raha hai use utha lo aur peeche mud ke mat dekho. Kayi problems me ye trick perfectly kaam karti hai — jaise fractional knapsack me, jahan tum item ka tukda bhi le sakte ho. Wahan density (value per kg) ke hisab se sort karke utha lo, aur bag ka har corner bhar jaata hai, koi space waste nahi hoti. Iska proof bhi solid hai (exchange argument se).
Lekin 0/1 knapsack me ek bada twist hai: item ya to pura lo ya bilkul mat lo, tukda nahi chalega. Bas yahi cheez greedy ko tod deti hai. Example: capacity 50, items A(10kg,60), B(20kg,100), C(30kg,120). Greedy density se A aur B uthata hai (total 160), phir C nahi fit hota aur 20 unit space waste ho jaati hai. But asli optimal answer to B+C = 220 hai, jo poora bag bhar deta hai! Greedy ne A ko greedily uthaya, aur wahi A ne baaki ka space "block" kar diya.
Toh lesson kya hai? Greedy myopic hota hai — aage ka nahi sochta. Total value bag bharne se aati hai, sirf density se nahi. Iska sahi solution hai Dynamic Programming: har item ke liye "lo ya chhodo" dono try karo aur best yaad rakho — recurrence K(i,c)=max(K(i−1,c),vi+K(i−1,c−wi)), time O(nW). Yaad rakhna: "Greedy grabs, gaps remain" — greedy densest cheez utha leta hai, par jo gap chhodta hai, asli behtar answer wahi chhupa hota hai.