Approximating Knapsack
Rounding values into a fully polynomial-time approximation scheme.
Knapsack is NP-hard, yet for every you can find a packing worth at least times the optimum in time polynomial in both and . The trick is to throw precision away on purpose: round every value onto a coarse grid, and the pseudo-polynomial table over values becomes a polynomial one.
What you’ll learn
- Define -approximation, PTAS and FPTAS, and place knapsack, bin packing, scheduling and TSP among them.
- Prove that the better of the greedy prefix and the single most valuable item is a -approximation, with the fractional relaxation as the upper bound.
- Build the FPTAS: scale the values by , solve the table over scaled values, and prove the guarantee and the running time.
- Prove that a strongly NP-hard problem with polynomially bounded integer optimum has no FPTAS unless P = NP, and describe the state of the art, , and why it is believed optimal.
Approximation schemes, and a first guarantee
| Problem | What is achievable |
|---|---|
| 0/1 knapsack | An FPTAS (Ibarra & Kim, 1975); a PTAS came first (Sahni, 1975) |
| Scheduling on identical machines, part of the input | A PTAS (Hochbaum & Shmoys, 1987) but no FPTAS unless P = NP |
| Bin packing | No ratio below unless P = NP: deciding whether 2 bins suffice is partition |
| Metric TSP | Ratio (Christofides), and no PTAS unless P = NP |
| Euclidean TSP in the plane | A PTAS (Arora, 1998; Mitchell, 1999) |
A quick constant-factor algorithm comes first. Drop the items heavier than (they never fit) and sort the rest by value per weight, , best first. Greedy takes items in this order while they fit and stops at the first item that does not, the critical item; call the value taken . Greedy alone can be terrible: with items and it takes the first, cannot add the second, and gets 2 against an optimum of . One extra candidate repairs it.
Allow fractions of items: this relaxation can only raise the optimum. Its optimum is the greedy prefix plus the fitting fraction of the critical item (an exchange argument: see Proving Greedy Algorithms Correct):
The critical item fits alone, so , and a sum of two numbers is at most twice the larger one:
Sorting dominates, so the algorithm runs in time. The fractional optimum is also a cheap upper bound on , which branch-and-bound codes use to prune.
A table indexed by value
The FPTAS rests on the table of Pseudo-Polynomial Time that is indexed by value rather than weight. Let be the least total weight of a subset of the first items whose total value is exactly ( if there is none):
The optimum is the largest with . With the table costs time: polynomial in and in the values, independent of the weights and of .
Think first If every value were an integer between 1 and , how fast would this table solve knapsack exactly?
, so the table takes time: polynomial. Knapsack is hard only because the values (or the weights) can be huge. The FPTAS forces the values into that small range by rounding, and pays for it with a small loss.
Round, solve, and prove
Drop the items heavier than , so that every remaining item fits alone and . Fix and a grid step , round every value down onto the grid, , and solve the table over the scaled values exactly. Return the subset it finds, valued with the original . This is the scheme of Ibarra and Kim (1975) in its simplest form.
def knapsack_fptas(weights, values, W, eps):
"""Items with total value >= (1 - eps) * OPT, in O(n^3 / eps) time."""
items = [i for i in range(len(weights)) if weights[i] <= W] # heavier items never fit
if not items:
return []
K = eps * max(values[i] for i in items) / len(items)
scaled = [int(values[i] // K) for i in items] # values rounded down to a grid of step K
V = sum(scaled)
least = [0] + [float('inf')] * V # least[q]: least weight, scaled value q
took = [[False] * (V + 1) for _ in items]
for j, i in enumerate(items):
for q in range(V, scaled[j] - 1, -1):
if least[q - scaled[j]] + weights[i] < least[q]:
least[q] = least[q - scaled[j]] + weights[i]
took[j][q] = True
q = max(q for q in range(V + 1) if least[q] <= W)
chosen = []
for j in range(len(items) - 1, -1, -1): # walk the choices back
if took[j][q]:
chosen.append(items[j])
q -= scaled[j]
return chosen[::-1]
print(knapsack_fptas([6, 8, 1, 9], [40, 48, 38, 80], 9, 0.5)) # [3]: value 80, optimum 86The table over scaled values, with one bit per cell (took) to walk the choices back: the last pass that improved took item .
Rounding down loses less than one grid step per item:
Let be an optimal set and the set returned. is the best feasible set for the scaled values, so . Chain the inequalities, losing less than for each of the at most items of :
By the choice of , the total loss is , and because the most valuable item fits alone:
Every scaled value is at most , so the scaled total is small:
The table has rows of cells, each filled in :
That is polynomial in and , with no dependence on the sizes of the weights or values beyond -time arithmetic: a fully polynomial-time approximation scheme. Lawler (1979) cut the time to by rounding more cleverly and treating small items greedily.
| Item | Weight | Value | Scaled |
|---|---|---|---|
| A | 6 | 40 | 4 |
| B | 8 | 48 | 4 |
| C | 1 | 38 | 3 |
| D | 9 | 80 | 8 |
On this instance, the scheme with solves the scaled problem exactly and returns its set. What is the original value of the set it returns?
Show the answer
80
In scaled values D is worth 8, while B + C and A + C are worth only : rounding cost B and C 8 units each but D nothing. So the scheme returns D, worth 80, while the optimum is 86. The guarantee promises at least ; the actual ratio is . With the grid is fine enough to return B + C.
Limits, and the state of the art
Run the scheme with . For a maximization problem it returns a value with
and are integers and , so : the answer is exact (minimization is symmetric). The time is polynomial in and :
That is a pseudo-polynomial exact algorithm, which a strongly NP-hard problem has only if P = NP (Pseudo-Polynomial Time). Bin packing (optimum at most ) and scheduling on machines with in the input (optimum at most the total processing time) are examples: PTAS-type results are the best one can hope for.
Scheduling jobs on identical machines to minimize the finishing time, with part of the input, is strongly NP-hard and has a PTAS (Hochbaum & Shmoys, 1987). Which further result would show that P = NP?
- A PTAS whose running time is
- An FPTAS
- A -approximation in time
- An exact algorithm in time
Show the answer
B. An FPTAS
The optimum is an integer bounded by the total processing time, which is at most the unary length of the instance. An FPTAS run with below one over that bound would be an exact pseudo-polynomial algorithm for a strongly NP-hard problem, so it would show that P = NP.
For knapsack itself, the question is how fast an FPTAS can be. Sahni (1975) gave a PTAS that tries all small sets of items and fills up greedily; Ibarra and Kim (1975) gave the first FPTAS; Lawler (1979) and many later papers refined the rounding. In this century the target became running times of the form : (Chan, 2018), (Deng, Jin & Mao, 2023), and finally , found independently by Mao and by Chen, Lian, Mao and Zhang (2024). For unbounded knapsack, was known earlier (Jansen & Kraft).
Key takeaways
- A PTAS is polynomial for each fixed ; an FPTAS is polynomial in and . Knapsack has an FPTAS; strongly NP-hard problems with polynomially bounded integer optima do not, unless P = NP.
- Greedy by value per weight can be arbitrarily bad, but the better of the greedy prefix and the best single item is within a factor 2, because the fractional optimum is at most .
- The FPTAS rounds values down to multiples of , solves the table over scaled values in , and loses at most .
- The fastest schemes run in (2024), and a -convolution lower bound says the exponent 2 cannot be lowered.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Which running time, for every , makes a -approximation algorithm an FPTAS?
Show the answer
C.
An FPTAS must be polynomial in both and . is polynomial for each fixed but not in (a PTAS), and is exponential in (also only a PTAS).
The FPTAS sets the grid step to . Why and not , which would give a coarser grid?
- is larger than , so the grid is safer
- is unknown, while is computable and satisfies
- Rounding must use an item's value, not a sum of values
- Any constant would do; the choice affects only the running time
Show the answer
B. is unknown, while is computable and satisfies
Every item fits alone, so , and the total loss is then at most . The upper bound keeps the scaled values at most each, which bounds the table.
Items: with and with ; capacity 10. What do plain greedy (by value per weight) and "greedy or the best single item" return?
- Both return 10
- Plain greedy 2, the repaired version 10
- Plain greedy 12, the repaired version 12
- Both return 2
Show the answer
B. Plain greedy 2, the repaired version 10
Greedy takes (ratio 2), and then no longer fits: value 2. The repaired version compares with the best single item, worth 10, which is optimal here. Without the repair the ratio can be made arbitrarily small.
Bin packing is strongly NP-hard, and its optimum is an integer between 1 and . What does this imply?
- It has an FPTAS, because its optimum is small
- It has no FPTAS unless P = NP: running one with would solve it exactly in polynomial time
- It has no approximation algorithm with any constant ratio
- Nothing: strong NP-hardness concerns exact algorithms only
Show the answer
B. It has no FPTAS unless P = NP: running one with would solve it exactly in polynomial time
For a minimization problem with integer optimum at most , a -approximation with is off by less than 1, hence exact, and an FPTAS would deliver it in time polynomial in . That would solve a strongly NP-hard problem in polynomial time.
Further reading
- O. H. Ibarra & C. E. Kim, "Fast approximation algorithms for the knapsack and sum of subset problems," Journal of the ACM 22(4) (1975), 463–468.
- E. L. Lawler, "Fast approximation algorithms for knapsack problems," Mathematics of Operations Research 4(4) (1979), 339–356.
- V. V. Vazirani, Approximation Algorithms (Springer, 2001), chapter 8, "Knapsack".
- D. P. Williamson & D. B. Shmoys, The Design of Approximation Algorithms (Cambridge, 2011), §3.1.
- X. Mao, "(1 − ε)-Approximation of Knapsack in Nearly Quadratic Time," STOC 2024; independently L. Chen, J. Lian, Y. Mao & G. Zhang, "A Nearly Quadratic-Time FPTAS for Knapsack" (2024).