Approximation Algorithms: Knapsack

This is the lecture notes from Chandra Chekuri's CS583 course on Approximation Algorithms. Chapter 3: Knapsack. You can read Chapter 4: Packing Problems, here. Chapter 2: Covering problems, here.
Chapter 3
In this lecture we explore the Knapsack problem. This problem provides a good basis for learning some important procedures used for approximation algorithms that give better solutions at the cost of higher running time.

3.1 The Knapsack Problem

In the Knapsack problem we are given a number (knapsack capacity) B 0 B 0 B >= 0B \geq 0, and a set N N NN of n n nn items; each item i i ii has a given size s i 0 s i 0 s_(i) >= 0s_{i} \geq 0 and a profit p i 0 p i 0 p_(i) >= 0p_{i} \geq 0. We will assume that all the input numbers are integers (or more generally rationals). Given a subset of the items A N A N A sube NA \subseteq N, we define two functions, s ( A ) = i A s i s ( A ) = i A s i s(A)=sum_(i in A)s_(i)s(A)=\sum_{i \in A} s_{i} and p ( A ) = i A p i p ( A ) = i A p i p(A)=sum_(i in A)p_(i)p(A)=\sum_{i \in A} p_{i}, representing the total size and profit of the group of items respectively. The goal is to choose a subset of the items, A A AA, such that s ( A ) B s ( A ) B s(A) <= Bs(A) \leq B and p ( A ) p ( A ) p(A)p(A) is maximized. We will assume, without loss of generality, that s i B s i B s_(i) <= Bs_{i} \leq B for all i i ii; we can discard items that do not satisfy this constraint.
It is not difficult to see that if all the profits are identical (say 1), the natural greedy algorithm that inserts items in the order of non-increasing sizes yields. Assuming the profits and sizes are integral, we can still find an optimal solution to the problem using dynamic programming in either O ( n B ) O ( n B ) O(nB)O(n B) or O ( n P ) O ( n P ) O(nP)O(n P) time, where P = i = 1 n p i P = i = 1 n p i P=sum_(i=1)^(n)p_(i)P=\sum_{i=1}^{n} p_{i}. These are standard exercises. While these algorithms appear to run in polynomial time, it should be noted that B B BB and P P PP can be exponential in the size of the input written in binary. We call such algorithms pseudo-polynomial time algorithms as their running times are polynomial when numbers in the input are given in unary. Knapsack is a classical NP-Hard problem and these results (and the proof of NP-Hardness) show that the hardness manifests itself when the numbers are large (exponential in n n nn which means that the number of bits in the size or profit are polynomial in n n nn).

3.1.1 A Greedy Algorithm

Consider the following greedy algorithm for the Knapsack problem which we will refer to as GreedyKnapsack. We sort all the items by the ratio of their profits to their sizes so that p 1 s 1 p 2 s 2 p n s n p 1 s 1 p 2 s 2 p n s n (p_(1))/(s_(1)) >= (p_(2))/(s_(2)) >= cdots >= (p_(n))/(s_(n))\frac{p_{1}}{s_{1}} \geq \frac{p_{2}}{s_{2}} \geq \cdots \geq \frac{p_{n}}{s_{n}}. Afterward, we greedily take items in this order as long as adding an item to our collection does not exceed the capacity of the knapsack. It turns out that this algorithm can be arbitrarily bad. Suppose we only have two items in N N NN. Let s 1 = 1 , p 1 = 2 s 1 = 1 , p 1 = 2 s_(1)=1,p_(1)=2s_{1}=1, p_{1}=2, s 2 = B s 2 = B s_(2)=Bs_{2}=B, and p 2 = B p 2 = B p_(2)=Bp_{2}=B. GreedyKnapsack will take only item 1 1 11, but taking only item 2 2 22 would be a better solution and the ratio of the profits in the two cases is 2 / B 2 / B 2//B2 / B which can be made arbitrarily small. As it turns out, we can easily modify this algorithm to provide a 2 2 22-approximation by simply taking the best of GreedyKnapsack's solution or the most profitable item. We will call this new algorithm ModifiedGreedy.
Theorem 3.1. ModifiedGreedy has an approximation ratio of 1 / 2 1 / 2 1//21 / 2 for the Knapsack problem.
Proof. Let k k kk be the index of the first item that is not accepted by GreedyKnapsack. Consider the following claim:
Claim 3.1.1. p 1 + p 2 + p k OPT p 1 + p 2 + p k OPT p_(1)+p_(2)+dotsp_(k) >= OPTp_{1}+p_{2}+\ldots p_{k} \geq \operatorname{OPT}. In fact, p 1 + p 2 + + α p k OPT p 1 + p 2 + + α p k OPT p_(1)+p_(2)+cdots+alphap_(k) >= OPTp_{1}+p_{2}+\cdots+\alpha p_{k} \geq \operatorname{OPT} where α = B ( s 1 + s 2 + + s k 1 ) s k α = B s 1 + s 2 + + s k 1 s k alpha=(B-(s_(1)+s_(2)+cdots+s_(k-1)))/(s_(k))\alpha=\frac{B-\left(s_{1}+s_{2}+\cdots+s_{k-1}\right)}{s_{k}} is the fraction of item k k kk that can still fit in the knapsack after packing the first k 1 k 1 k-1k-1 items.
The proof of Theorem 3.1 follows immediately from the claim. In particular, either p 1 + p 2 + + p k 1 p 1 + p 2 + + p k 1 p_(1)+p_(2)+cdots+p_(k-1)p_{1}+p_{2}+\cdots+p_{k-1} or p k p k p_(k)p_{k} must be at least OPT / 2 OPT / 2 OPT //2\operatorname{OPT}/2. We now only have to prove Claim 3.1.1. We give an LP LP LP\operatorname{LP} relaxation of the Knapsack problem as follows: Here, x i [ 0 , 1 ] x i [ 0 , 1 ] x_(i)in[0,1]x_{i} \in[0,1] denotes the fraction of item i i ii packed in the knapsack.
maximize i = 1 n p i x i subject to i = 1 n s i x i B x i 1 for all i in { 1 n } x i 0 for all i in { 1 n } maximize i = 1 n p i x i subject to i = 1 n s i x i B x i 1  for all  i  in  { 1 n } x i 0  for all  i  in  { 1 n } {:["maximize"sum_(i=1)^(n)p_(i)x_(i)],["subject to"sum_(i=1)^(n)s_(i)x_(i) <= B],[x_(i) <= 1" for all "i" in "{1dots n}],[x_(i) >= 0" for all "i" in "{1dots n}]:}\begin{aligned} \text {maximize} \sum_{i=1}^{n} p_{i} x_{i}&\\ \text{subject to}\sum_{i=1}^{n}s_{i}x_{i}&\leq B\\ x_{i}&\leq 1\text{ for all }i\text{ in }\{1\ldots n\}\\ x_{i}&\geq 0\text{ for all }i\text{ in }\{1\ldots n\}\\ \end{aligned}
Let OPT L P OPT L P OPT_(LP)\operatorname{OPT}_{L P} be the optimal value of the objective function in this linear programming instance. Any solution to Knapsack is a feasible solution to the LP LP LP\operatorname{LP} and both problems share the same objective function, so OPT L P OPT OPT L P OPT OPT_(LP) >= OPT\operatorname{OPT}_{L P} \geq \operatorname{OPT}. Now set x 1 = x 2 = = x k 1 = 1 , x k = α x 1 = x 2 = = x k 1 = 1 , x k = α x_(1)=x_(2)=cdots=x_(k-1)=1,x_(k)=alphax_{1}=x_{2}=\cdots=x_{k-1}=1, x_{k}=\alpha, and x i = 0 x i = 0 x_(i)=0x_{i}=0 for all i > k i > k i > ki>k. This is a feasible solution to the LP LP LP\operatorname{LP}. We leave it as an exercise to the reader to argue that is an optimum solution. Therefore, p 1 + p 2 + + α p k = OPT OPT p 1 + p 2 + + α p k = OPT OPT p_(1)+p_(2)+cdots+alphap_(k)=OPT^(') >= OPTp_{1}+p_{2}+\cdots+\alpha p_{k}= \operatorname{OPT}^{\prime} \geq \operatorname{OPT}. The first statement of the lemma follows from the second as α 1 α 1 alpha <= 1\alpha \leq 1.

3.1.2 A Polynomial Time Approximation Scheme

Using the results from the last section, we make a few simple observations. Some of these lead to a better approximation.
Observation 3.2. If for all i , p i ϵ OPT i , p i ϵ OPT i,p_(i) <= epsilon OPTi, p_{i} \leq \epsilon \operatorname{OPT}, GreedyKnapsack gives a ( 1 ϵ ) a ( 1 ϵ ) a(1-epsilon)a(1-\epsilon) approximation.
Proof. Follows easily from Claim 3.1.1.
Observation 3.3. There are at most 1 ϵ 1 ϵ |~(1)/(epsilon)~|\left\lceil\frac{1}{\epsilon}\right\rceil items with profit at least ϵ OPT ϵ OPT epsilon OPT\epsilon \operatorname{OPT} in any optimal solution.
The next claim is perhaps more interesting and captures the intuition that the bad case for greedy happens only when there are "big" items.
Claim 3.1.2. If for all i , s i ϵ B i , s i ϵ B i,s_(i) <= epsilon Bi, s_{i} \leq \epsilon B, GreedyKnapsack gives a ( 1 ϵ ) ( 1 ϵ ) (1-epsilon)(1-\epsilon) approximation.
Proof. We give a proof sketch via the LP LP LP\operatorname{LP} relaxation. Recall that k k kk is the first item that did not fit in the knapsack. We make the following observation. Recall that OPT L P OPT L P OPT_(LP)\operatorname{OPT}_{L P} is the optimum value of LP LP LP\operatorname{LP} relaxation. Suppose we reduce the knapsack capacity to B = s 1 + s 2 + s k 1 B = s 1 + s 2 + s k 1 B^(')=s_(1)+s_(2)+dotss_(k-1)B^{\prime}=s_{1}+s_{2}+\ldots s_{k-1} while keeping all the items the same. Let OPT L P OPT L P OPT_(LP)^(')\operatorname{OPT}_{L P}^{\prime} be the value for the new size. We claim that OPT L P B B OPT L P OPT L P B B OPT L P OPT_(LP)^(') >= (B^('))/(B)OPT_(LP)\operatorname{OPT}_{L P}^{\prime} \geq \frac{B^{\prime}}{B} \operatorname{OPT}_{L P}–this is because we can take any feasible solution to the original LP LP LP\operatorname{LP} and scale each variable by B / B B / B B^(')//BB^{\prime} / B to obtain a feasible solution with the new capacity. What is O P T L P O P T L P OPT_(LP)^(')\mathrm{OPT}_{L P}^{\prime}? We note that Greedy will fill B B B^(')B^{\prime} to capacity with the first k 1 k 1 k-1k-1 items and hence, OPT L P = p 1 + + p k 1 OPT L P = p 1 + + p k 1 OPT_(LP)^(')=p_(1)+dots+p_(k-1)\operatorname{OPT}_{L P}^{\prime}=p_{1}+\ldots+p_{k-1}. Combining, we obtain that
p 1 + + p k 1 B B OPT L P B B OPT . p 1 + + p k 1 B B OPT L P B B OPT . p_(1)+dots+p_(k-1) >= (B^('))/(B)OPT_(LP) >= (B^('))/(B)OPT.p_{1}+\ldots+p_{k-1} \geq \frac{B^{\prime}}{B} \operatorname{OPT}_{L P} \geq \frac{B^{\prime}}{B} \operatorname{OPT} .
We note that B + s k B B + s k B B^(')+s_(k) >= BB^{\prime}+s_{k} \geq B since item k k kk did not fit, and hence B B s k B ϵ B B B s k B ϵ B B^(') >= B-s_(k) >= B-epsilon B >=B^{\prime} \geq B-s_{k} \geq B-\epsilon B \geq ( 1 ϵ ) B ( 1 ϵ ) B (1-epsilon)B(1-\epsilon) B. Therefore B / B ( 1 ϵ ) B / B ( 1 ϵ ) B^(')//B >= (1-epsilon)B^{\prime} / B \geq(1-\epsilon) and this finishes the proof.
We may now describe the following algorithm. Let ϵ ( 0 , 1 ) ϵ ( 0 , 1 ) epsilon in(0,1)\epsilon \in(0,1) be a fixed constant and let h = 1 ϵ h = 1 ϵ h=|~(1)/(epsilon)~|h=\left\lceil\frac{1}{\epsilon}\right\rceil. We will try to guess the h h hh most profitable items in an optimal solution and pack the rest greedily.
Guess H+ Greedy ( N , B ) _ Guess H+ Greedy ( N , B ) _ "Guess H+ Greedy"(N,B)_\underline{\text{Guess H+ Greedy} (N, B)}
  1. For each S N S N S sube NS \subseteq N such that | S | h | S | h |S| <= h|S| \leq h and s ( S ) B s ( S ) B s(S) <= Bs(S) \leq B do
    • A.Pack S S SS in knapsack of size at most B B BB
    • B.Let i i ii be the least profitable item in S S SS. Remove all items j N S j N S j in N-Sj \in N-S where p j > p i p j > p i p_(j) > p_(i)p_{j}>p_{i}.
    • C.Run GreedyKnapsack on remaining items with remaining capacity B i S s i B i S s i B-sum_(i in S)s_(i)B-\sum_{i \in S} s_{i}
  2. Output best solution from above
Theorem 3.4. Guess H + H + H+H+ Greedy gives a ( 1 ϵ ) ( 1 ϵ ) (1-epsilon)(1-\epsilon) approximation and runs in O ( n 1 / ϵ + 1 ) O n 1 / ϵ + 1 O(n^(|~1//epsilon~|+1))O\left(n^{\lceil 1 / \epsilon\rceil+1}\right) time.
Proof. For the running time, observe that there are O ( n h ) O n h O(n^(h))O\left(n^{h}\right) subsets of N N NN. For each subset, we spend linear time greedily packing the remaining items. The time initially spent sorting the items can be ignored thanks to the rest of the running time.
For the approximation ratio, consider a run of the loop where S S SS actually is the h h hh most profitable items in an optimal solution and the algorithm's greedy stage packs the set of items A ( N S ) A ( N S ) A^(')sube(N-S)A^{\prime} \subseteq(N-S). Let OPT OPT OPT^(')\operatorname{OPT}^{\prime} be the optimal way to pack the smaller items in N S N S N-SN-S so that OPT = p ( S ) + OPT OPT = p ( S ) + OPT OPT=p(S)+OPT^(')\operatorname{OPT} =p(S)+\operatorname{OPT}^{\prime}. Let item k k kk be the first item rejected by the greedy packing of N S N S N-SN-S. We know p k ϵ OPT p k ϵ OPT p_(k) <= epsilon OPTp_{k} \leq \epsilon \operatorname{OPT} so by Claim 3.1.1 p ( A ) OPT ϵ OPT p A OPT ϵ OPT p(A^(')) >= OPT^(')-epsilon OPTp\left(A^{\prime}\right) \geq \operatorname{OPT}^{\prime}-\epsilon \operatorname{OPT}. This means the total profit found in that run of the loop is p ( S ) + p ( A ) ( 1 ϵ ) OPT p ( S ) + p A ( 1 ϵ ) OPT p(S)+p(A^(')) >= (1-epsilon)OPTp(S)+p\left(A^{\prime}\right) \geq(1-\epsilon) \operatorname{OPT}.
Note that for any fixed choice of ϵ > 0 ϵ > 0 epsilon > 0\epsilon>0, the preceding algorithm runs in polynomial time. This type of algorithm is known as a polynomial time approximation scheme or PTAS PTAS PTAS\operatorname{PTAS}. The term "scheme" refers to the fact that the algorithm varies with ϵ ϵ epsilon\epsilon. We say a maximization problem Π Π Pi\Pi has a PTAS PTAS PTAS\operatorname{PTAS} if for all ϵ > 0 ϵ > 0 epsilon > 0\epsilon>0, there exists a polynomial time algorithm that gives a ( 1 ϵ ) ( 1 ϵ ) (1-epsilon)(1-\epsilon) approximation ( ( 1 + ϵ ) ( ( 1 + ϵ ) ((1+epsilon)((1+\epsilon) for minimization problems). In general, one can often find a PTAS PTAS PTAS\operatorname{PTAS} for a problem by greedily filling in a solution after first searching for a good basis on which to work. As described below, Knapsack actually has something stronger known as a fully polynomial time approximation scheme or FPTAS FPTAS FPTAS\operatorname{FPTAS}. A maximization problem Π Π Pi\Pi has a FPTAS FPTAS FPTAS\operatorname{FPTAS} if for all ϵ > 0 ϵ > 0 epsilon > 0\epsilon>0, there exists an algorithm that gives a ( 1 ϵ ) ( 1 ϵ ) (1-epsilon)(1-\epsilon) approximation ( ( 1 + ϵ ) ( ( 1 + ϵ ) ((1+epsilon)((1+\epsilon) for minimization problems) and runs in time polynomial in both the input size and 1 / ϵ 1 / ϵ 1//epsilon1 / \epsilon.

3.1.3 Rounding and Scaling

Earlier we mentioned exact algorithms based on dynamic programming that run in O ( n B ) O ( n B ) O(nB)O(n B) and O ( n P ) O ( n P ) O(nP)O(n P) time but noted that B B BB and P P PP may be prohibitively large. If we could somehow decrease one of those to be polynomial in n n nn without losing too much information, we might be able to find an approximation based on one of these algorithms. Let p max = max i p i p max = max i p i p_(max)=max_(i)p_(i)p_{\max }=\max _{i} p_{i} and note the following.
Observation 3.5. p max OPT n p max p max OPT n p max p_(max) <= OPT <= np_("max")p_{\max } \leq \operatorname{OPT} \leq n p_{\text{max}}
Now, fix some ϵ ( 0 , 1 ) ϵ ( 0 , 1 ) epsilon in(0,1)\epsilon \in(0,1). We want to scale the profits and round them to be integers so we may use the O ( n P ) O ( n P ) O(nP)O(n P) algorithm efficiently while still keeping enough information in the numbers to allow for an accurate approximation. For each i i ii, let p i = n ϵ 1 p max p i p i = n ϵ 1 p max p i p_(i)^(')=|__(n)/( epsilon)(1)/(p_("max"))p_(i)__|p_{i}^{\prime}=\left\lfloor\frac{n}{\epsilon} \frac{1}{p_{\text{max} }} p_{i}\right\rfloor. Observe that p i n ϵ p i n ϵ p_(i)^(') <= (n)/( epsilon)p_{i}^{\prime} \leq \frac{n}{\epsilon} so now the sum of the profits P P P^(')P^{\prime} is at most n 2 ϵ n 2 ϵ (n^(2))/(epsilon)\frac{n^{2}}{\epsilon}. Also, note that we lost at most n n nn profit from the scaled optimal solution during the rounding, but the scaled down OPT OPT OPT\operatorname{OPT} is still at least n ϵ n ϵ (n)/( epsilon)\frac{n}{\epsilon}. We have only lost an ϵ ϵ epsilon\epsilon fraction of the solution. This process of rounding and scaling values for use in exact algorithms has use in a large number of other maximization problems. We now formally state the algorithm Round&Scale and prove its correctness and running time.
Round&Scale ( N , B ) _ Round&Scale ( N , B ) _ "Round&Scale"(N,B)_\underline{\text{Round&Scale}(N, B)}
  1. For each i i ii set p i = n ϵ 1 p max p i p i = n ϵ 1 p max p i p_(i)^(')=|__(n)/( epsilon)(1)/(p_("max"))p_(i)__|p_{i}^{\prime}=\left\lfloor\frac{n}{\epsilon} \frac{1}{p_{\text{max} }} p_{i}\right\rfloor
  2. Run exact algorithm with run time O ( n P ) O n P O(nP^('))O\left(n P^{\prime}\right) to obtain A A AA
  3. Output A A AA
Theorem 3.6. Round&Scale gives a ( 1 ϵ ) ( 1 ϵ ) (1-epsilon)(1-\epsilon) approximation and runs in O ( n 3 ϵ ) O n 3 ϵ O((n^(3))/(epsilon))O\left(\frac{n^{3}}{\epsilon}\right) time.
Proof. The rounding can be done in linear time and as P = O ( n 2 ϵ ) P = O n 2 ϵ P^(')=O((n^(2))/(epsilon))P^{\prime}=O\left(\frac{n^{2}}{\epsilon}\right), the dynamic programing portion of the algorithm runs in O ( n 3 ϵ ) O n 3 ϵ O((n^(3))/(epsilon))O\left(\frac{n^{3}}{\epsilon}\right) time. To show the approximation ratio, let α = n ϵ 1 p max α = n ϵ 1 p max alpha=(n)/( epsilon)(1)/(p_("max"))\alpha=\frac{n}{\epsilon} \frac{1}{p_{\text{max}}} and let A A AA be the solution returned by the algorithm and A A A^(**)A^{*} be the optimal solution. Observe that for all X N X N X sube NX \subseteq N, α p ( X ) | X | p ( X ) α p ( X ) α p ( X ) | X | p ( X ) α p ( X ) alpha p(X)-|X| <= p^(')(X) <= alpha p(X)\alpha p(X)-|X| \leq p^{\prime}(X) \leq \alpha p(X) as the rounding lowers each scaled profit by at most 1. The algorithm returns the best choice for A A AA given the scaled and rounded values, so we know p ( A ) p ( A ) p ( A ) p A p^(')(A) >= p^(')(A^(**))p^{\prime}(A) \geq p^{\prime}\left(A^{*}\right).
p ( A ) 1 α p ( A ) 1 α p ( A ) p ( A ) n α = OPT ϵ p max ( 1 ϵ ) OPT p ( A ) 1 α p ( A ) 1 α p A p A n α = OPT ϵ p max ( 1 ϵ ) OPT p(A) >= (1)/(alpha)p^(')(A) >= (1)/(alpha)p^(')(A^(**)) >= p(A^(**))-(n)/( alpha)=OPT-epsilonp_("max") >= (1-epsilon)OPTp(A) \geq \frac{1}{\alpha} p^{\prime}(A) \geq \frac{1}{\alpha} p^{\prime}\left(A^{*}\right) \geq p\left(A^{*}\right)-\frac{n}{\alpha}=\operatorname{OPT}-\epsilon p_{\text{max} } \geq(1-\epsilon) \operatorname{OPT}
It should be noted that this is not the best FPTAS FPTAS FPTAS\operatorname{FPTAS} known for Knapsack. In particular, [1] shows a FPTAS FPTAS FPTAS\operatorname{FPTAS} that runs in O ( n log ( 1 / ϵ ) + 1 / ϵ 4 ) O n log ( 1 / ϵ ) + 1 / ϵ 4 O(n log(1//epsilon)+1//epsilon^(4))O\left(n \log (1 / \epsilon)+1 / \epsilon^{4}\right) time. There have been several improvements and we refer the reader to Chan's paper for the latest [2].

3.2 Other Problems

There are many variants of Knapsack and it is a fundamental problem of interest in integer programming as well in several other areas. One can find a book length treatment in [3]. We close with an interesting variant.
Multiple Knapsack: The input now consists of m m mm knapsacks with capacities B 1 , B 2 , , B m B 1 , B 2 , , B m B_(1),B_(2),dots,B_(m)B_{1}, B_{2}, \ldots, B_{m} and n n nn items with sizes and profits as in Knapsack. We again wish to pack items to obtains as large a profit as possible, except now we have more than one knapsack with which to do so. An interesting special case is when all the knapsack capacities are the same quantity B B BB which is related to the well known Bin Packing problem.

  1. Eugene L Lawler. “Fast approximation algorithms for knapsack problems”. In: Mathematics of Operations Research 4.4 (1979), pp. 339–356. ↩︎
  2. Timothy M Chan. “Approximation schemes for 0-1 knapsack”. In: 1st Symposium on Simplicity in Algorithms (SOSA 2018). Schloss DagstuhlLeibniz-Zentrum fuer Informatik. 2018. ↩︎
  3. H. Kellerer, H.K.U.P.D. Pisinger, U. Pferschy, and D. Pisinger. Knapsack Problems. Springer Nature Book Archives Millennium. Springer, 2004. isbn:9783540402862. url: https://books.google.com/books?id=u5DB7gck08YC. ↩︎

Recommended for you

David McCaffary
Towards continual task learning in artificial neural networks
Towards continual task learning in artificial neural networks
Critical appraisal of prominent current approaches to alleviating catastrophic forgetting in neural networks, drawing on inspiration from neuroscience.
7 points
0 issues