3.7.5 · D1 · HinglishAlgorithm Paradigms

FoundationsWhen greedy fails — 0 - 1 knapsack counter-example

3,039 words14 min read↑ Read in English

3.7.5 · D1 · Coding › Algorithm Paradigms › When greedy fails — 0 - 1 knapsack counter-example

Yeh page har ek symbol, word, aur picture build karta hai jo parent note the parent topic silently assume karta hai. Ise top to bottom padho — har idea agle ke liye ek brick hai.


0. Pehle: kitne items hain, aur hum unhe kaise count karte hain

Hum aur se sabse pehle milte hain kyunki neeche har symbol inhi par lean karta hai — aap "item ka weight" tab tak nahi likh sakte jab tak yeh na pata ho ki kahan tak range karta hai.


1. Ek "item" kya hota hai? (weight aur value)

Letter ke neeche chhota subscript kehlata hai. Yeh bas ek name tag hai: matlab "item 1 ka weight", matlab "item 3 ki value". Yeh multiplication nahi hai, power nahi — yeh ek label hai, jaise jersey par number.

Figure — When greedy fails — 0 - 1 knapsack counter-example
Figure 1 — ek single item ek orange box ke roop mein draw kiya gaya "item i" label ke saath. Ek teal arrow uske left edge ki taraf point karta hai jis par "weight (room it uses)" tag hai; ek plum arrow uske top-right ki taraf point karta hai jis par "value (what it is worth)" tag hai. Ek takeaway: har item exactly do numbers bundle karta hai — ek weight aur ek value.


2. Bag aur uski limit: capacity

Note karo ki case matter karta hai: chhota = ek item ka weight; capital = bag ki limit. Same letter, bahut alag kaam — inhe mix karna sabse common beginner galti hai.


3. Sets aur subsets: actually kya hai

Symbol do allowed values ka set hai. likhne ka literal matlab hai "switch sirf 0 ya sirf 1 ho sakta hai." Yahi famous "0/1" hai problem ke naam mein.

Figure — When greedy fails — 0 - 1 knapsack counter-example
Figure 2 — left panel: ek 0/1 "switch" do boxes dikhata hai, "0 (leave it)" aur ek filled "1 (take it)", label ke neeche. Right panel: ek "dial" bar partly filled 0.6 tak ek plum arrow ke saath "take 0.6 of it", ke neeche. Ek takeaway: 0/1 knapsack sirf switch allow karta hai; fractional version dial allow karta hai.


4. symbol — chosen pile ko saath mein add karna

Ise left to right ek sentence ki tarah padho: "sum, har ke liye jo mein hai, ka."


5. Value-density — value per unit weight

Figure — When greedy fails — 0 - 1 knapsack counter-example
Figure 3 — ek graph jisme x-axis par weight hai aur y-axis par value hai. Teen lines origin se points A(10,60), B(20,100), C(30,120) tak jaati hain, orange, teal, plum colored. Jitni steep line, utni zyada density (A sabse steep hai 6 par, C sabse flat hai 4 par). Ek takeaway: density origin se item tak line ka slope hai.


6. "Greedy" ka matlab kya hai — woh algorithm jiske baare mein yeh page hai


7. DP table symbol — "best value so far"

bas is answers ki table ka naam hai, jaise ek spreadsheet ko naam dena. Brackets mein do cheezein, , row aur column hain jo ek cell pick karti hain. Final answer jo hume chahiye woh hai saare items allowed, poori capacity par.


8. Do aur phrases jo aap miloge


Prerequisite map

count n and index i

item weight w_i and value v_i

problem statement: max total value under limit

capacity W

set S subset of 1 to n and switch x_i

sum symbol

density rho = v over w

greedy heuristic: sort by density, take if fits

counter-example: greedy fails

table K of i and c

dynamic programming fix

exchange argument

Ise aise padho: count /index item numbers aur subset feed karte hain; woh plus capacity aur problem define karte hain; problem plus density greedy attempt build karta hai; greedy plus failed exchange argument counter-example produce karta hai; aur counter-example table par bana DP fix motivate karta hai.


Equipment checklist

kya count karta hai, aur index kahan tak range karta hai?
items ki total number hai; tak range karta hai, "jis bhi item ki hum baat kar rahe hain" ke liye stand-in hai.
mein subscript ka kya matlab hai?
Yeh ek name tag hai — "item number ka weight" — multiplication ya power nahi.
, , aur ki allowed range kya hai?
(strictly positive), , aur teeno standard problem mein positive whole numbers (integers) hain.
strictly positive kyun hona chahiye?
Kyunki density mein se divide hota hai; zero weight ise undefined bana deta.
aur mein kya fark hai?
Chhota ek single item ka weight hai; capital bag ki total capacity limit hai.
Ek set kya hai, aur kya ko ka subset banata hai?
Ek set distinct cheezein ki unordered collection hai; koi bhi collection hai jo item numbers mein se kuch, koi nahi, ya sab rakh ke bani ho.
words mein kya kehta hai?
Item ka switch sirf 0 (leave it) ya 1 (take it) ho sakta hai — koi fractions nahi.
hone par kya hota hai?
Zero — empty set par sum 0 hota hai (ek khali bag worth kuch nahi).
generally kya compute karta hai?
Har item ki value add karo jo tumhare chosen set mein hai — total loot value.
"s.t." kiska abbreviation hai aur kya matlab hai?
"Subject to" — woh constraint jo aapko manni hai, yahan ki total weight rahe.
Greedy algorithm yahan exactly kya karta hai?
Density compute karo, items ko highest-first sort karo, phir list walk karo har item ko lete hue agar woh abhi bhi fit hoti ho — kabhi reconsider nahi karte.
Value-density kya hai aur divide kyun karte hain?
, value per unit weight; divide karna bade aur chhote items ko same per-kilo scale par laata hai.
High aur high total value ek kyun nahi hain?
ek per-kilo rate hai; total value poora bag fill karne par depend karta hai, aur ek high-rate item bachi hui room waste kar sakta hai.
kya store karta hai?
Best total value jo sirf items use karke pretend capacity mein achieve ki ja sakti hai.
Poore problem ka answer kaunsa final cell deta hai?
— saare items allowed, poori capacity par.
0/1 knapsack mein exchange argument kyun fail karta hai?
Aap sirf whole items swap kar sakte ho, jo total weight change karta hai aur capacity break kar sakta hai, toh swap hamesha legal nahi hota.

Connections

  • When greedy fails — 0 - 1 knapsack counter-example (index 3.7.5) — woh parent jo yahan build har symbol use karta hai.
  • Fractional Knapsack — same symbols, lekin switch ek dial ban jaata hai.
  • Dynamic Programming — woh paradigm table ke peeche hai.
  • Greedy Algorithms — jahan density heuristic actually jeetta hai.
  • Greedy-Choice Property · Exchange Argument · Optimal Substructure — proof machinery.