Problem statement
- A knapsack (literally a backpack) with capacity
Wpounds (56:21,57:39). nitems; itemihas weightw_iand valuev_i(56:21,57:39).- Pack to maximize total value (
57:39): - Knapsack is NP-hard (
1:01:10).
Fractional knapsack is easy (and solved greedily)
- Relax to
0 <= x_i <= 1— not justx_i >= 0, otherwise you would be incentivized to take many copies of a single valuable item. (Multiple copies is a different variant; here you may only take a subset of thenitems.) (58:41,59:23,59:45) - Optimal fractional algorithm: sort items in decreasing order of
v_i / w_i("most bang per buck") and fill greedily; the last item is taken as whatever fraction still fits (1:00:06,1:00:54).
Exact integral knapsack via dynamic programming
DP 1 — O(nW), indexed by remaining capacity
f(i, b) = max value achievable using only items 1..i with b space left; the answer is f(n, W) (1:01:10, 1:01:48, 1:02:18).
f(i, b) = 0 if i = 0
f(i, b) = max( f(i-1, b), (skip item i)
v_i + f(i-1, b - w_i) if b - w_i >= 0 ) (take item i)
DP 2 — O(nV), indexed by achieved value
With V = sum_i v_i, define f(i, p) = the minimum weight needed to achieve value exactly p using items 1..i; a symmetric recurrence fills the table (1:04:12, 1:04:42, 1:05:11). These are simply two different algorithms, not one derived from the other (1:05:11, 1:05:47).
Why NP-hardness is not contradicted: pseudo-polynomial time
- "Polynomial time" means polynomial in the bit representation of the input (
1:03:44). - Writing
Wdown takes onlylog Wbits, so genuine polynomial time meanspoly(n, log W)(1:03:44). - Hence
O(nW)is not acceptable as polynomial — it is exponential in the input size (1:04:12). - The goal for the rest of the lecture: an approximation with
log W-style dependence (1:05:47).