3.7.5 · D3Algorithm Paradigms

Worked examples — When greedy fails — 0 - 1 knapsack counter-example

3,961 words18 min readBack to topic

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.


The scenario matrix

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 , 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 by hand Ex 10

Read each example's Forecast and guess before scrolling. That guess is the point.


Figure — When greedy fails — 0 - 1 knapsack counter-example
Figure 1. A single stacked bar up to the capacity line : A (orange, 10) + B (violet, 20) + C (magenta, 20) fills the bag exactly to with no gap, total value .

  1. Densities. , , . Why this step? Greedy's only input is the density ordering, so compute it first.
  2. Greedy walk. Take A (room , value ). Take B (room , value ). Try C (needs , room is exactly fits), value . Why this step? Greedy takes each item if it still fits; here nothing gets skipped.
  3. Greedy total , room left . 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.
  4. Is anything better? Total weight of all three is , 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 , OPT . Equal ✓.


Figure — When greedy fails — 0 - 1 knapsack counter-example
Figure 2. Two stacked bars up to the capacity line . Left (orange/violet) is greedy A+B, topping out at with a hatched empty GAP of and total value . Right (violet/magenta) is B+C, filling all with total value . The empty slot on the left is exactly the room item C needed.

  1. Densities. , , . Order: A, B, C. Why this step? Establish greedy's marching order.
  2. Greedy walk. Take A (room , val ). Take B (room , val ). Try C (needs ) → skip. Why this step? C can't be sliced (0/1 rule), so greedy stops with 20 units wasted.
  3. Greedy total . Why this step? Sum the values of the taken items A+B to record greedy's final score before comparing to OPT.
  4. Find OPT. Try B+C: weight ✓, value . 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 , OPT . Shortfall coins, i.e. greedy is of optimal (≈27% worse) ✓.


Figure — When greedy fails — 0 - 1 knapsack counter-example
Figure 3. Two stacked bars to . Left (magenta) is value-greedy: one giant item H filling all but worth only . Right (violet/orange) is P+Q filling all and worth . Same fill, far more value — the heavy item was a value hog.

  1. Sort by value (not density). Order: H, Q, P. Why this step? We're testing the tempting "maximize value → take valuable things first" heuristic.
  2. Value-greedy walk. Take H (weight , room , value ). Room is → skip P, skip Q. Why this step? H is heavy and valuable; grabbing it monopolizes the whole bag.
  3. Value-greedy total . 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.
  4. Find OPT. P+Q: weight ✓, value . Why this step? Two lighter items together out-value the single heavy hog.

Verify: value-greedy , OPT . ✓.


Figure — When greedy fails — 0 - 1 knapsack counter-example
Figure 4. A tiny capacity line at far below three item-weight bars (10, 20, 30). None reach — everything is over the line, so the loaded bar is empty (value 0).

  1. Check each item against . , , . Every item is too heavy. Why this step? The "take if it fits" test fails for all — the greedy walk takes nothing.
  2. Greedy total , room left . Why this step? Empty subset is the only legal solution.
  3. OPT. No subset (other than ) has weight , so OPT too. Why this step? A degenerate instance where greedy and OPT trivially agree at zero.

Verify: Greedy , OPT ✓.


  1. Total weight. . Why this step? If the whole set fits, the capacity constraint is inactive.
  2. Greedy walk. Take A, B, C — each fits in the ever-shrinking-but-still-ample room. Total . Why this step? Greedy never has to skip; it grabs all.
  3. OPT (take everything — adding an item never lowers a nonnegative-value total). Why this step? With slack capacity, OPT = sum of all values.

Verify: Greedy , OPT ✓.


  1. Densities — careful with the zeros. . For Z: , so by the infinite-density convention above, Z gets the priority label ("free value" ⇒ sorts first). For N: (sorts last). Why this step? 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.
  2. Greedy walk (rule as written). Take Z (label , weight → room stays , value ). Take A (room , value ). Try N (weight fits, so greedy takes it), value , room . Why this step? We obey "take if it fits" literally — N is taken even though it is useless, because the rule never inspects value.
  3. Greedy total (Z + A + N; N contributed ). Why this step? Sum the taken values ; the useless N leaves the total unchanged, which is exactly the point being demonstrated.
  4. OPT. Z is free positive value ⇒ always in (). A is the only positive-value weighted item (). N contributes so including or excluding it cannot change the score. OPT . 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 items"; but that guard is an optimization on top of the base rule, not the base rule itself.

Verify: Greedy , OPT ✓.


Figure — When greedy fails — 0 - 1 knapsack counter-example
Figure 5. Capacity line at . 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.

  1. Densities. , . Order: T, M. Why this step? T looks best per kilo.
  2. Greedy walk. Take T (room , value ). Try M (needs ) → skip. Why this step? The tiny item stole 1 unit of room, and that's enough to block the giant.
  3. Greedy total . Why this step? Only T was taken, so its value alone is greedy's score — record it before comparing.
  4. OPT. Just take M: weight ✓, value . Why this step? Dropping the tiny lure lets the giant fit.

Verify: Greedy , OPT , ratio ✓.


  1. Density-greedy value (from Ex 7) . Why this step? One of the two candidates.
  2. Best single item that fits. Both T () and M (, and so it fits) are candidates; the best is M . Why this step? This second candidate protects against exactly the Ex-7 disaster — a lone giant that greedy skipped.
  3. Heuristic answer . Why this step? Take the better of the two candidates.

Verify: HEUR , OPT , and ✓.


Figure — When greedy fails — 0 - 1 knapsack counter-example
Figure 6. A drone payload bar capped at kg. Naive dispatch loads laptop+monitor (orange/violet) to kg with a -kg empty gap, fees \16050$220$.

  1. Translate to knapsack. ; items A=laptop, B=monitor, C=printer. Why this step? Word problems are knapsack in disguise: capacity = payload, value = fee, integrality = "can't split a parcel".
  2. Density-greedy (what a naive dispatcher does). A then B = \1603020$ left). Why this step? Shows the tempting-but-wrong shortcut.
  3. Optimal load. monitor + printer kg , 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 , optimal ✓.


Figure — When greedy fails — 0 - 1 knapsack counter-example
Figure 7. The filled DP grid. Rows are (items unlocked, orange labels); columns are (violet labels). Each cell holds . The bottom-right cell is highlighted magenta; an arrow shows the "skip" link from the cell directly above.

  1. Row 0 (no items). for all . Why this step? Base case: no items → no value. Every DP grows from here.
  2. Row 1 (item ). For : . For : . Row1 . Why this step? One item worth 3 fits once we have room ; more room doesn't add value.
  3. Row 2 (item ).
    • : → too heavy → copy Row1 → .
    • : .
    • : .
    • : . Row2 . Why this step? At we take both items 1 and 2 ( kg, ).
  4. Row 3 (item ).
    • : → too heavy → copy Row2 → .
    • : .
    • : . Row3 . Why this step? At , keeping items 1+2 (value 7) beats taking item 3 alone (value 5).
  5. Answer , 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: , and brute force over all subsets of weight agrees ✓.


Recall Quick self-quiz over the matrix

When is density-greedy guaranteed correct on 0/1 knapsack? ::: When all items fit (, Cell 5) or trivially none fit (Cell 4) — i.e. when capacity never forces a choice. Does the fixed greedy rule take a 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? ::: vs — 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 mean? ::: The max value using only the first items with a sub-bag of capacity ; the answer is . Final answer of the DP table in Ex 10? ::: (items 1 and 2). How do you handle a zero-weight positive-value item? ::: Give it priority (always first, always take) — Cell 6.


Connections

  • Parent: When greedy fails — the theory these examples drill
  • Dynamic Programming — Ex 10 is DP by hand
  • Fractional Knapsack — where greedy is optimal (contrast to Ex 2); the source of the upper-bound lemma in Ex 8
  • Greedy Algorithms · Greedy-Choice Property · Exchange Argument — why the proof holds there, not here
  • Approximation Algorithms — the ½-guarantee of Ex 8
  • NP-hardness — why no fast exact greedy key exists
  • Optimal Substructure — the property the DP recurrence exploits