Sub topic 16
Approximating Within a Factor: The Knapsack FPTAS
This chapter assumes you know the value-indexed DP from the earlier chapter, where dp[t] is the minimum weight needed to reach value exactly t. It also assumes you know why that DP's running time depends on the total value. If the values are huge, for example prices in cents up to 10^9, that table is too large to build. An approximation scheme trades a small, controlled loss in value for a table whose size depends only on n and the accuracy you ask for.
The Idea: Round Values Down
Choose an error tolerance eps between 0 and 1. Let vmax be the largest value among the items that fit in the knapsack on their own. Set a scale factor K = eps * vmax / n and replace each value v with floor(v / K). Every scaled value is now at most n / eps, so the total scaled value is at most n² / eps. Run the value-indexed DP on the scaled values, then report the chosen items' original values.
Implementation
The DP needs to report which items it took, not just a number. So it records, for each item, which entries of the table that item improved.
def knapsack_fptas(values, weights, capacity, eps):
items = [(i, v, w) for i, (v, w) in enumerate(zip(values, weights)) if w <= capacity]
if not items:
return 0, []
n = len(items)
vmax = max(v for _, v, _ in items)
K = eps * vmax / n
scaled = [int(v // K) for _, v, _ in items]
total = sum(scaled)
INF = float('inf')
dp = [0] + [INF] * total
took = [[False] * (total + 1) for _ in range(n)]
for j, (s, (_, _, w)) in enumerate(zip(scaled, items)):
for t in range(total, s - 1, -1):
if dp[t - s] + w < dp[t]:
dp[t] = dp[t - s] + w
took[j][t] = True
t = max(t for t in range(total + 1) if dp[t] <= capacity)
chosen = []
for j in range(n - 1, -1, -1):
if took[j][t]:
chosen.append(items[j][0])
t -= scaled[j]
chosen.reverse()
return sum(values[i] for i in chosen), chosen
print(knapsack_fptas([60, 100, 120], [10, 20, 30], 50, 0.5)) # (220, [1, 2])When eps = 0.5, K = 20 and the scaled values are 3, 5 and 6. The DP finds the optimum here, but in general it is only guaranteed to come close.
Why the Answer Is Within (1 − eps) of Optimal
Let S* be an optimal set and S the set the algorithm returns. Rounding down loses less than K per item, so K * floor(v / K) > v - K.
Smaximizes the scaled value, soscaled(S) ≥ scaled(S*).value(S) ≥ K * scaled(S) ≥ K * scaled(S*) > value(S*) - nK = OPT - eps * vmax.- Every remaining item fits alone, so
OPT ≥ vmax. That givesvalue(S) ≥ (1 - eps) * OPT.
Step 3 explains why the first line of the function removes items that are too heavy. If you skip that filter, vmax could belong to an item that can never be packed, and the bound breaks.
Cost
The table has at most n² / eps entries, and the DP processes it once per item, so the running time is O(n³ / eps). The running time is polynomial in both n and 1/eps, and that is what fully polynomial means.
| eps | Guarantee | Scaled-value bound (n = 100) | Work, roughly |
|---|---|---|---|
| 0.5 | ≥ 50% of OPT | 20,000 | 2 × 10^6 |
| 0.1 | ≥ 90% of OPT | 100,000 | 10^7 |
| 0.01 | ≥ 99% of OPT | 1,000,000 | 10^8 |
Exercise
With values [60, 100, 120], weights [10, 20, 30], capacity 50 and eps = 0.9, what are K and the scaled values, and what does the algorithm return?
Show answer
K = 0.9 * 120 / 3 = 36, so the scaled values are 1, 2 and 3. Items 1 and 2 reach scaled value 5 with weight 50, the best that fits, so the algorithm returns (220, [1, 2]).
Exercise
Why does the FPTAS not contradict the NP-hardness of 0/1 knapsack?
Show answer
To guarantee the exact optimum you would need eps small enough that rounding loses nothing, about eps < 1/vmax. Then the running time O(n³ / eps) grows with vmax, which is again pseudo-polynomial.