3.7.5 · D1Algorithm Paradigms

Foundations — When greedy fails — 0 - 1 knapsack counter-example

2,825 words13 min readBack to topic

This page builds every symbol, word, and picture that the parent note the parent topic silently assumes. Read it top to bottom — each idea is a brick for the next.


0. First: how many items, and how we count them

We meet and before anything else because every symbol below leans on them — you cannot write "the weight of item " until you know what ranges over.


1. What is an "item"? (weight and value)

The little below the letter is called a subscript. It is just a name tag: means "the weight of item 1", means "the value of item 3". It is not multiplication, not a power — it is a label, like the number on a jersey.

Figure — When greedy fails — 0 - 1 knapsack counter-example
Figure 1 — a single item drawn as an orange box labelled "item i". A teal arrow points to its left edge tagged "weight (room it uses)"; a plum arrow points to its top-right tagged "value (what it is worth)". The one takeaway: every item bundles exactly two numbers — one weight and one value.


2. The bag and its limit: capacity

Notice the case matters: small = weight of one item; capital = the bag's limit. Same letter, very different jobs — mixing them up is the most common beginner slip.


3. Sets and subsets: what actually is

The symbol is a set of two allowed values. Writing literally says "the switch is allowed to be only 0 or only 1". That is the famous "0/1" in the problem's name.

Figure — When greedy fails — 0 - 1 knapsack counter-example
Figure 2 — left panel: a 0/1 "switch" showing two boxes, "0 (leave it)" and a filled "1 (take it)", under the label . Right panel: a "dial" bar partly filled to 0.6 with a plum arrow "take 0.6 of it", under . The one takeaway: 0/1 knapsack allows only the switch; the fractional version allows the dial.


4. The symbol — adding a chosen pile together

Read it left to right as a sentence: "sum, over every that is in , of ."


5. Value-density — value per unit weight

Figure — When greedy fails — 0 - 1 knapsack counter-example
Figure 3 — a graph with weight on the x-axis and value on the y-axis. Three lines run from the origin to points A(10,60), B(20,100), C(30,120), coloured orange, teal, plum. The steeper the line, the higher the density (A is steepest at 6, C flattest at 4). The one takeaway: density is the slope of the line from the origin to the item.


6. What "greedy" means — the algorithm this page is about


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

is just a name for this table of answers, like naming a spreadsheet. The two things in the brackets, , are the row and column that pick out one cell. The final answer we care about is all items allowed, full capacity.


8. Two more phrases you'll meet


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

Read it as: the count /index feed the item numbers and the subset ; those plus capacity and define the problem; the problem plus density builds the greedy attempt; greedy plus the failed exchange argument produces the counter-example; and the counter-example motivates the DP fix built on the table .


Equipment checklist

What does count, and what does the index range over?
is the total number of items; ranges over , standing for "whichever item we mean."
What does the subscript in mean?
It is a name tag — "the weight of item number " — not multiplication or a power.
What is the allowed range of , , and ?
(strictly positive), , and all three are positive whole numbers (integers) in the standard problem.
Why must be strictly positive?
Because density divides by ; a zero weight would make that undefined.
What is the difference between and ?
Small is the weight of a single item; capital is the bag's total capacity limit.
What is a set, and what makes a subset of ?
A set is an unordered collection of distinct things; is any collection built by keeping some, none, or all of the item numbers.
What does say in words?
The switch for item may only be 0 (leave it) or 1 (take it) — no fractions.
What is when ?
Zero — the sum over an empty set is 0 (an empty bag is worth nothing).
What does compute in general?
Add up the value of every item that is in your chosen set — the total loot value.
What does "s.t." abbreviate and mean?
"Subject to" — the constraint you must obey, here that total weight stays .
What exactly does the greedy algorithm do here?
Compute density , sort items highest-first, then walk the list taking each item if it still fits — never reconsidering.
What is value-density and why divide?
, value per unit weight; dividing puts big and small items on the same per-kilo scale.
Why is a high not the same as a high total value?
is a rate per kilo; total value depends on filling the whole bag, and a high-rate item can waste leftover room.
What does store?
The best total value achievable using only items within a pretend capacity .
Which final cell answers the whole problem?
— all items allowed at full capacity.
Why does the exchange argument fail for 0/1 knapsack?
You can only swap whole items, which changes total weight and may break the capacity, so the swap isn't always legal.

Connections

  • When greedy fails — 0 - 1 knapsack counter-example (index 3.7.5) — the parent that uses every symbol built here.
  • Fractional Knapsack — same symbols, but the switch becomes a dial .
  • Dynamic Programming — the paradigm behind the table .
  • Greedy Algorithms — where the density heuristic does win.
  • Greedy-Choice Property · Exchange Argument · Optimal Substructure — the proof machinery.