This page is a drill room. The parent note proved once that density-greedy fails. Here we hit every kind of instance so you never meet a case you haven't already seen worked out.
Before we start, one reminder of the vocabulary — no symbol used before it is named.
Each row below is a case class — a distinct way the greedy-vs-optimal story can play out. The worked examples afterward each carry a tag like [Cell 3] so you can see we covered them all.
Cell
Case class
What it stresses
Example
1
Greedy = OPT (lucky)
greedy can still be right
Ex 1
2
Greedy < OPT (the classic gap)
wasted capacity blocks a combo
Ex 2
3
Sort-by-value greedy fails
wrong key hogs capacity
Ex 3
4
Degenerate: nothing fits
zero-item answer
Ex 4
5
Degenerate: everything fits
capacity ≥ total weight
Ex 5
6
Zero-value / zero-weight items
edge inputs vi=0, wi=0
Ex 6
7
Greedy arbitrarily bad
tiny high-density item ruins it
Ex 7
8
½-approximation rescue
the guaranteed cheap fix
Ex 8
9
Real-world word problem
translate → solve
Ex 9
10
Exam twist: build the DP table
fill K(i,c) by hand
Ex 10
Read each example's Forecast and guess before scrolling. That guess is the point.
Figure 1. A single stacked bar up to the capacity line W=50: A (orange, 10) + B (violet, 20) + C (magenta, 20) fills the bag exactly to 50 with no gap, total value 230.
Densities.ρA=60/10=6, ρB=100/20=5, ρC=70/20=3.5.
Why this step? Greedy's only input is the density ordering, so compute it first.
Greedy walk. Take A (room 50→40, value 60). Take B (room 40→20, value 160). Try C (needs 20, room is exactly 20 → fits), value 230.
Why this step? Greedy takes each item if it still fits; here nothing gets skipped.
Greedy total =60+100+70=230, room left =0.
Why this step? Sum the taken values and confirm the bag is exactly full — zero waste is the warning sign that greedy might already be optimal.
Is anything better? Total weight of all three is 10+20+20=50=W, so all items fit at once. You literally cannot beat "take everything".
Why this step? When the whole set fits, OPT = sum of all values, no search needed.
Verify: Greedy =230, OPT =230. Equal ✓.
Figure 2. Two stacked bars up to the capacity line W=50. Left (orange/violet) is greedy A+B, topping out at 30 with a hatched empty GAP of 20 and total value 160. Right (violet/magenta) is B+C, filling all 50 with total value 220. The empty slot on the left is exactly the room item C needed.
Densities.ρA=6, ρB=5, ρC=4. Order: A, B, C.
Why this step? Establish greedy's marching order.
Greedy walk. Take A (room →40, val 60). Take B (room →20, val 160). Try C (needs 30>20) → skip.
Why this step? C can't be sliced (0/1 rule), so greedy stops with 20 units wasted.
Greedy total =160.Why this step? Sum the values of the taken items A+B to record greedy's final score before comparing to OPT.
Find OPT. Try B+C: weight 20+30=50=W ✓, value 100+120=220.
Why this step? The wasted 20 units in step 2 is a hint that a lower-density item (C) could fill the bag better. Swapping out A for C recovers that gap.
Verify: Greedy =160, OPT =220. Shortfall =60 coins, i.e. greedy is 160/220≈72.7% of optimal (≈27% worse) ✓.
Figure 3. Two stacked bars to W=50. Left (magenta) is value-greedy: one giant item H filling all 50 but worth only 130. Right (violet/orange) is P+Q filling all 50 and worth 220. Same fill, far more value — the heavy item was a value hog.
Sort by value (not density). Order: H(130), Q(120), P(100).
Why this step? We're testing the tempting "maximize value → take valuable things first" heuristic.
Value-greedy walk. Take H (weight 50, room 50→0, value 130). Room is 0 → skip P, skip Q.
Why this step? H is heavy and valuable; grabbing it monopolizes the whole bag.
Value-greedy total =130.Why this step? H is the only item taken, so its value alone is the heuristic's score — record it before finding the true optimum.
Find OPT. P+Q: weight 20+30=50=W ✓, value 100+120=220.
Why this step? Two lighter items together out-value the single heavy hog.
Verify: value-greedy =130, OPT =220. 130<220 ✓.
Figure 4. A tiny capacity line at W=5 far below three item-weight bars (10, 20, 30). None reach — everything is over the line, so the loaded bar is empty (value 0).
Check each item against W.wA=10>5, wB=20>5, wC=30>5. Every item is too heavy.
Why this step? The "take if it fits" test fails for all — the greedy walk takes nothing.
Greedy total =0, room left =5.
Why this step? Empty subset is the only legal solution.
OPT. No subset (other than ∅) has weight ≤5, so OPT =0 too.
Why this step? A degenerate instance where greedy and OPT trivially agree at zero.
Verify: Greedy =0, OPT =0 ✓.
Total weight.10+20+30=60≤100=W.
Why this step? If the whole set fits, the capacity constraint is inactive.
Greedy walk. Take A, B, C — each fits in the ever-shrinking-but-still-ample room. Total =60+100+120=280.
Why this step? Greedy never has to skip; it grabs all.
OPT =280 (take everything — adding an item never lowers a nonnegative-value total).
Why this step? With slack capacity, OPT = sum of all values.
Verify: Greedy =280, OPT =280 ✓.
Densities — careful with the zeros.ρA=6. For Z: wZ=0, so by the infinite-density convention above, Z gets the priority label +∞ ("free value" ⇒ sorts first). For N: ρN=0/15=0 (sorts last).
Why this step?w=0 has no finite ratio, so we use the convention we defined earlier — Z is tagged "always first"; a genuine ordering still exists: Z, A, N.
Greedy walk (rule as written). Take Z (label +∞, weight 0 → room stays 50, value 5). Take A (room 50→40, value 65). Try N (weight 15≤40 → fits, so greedy takes it), value 65+0=65, room →25.
Why this step? We obey "take if it fits" literally — N is taken even though it is useless, because the rule never inspects value.
Greedy total =65 (Z + A + N; N contributed 0).
Why this step? Sum the taken values 5+60+0; the useless N leaves the total unchanged, which is exactly the point being demonstrated.
OPT. Z is free positive value ⇒ always in (+5). A is the only positive-value weighted item (+60). N contributes 0 so including or excluding it cannot change the score. OPT =5+60=65.Why this step? Here greedy = OPT anyway — the zero-value item was harmless because enough room remained. In a tighter bag, blindly taking N could waste room a positive item needed, which is why real solvers add a guard "don't take vi=0 items"; but that guard is an optimization on top of the base rule, not the base rule itself.
Verify: Greedy =65, OPT =65 ✓.
Figure 5. Capacity line at W=1000. Greedy bar (orange) shows a sliver of height 1 (item T, value 2) with a vast hatched gap of 999. Optimal bar (magenta) is one solid block filling all 1000 (item M, value 1500). The sliver blocked the giant.
Densities.ρT=2/1=2, ρM=1500/1000=1.5. Order: T, M.
Why this step? T looks best per kilo.
Greedy walk. Take T (room 1000→999, value 2). Try M (needs 1000>999) → skip.
Why this step? The tiny item stole 1 unit of room, and that's enough to block the giant.
Greedy total =2.Why this step? Only T was taken, so its value alone is greedy's score — record it before comparing.
OPT. Just take M: weight 1000=W ✓, value 1500.
Why this step? Dropping the tiny lure lets the giant fit.
Verify: Greedy =2, OPT =1500, ratio <0.5 ✓.
Density-greedy value (from Ex 7) =2.
Why this step? One of the two candidates.
Best single item that fits. Both T (v=2) and M (v=1500, and 1000≤1000 so it fits) are candidates; the best is M =1500.
Why this step? This second candidate protects against exactly the Ex-7 disaster — a lone giant that greedy skipped.
Heuristic answer =max(2,1500)=1500.Why this step? Take the better of the two candidates.
Verify: HEUR =1500, OPT =1500, and 1500≥0.5×1500 ✓.
Figure 6. A drone payload bar capped at 50 kg. Naive dispatch loads laptop+monitor (orange/violet) to 30 kg with a 20-kg empty gap, fees \160.Optimalloadsmonitor+printer(violet/magenta)fillingall50kg,fees$220$.
Translate to knapsack.W=50; items A=laptop(10,60), B=monitor(20,100), C=printer(30,120).
Why this step? Word problems are knapsack in disguise: capacity = payload, value = fee, integrality = "can't split a parcel".
Density-greedy (what a naive dispatcher does). A then B = \160,printerskipped(needs30,only20$ left).
Why this step? Shows the tempting-but-wrong shortcut.
Optimal load. monitor + printer =20+30=50 kg =W, fees \100 + $120 = $220$.
Why this step? Fills the payload exactly; the story's "can't split" is the 0/1 constraint that makes DP necessary.
Verify: naive =160, optimal =220 ✓.
Figure 7. The filled DP grid. Rows are i=0,1,2,3 (items unlocked, orange labels); columns are c=0..5 (violet labels). Each cell holds K(i,c). The bottom-right cell K(3,5)=7 is highlighted magenta; an arrow shows the "skip" link from the cell directly above.
Row 0 (no items).K(0,c)=0 for all c∈{0,1,2,3,4,5}.
Why this step? Base case: no items → no value. Every DP grows from here.
Row 1 (item w=2,v=3). For c<2: K=0. For c≥2: max(0,3+K(0,c−2))=3. Row1 =[0,0,3,3,3,3].
Why this step? One item worth 3 fits once we have room ≥2; more room doesn't add value.
Row 2 (item w=3,v=4).
c=0,1,2: w2=3>c → too heavy → copy Row1 → 0,0,3.
c=3: max(K1(3)=3,4+K1(0)=4)=4.
c=4: max(K1(4)=3,4+K1(1)=4)=4.
c=5: max(K1(5)=3,4+K1(2)=4+3=7)=7.
Row2 =[0,0,3,4,4,7].
Why this step? At c=5 we take both items 1 and 2 (2+3=5 kg, 3+4=7).
Row 3 (item w=4,v=5).
c=0,1,2,3: w3=4>c → too heavy → copy Row2 → 0,0,3,4.
Answer K(3,5)=7, achieved by items 1+2.
Why this step? Bottom-right cell is the whole problem's answer — no early commitment, both take/skip tried at every cell.
Verify:K(3,5)=7, and brute force over all subsets of weight ≤5 agrees ✓.
Recall Quick self-quiz over the matrix
When is density-greedy guaranteed correct on 0/1 knapsack? ::: When all items fit (W≥∑wi, Cell 5) or trivially none fit (Cell 4) — i.e. when capacity never forces a choice.
Does the fixed greedy rule take a v=0 item that fits? ::: Yes — the rule is "take iff it fits" and never inspects value; Ex 6 shows it takes N and simply gains nothing.
Ex 7 greedy score vs OPT? ::: 2 vs 1500 — greedy captured ~0.13%, proving no guarantee.
What two candidates does the ½-approximation compare? ::: density-greedy's value, and the single most valuable item that fits.
What does K(i,c) mean? ::: The max value using only the first i items with a sub-bag of capacity c; the answer is K(n,W).
Final answer of the DP table in Ex 10? ::: K(3,5)=7 (items 1 and 2).
How do you handle a zero-weight positive-value item? ::: Give it priority +∞ (always first, always take) — Cell 6.