Intuition The one core idea
The 0/1 knapsack asks: given a bag that can only carry so much weight, which whole items should you pack to make your loot worth the most? The whole story of "when greedy fails" is really the story of one word — "whole" : because you can't slice items, grabbing the "best-looking" one first can waste space that a smarter combination would have used perfectly.
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.
n and the index i
==n == is how many items exist in total . If there are 3 items, n = 3 .
We give each item a number from 1 to n : item 1 , item 2 , …, item n . The letter ==i == is a stand-in for "whichever item we're talking about right now" — it ranges over i = 1 , 2 , … , n .
So "for every i " just means "for item 1, then item 2, and so on up to item n ."
We meet n and i before anything else because every symbol below leans on them — you cannot write "the weight of item i " until you know what i ranges over.
Definition An item, its weight, and its value
An item is one object you might put in the bag. Item i (for i = 1 , … , n ) carries two numbers:
its weight — how much room / load it uses up. We write the weight of item i as ==w i ==.
its value — how much that object is worth to you. We write it as ==v i ==.
Allowed values: we require w i > 0 (every item has some positive weight — a zero-weight item makes no sense here and would break the density formula later), and v i ≥ 0 (value is never negative — an item is worth something or nothing, never a penalty). In the standard problem w i , v i and W are positive whole numbers (integers) , which is what lets the DP table below have a finite grid of cells.
The little i below the letter is called a subscript . It is just a name tag : w 1 means "the weight of item 1", v 3 means "the value of item 3". It is not multiplication, not a power — it is a label, like the number on a jersey.
Figure 1 — a single item drawn as an orange box labelled "item i". A teal arrow points to its left edge tagged "weight w i (room it uses)"; a plum arrow points to its top-right tagged "value v i (what it is worth)". The one takeaway: every item bundles exactly two numbers — one weight and one value.
Intuition Why two separate numbers?
If items only had value , you'd take everything. If they only had weight , nothing would be worth choosing. The whole problem is interesting because value and weight pull in different directions — a heavy thing might be precious, a light thing might be junk. The tension between w i and v i is the entire game.
W
The knapsack (bag) can hold a maximum total weight called the capacity , written with a capital ==W ==. If you add up the weights of everything you packed, that sum is not allowed to exceed W . Like the weights, W is a positive whole number (W > 0 ).
Notice the case matters: small w i = weight of one item ; capital W = the bag's limit . Same letter, very different jobs — mixing them up is the most common beginner slip.
Intuition Picture the capacity as a shelf edge
Imagine stacking items and watching a red line at height W . As long as your stack stays at or below the line, the load is legal. Cross the line and the bag "breaks" — that choice is forbidden.
Definition A set, and what makes
S a subset
A set is just an unordered collection of distinct things — like a bag of labels with no repeats and no particular order. We write a set with curly braces: { 1 , 3 } is "the collection containing items 1 and 3."
The set of all item numbers is { 1 , 2 , … , n } .
A subset is any set you can build by keeping some (or none, or all) of those numbers. ==S == is the subset of items you decided to pack .
"i ∈ S " (read "i in S" ) means "item i is one of the ones you took." "i ∈ / S " means you left it.
The empty set ∅ = { } is the subset that takes nothing — a perfectly valid choice (pack an empty bag).
Definition The indicator switch
x i
==x i == is a yes/no switch for item i : x i = 1 means "I took it" (i ∈ S ), x i = 0 means "I left it" (i ∈ / S ). Nothing in between. The set S and the switches x i are two ways of saying the same choice.
The symbol { 0 , 1 } is a set of two allowed values . Writing x i ∈ { 0 , 1 } literally says "the switch x i is allowed to be only 0 or only 1" . That is the famous "0/1" in the problem's name.
Intuition Why a 0/1 switch and not a dial?
A dial that could sit at, say, 0.4 would mean "take 40% of the item" — cutting it into pieces. The knapsack story exists precisely because real objects can't be cut : a laptop at 40% is a broken laptop worth nothing. So we forbid the dial and keep only the switch. Later we'll meet the Fractional Knapsack , which does allow the dial — and that one tiny change is why greedy works there and fails here.
Figure 2 — left panel: a 0/1 "switch" showing two boxes, "0 (leave it)" and a filled "1 (take it)", under the label x i ∈ { 0 , 1 } . Right panel: a "dial" bar partly filled to 0.6 with a plum arrow "take 0.6 of it", under x i ∈ [ 0 , 1 ] . The one takeaway: 0/1 knapsack allows only the switch; the fractional version allows the dial.
Definition Sigma, the "add-them-all" symbol
The big Greek letter ==∑ == (sigma) means "add up" . The writing underneath tells you what to add .
∑ i ∈ S w i = (add the weight w i of every item i that is in S )
So if S = { A , C } , then ∑ i ∈ S w i = w A + w C .
Edge case — empty set: if S = ∅ there is nothing to add, so by convention ∑ i ∈ ∅ w i = 0 (an empty sum is 0 ). This matches intuition: an empty bag weighs nothing and is worth nothing.
Read it left to right as a sentence: "sum, over every i that is in S , of w i ."
ρ i
To rank items, we need a single "how good is this item?" number. We use value-density : how much value you get per kilogram .
ρ i = w i v i
The symbol ==ρ == is the Greek letter rho (say "row"); here it just names this ratio. A fraction w i v i means "value shared out over the weight" — divide the worth by the room it costs. This is exactly why we insisted w i > 0 earlier : dividing by a zero weight would be undefined.
Intuition Why divide? — picture two blocks
Item A: value 60, weight 10 → ρ = 6 (worth 6 per kilo). Item C: value 120, weight 30 → ρ = 4 . Per kilo, A is "juicier." Division is the tool because it puts big items and small items on the same scale — you couldn't compare a 10-kg item to a 30-kg item by raw value alone; dividing normalises them to a per-kilo price tag.
Figure 3 — a graph with weight w i on the x-axis and value v i 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 ρ = v / w (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.
Definition The greedy algorithm (density heuristic)
"Greedy " is a strategy that, at every step, grabs whatever looks best right now and never reconsiders . For knapsack the "best right now" score is the density ρ i . The greedy method runs like this:
Compute ρ i = v i / w i for every item.
Sort the items from highest density to lowest.
Walk down that sorted list; take an item if it still fits in the space that's left, otherwise skip it.
Because it commits to each item the moment it's reached and never undoes that choice, greedy is fast but short-sighted — and that short-sightedness is the whole reason it can fail on 0/1 knapsack.
Common mistake Density is a
rate , not a total
A high ρ i tells you an item is efficient per kilo — it does not tell you the total loot is best. That gap between "best rate" and "best total" is exactly where greedy trips. Keep this seed in mind; the parent note grows the whole failure from it.
K ( i , c )
When we later fix the problem, we build a memory of partial answers. ==K ( i , c ) == reads as "the best total value you can get using only items 1 through i , if the bag capacity were c ."
i — how many of the items (numbered 1 to n ) you're allowed to consider so far (1, 2, 3, …).
c — a pretend capacity you're testing (0, 1, 2, …, up to W ).
K is just a name for this table of answers, like naming a spreadsheet. The two things in the brackets, ( i , c ) , are the row and column that pick out one cell. The final answer we care about is K ( n , W ) — all items allowed, full capacity.
Intuition Why remember answers at all?
The greedy algorithm commits early and never reconsiders. The fix must be allowed to say "what if I'd left that item?" To do that cheaply it writes down the best result for every ( i , c ) so it never re-solves the same little sub-question. That written-down memory is the heart of Dynamic Programming , and K ( i , c ) is the cell where each memory lives.
Definition Exchange argument & greedy-choice property (plain words)
An Exchange Argument is a proof trick: "if the best answer didn't use my greedy pick, I'll swap my pick in for something worse and show the answer only gets better." It needs the swap to be legal (not break the capacity).
The Greedy-Choice Property is the condition that makes greedy correct: "a locally best choice is always part of some overall-best answer."
The parent's punchline is that in 0/1 knapsack the exchange is illegal (you can only swap whole items, which changes weight), so the greedy-choice property fails .
item weight w_i and value v_i
problem statement: max total value under limit
set S subset of 1 to n and switch x_i
greedy heuristic: sort by density, take if fits
counter-example: greedy fails
Read it as: the count n /index i feed the item numbers and the subset S ; 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 K .
What does n count, and what does the index i range over? n is the total number of items; i ranges over 1 , 2 , … , n , standing for "whichever item we mean."
What does the subscript in w i mean? It is a name tag — "the weight of item number i " — not multiplication or a power.
What is the allowed range of w i , v i , and W ? w i > 0 (strictly positive), v i ≥ 0 , and all three are positive whole numbers (integers) in the standard problem.
Why must w i be strictly positive? Because density ρ i = v i / w i divides by w i ; a zero weight would make that undefined.
What is the difference between w i and W ? Small w i is the weight of a single item; capital W is the bag's total capacity limit.
What is a set, and what makes S a subset of { 1 , … , n } ? A set is an unordered collection of distinct things; S is any collection built by keeping some, none, or all of the item numbers.
What does x i ∈ { 0 , 1 } say in words? The switch for item i may only be 0 (leave it) or 1 (take it) — no fractions.
What is ∑ i ∈ S v i when S = ∅ ? Zero — the sum over an empty set is 0 (an empty bag is worth nothing).
What does ∑ i ∈ S v i compute in general? Add up the value v i of every item i that is in your chosen set S — the total loot value.
What does "s.t." abbreviate and mean? "Subject to" — the constraint you must obey, here that total weight stays ≤ W .
What exactly does the greedy algorithm do here? Compute density v i / w i , sort items highest-first, then walk the list taking each item if it still fits — never reconsidering.
What is value-density ρ i and why divide? ρ i = v i / w i , value per unit weight; dividing puts big and small items on the same per-kilo scale.
Why is a high ρ i not the same as a high total value? ρ i is a rate per kilo; total value depends on filling the whole bag, and a high-rate item can waste leftover room.
What does K ( i , c ) store? The best total value achievable using only items 1.. i within a pretend capacity c .
Which final cell answers the whole problem? K ( n , W ) — 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.
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 x i becomes a dial x i ∈ [ 0 , 1 ] .
Dynamic Programming — the paradigm behind the table K ( i , c ) .
Greedy Algorithms — where the density heuristic does win.
Greedy-Choice Property · Exchange Argument · Optimal Substructure — the proof machinery.