Sub topic 11
An FPTAS for 0/1 Knapsack
This chapter assumes you already know the PTAS/FPTAS definitions from the previous chapter, can read dynamic programming code, and understand what pseudo-polynomial running time means.
The Problem and the Exact DP
You have n items, each with a positive value v_i and a weight w_i, and a knapsack with weight limit W. The goal is to pick a subset of items that fits within W and has the largest possible total value. The problem is NP-hard, but it has an exact dynamic program indexed by profit. For each profit p, the program records the minimum weight needed to reach exactly p. The table has n · V entries, where V is the sum of all values, so the running time is O(n · V). That is pseudo-polynomial: it grows with the size of the values, not with the number of bits needed to write them down.
The FPTAS uses this weakness directly. If the values were small integers, the DP would be fast. So we shrink the values.
Value Scaling
Pick a scaling factor K = ε · vmax / n and replace each value with v'_i = ⌊v_i / K⌋. Every scaled value is at most n/ε, so the total scaled profit is at most n²/ε. Running the exact DP on the scaled values then takes O(n³/ε) time, which is polynomial in both n and 1/ε.
Implementation
def knapsack_fptas(values, weights, capacity, eps):
n = len(values)
k = eps * max(values) / n
scaled = [int(v // k) for v in values]
total = sum(scaled)
INF = float("inf")
min_w = [0] + [INF] * total
keep = [[False] * (total + 1) for _ in range(n)]
for i in range(n):
for p in range(total, scaled[i] - 1, -1):
cand = min_w[p - scaled[i]] + weights[i]
if cand < min_w[p]:
min_w[p] = cand
keep[i][p] = True
best = max(p for p in range(total + 1) if min_w[p] <= capacity)
items, p = [], best
for i in range(n - 1, -1, -1):
if keep[i][p]:
items.append(i)
p -= scaled[i]
return sorted(items)
print(knapsack_fptas([60, 100, 120], [10, 20, 30], 50, 0.1))Here K = 4, the scaled values are 15, 25, 30, and the output is [1, 2], with value 220. That is the true optimum for this instance.
Proving the (1 − ε) Guarantee
Rounding down loses less than K per item, so v_i − K < K · v'_i ≤ v_i. Let O be an optimal set and S the set the algorithm returns. Because the DP is exact on the scaled values, S has scaled profit at least as large as O's:
v(S) ≥ K · v'(S) ≥ K · v'(O)K · v'(O) > v(O) − nK = OPT − ε · vmaxvmax ≤ OPT, sov(S) ≥ (1 − ε) · OPT
Step 3 holds only if the item with the largest value fits in the knapsack by itself.
Common Errors
| Error | Consequence |
|---|---|
| Scaling by `vmax` without first dropping items that don't fit | The bound `vmax ≤ OPT` breaks |
| Running the DP by weight instead of by profit | Scaling the values has no effect on running time |
| Reporting the scaled profit as the answer | The reported value is too low by a factor of about `K` |
| Iterating `p` upward in the 1-D table | Items get reused, which solves unbounded knapsack instead |
Exercise
For n = 50 items with vmax = 1000 and ε = 0.2, what is K, what is the largest possible scaled total profit, and how does the DP's running time change if ε is halved?
Show answer
K = 0.2 · 1000 / 50 = 4. Each scaled value is at most 1000/4 = 250, so the total is at most 50 · 250 = 12,500 = n²/ε. Halving ε to 0.1 doubles the table size to 25,000, so the running time doubles. The cost grows linearly in 1/ε, which is what makes this an FPTAS rather than only a PTAS.
Exercise
Why can't this scaling trick produce an FPTAS for a strongly NP-hard problem?
Show answer
An FPTAS with ε < 1/OPT would return the exact optimum when OPT is a polynomially bounded integer. That would give a polynomial-time exact algorithm for a strongly NP-hard problem, which implies P = NP.