The Fully Polynomial Approximation Algorithm for the 0-1 Knapsack Problem

6
DOI: 10.1007/s00224-002-1076-0 Theory Comput. Systems 35, 559–564 (2002) Theory of Computing Systems © 2002 Springer-Verlag New York Inc. The Fully Polynomial Approximation Algorithm for the 0-1 Knapsack Problem Yingjie Liu Department of Applied Mathematics and Statistics, State University of New York, Stony Brook, NY 11794, USA [email protected] Abstract. A modified fast approximation algorithm for the 0-1 knapsack prob- lem with improved complexity is presented, based on the schemes of Ibarra, Kim and Babat. By using a new partition of items, the algorithm solves the n-item 0-1 knapsack problem to any relative error tolerance ε> 0 in the scaled profit space P / K = O (11+δ ) with time O (n log(1/ε) + 12+2δ ) and space O (n + 12+δ ), where P and b are the maximal profit and the weight capacity of the knapsack problem, respectively, K is a problem-dependent scaling factor, δ = α/(1 + α) and α = O (log b). This algorithm reduces the exponent of the complexity bound in [5]. 1. Introduction The 0-1 knapsack problem (KP) is to solve for a given item set N ={1, 2,..., n}, P = max j N p j x j subject to j N w j x j b, (1) where x j ∈{0, 1} and p j ,w j are positive integers. KP is NP-hard, but it can be solved in pseudo-polynomial time O (nb) through dynamic programming. Note that the space for storing input data b is m = log b, therefore the complexity contains an exponential function of m. The first approximation scheme for KP was proposed by Sahni [9], [8], [4] and makes use of a greedy procedure. The first fully polynomial approximation was found by Ibarra and Kim [3], and the same idea was also found by Babat [1] independently. The basic strategy in the Ibarra–Kim algorithm is: (a) to separate items into a class of “large” items and a class of “small” items, according to the size of the profit p j ;

Transcript of The Fully Polynomial Approximation Algorithm for the 0-1 Knapsack Problem

Page 1: The Fully Polynomial Approximation Algorithm for the 0-1 Knapsack Problem

DOI: 10.1007/s00224-002-1076-0

Theory Comput. Systems 35, 559–564 (2002) Theory ofComputing

Systems© 2002 Springer-Verlag

New York Inc.

The Fully Polynomial Approximation Algorithm for the0-1 Knapsack Problem

Yingjie Liu

Department of Applied Mathematics and Statistics, State University of New York,Stony Brook, NY 11794, [email protected]

Abstract. A modified fast approximation algorithm for the 0-1 knapsack prob-lem with improved complexity is presented, based on the schemes of Ibarra, Kimand Babat. By using a new partition of items, the algorithm solves the n-item 0-1knapsack problem to any relative error tolerance ε > 0 in the scaled profit spaceP�/K = O(1/ε1+δ) with time O(n log(1/ε) + 1/ε2+2δ) and space O(n + 1/ε2+δ),where P� and b are the maximal profit and the weight capacity of the knapsackproblem, respectively, K is a problem-dependent scaling factor, δ = α/(1 + α) andα = O(log b). This algorithm reduces the exponent of the complexity bound in [5].

1. Introduction

The 0-1 knapsack problem (KP) is to solve for a given item set N = {1, 2, . . . , n},P� = max

∑j∈N

pj xj

subject to∑j∈N

wj xj ≤ b, (1)

where xj ∈ {0, 1} and pj , wj are positive integers. KP is NP-hard, but it can be solvedin pseudo-polynomial time O(nb) through dynamic programming. Note that the spacefor storing input data b is m = log b, therefore the complexity contains an exponentialfunction of m. The first approximation scheme for KP was proposed by Sahni [9], [8], [4]and makes use of a greedy procedure. The first fully polynomial approximation was foundby Ibarra and Kim [3], and the same idea was also found by Babat [1] independently.The basic strategy in the Ibarra–Kim algorithm is: (a) to separate items into a classof “large” items and a class of “small” items, according to the size of the profit pj ;

Page 2: The Fully Polynomial Approximation Algorithm for the 0-1 Knapsack Problem

560 Y. Liu

(b) to solve the KP problem for the “large” items only, with profits scaled by a factorK > 0, through dynamic programming, and store the dynamic programming list in atable; and (c) to fill the residual capacity for each entry in the table with the “small”items. The time and space complexity are O(n log n + 1/ε4 log(1/ε)) and O(n + 1/ε3),

respectively. The Ibarra–Kim scheme has been modified by Lawler [5] to obtain improvedtime complexity O(n log(1/ε) + 1/ε4) and space O(n + 1/ε3), and by Magazine andOguz [7] to obtain time complexity O((n2 log n)/ε) and space O(n/ε), which usessmaller space when ε is set to be small enough, e.g., O(1/n). In 1994 Gens and Levner[2] proposed an approximation algorithm for the subset-sum problem which has timecomplexity O(min(n/ε, n + 1/ε3)) and O(min(n/ε, n + 1/ε2)) space.

The main idea of this paper is to use a modified partitioning in the Ibarra–Kim schemeto reduce the size of the table described in (b), and then use the techniques in [5] to getscaled profit space P�/K = O(1/ε1+δ), time complexity O(n log(1/ε) + 1/ε2+2δ) andspace O(n + 1/ε2+δ) with 0 < δ < 1. In particular, δ = α/(1 + α) with α = O(log b).This is an improvement from [5] where the scaled profit space, time complexity andspace are P�/K = O(1/ε2), O(n log(1/ε) + 1/ε4) and O(n + 1/ε3), respectively.

We first separate the items into “heavy”, “large” and others, then solve the problemthrough dynamic programming for “heavy” and “large” items only, with profits scaledby a factor K (differently for “heavy” and “large” items). This yields a final list of (Q, A)

pairs, where Q is the total scaled profit and A is the total weight satisfying A ≤ b. Thedominated pairs are discarded so that for any two pairs (Q1, A1), (Q2, A2) in the list,either Q1 < Q2, A1 < A2 or Q1 > Q2, A1 > A2. For each pair (Q, A) in the list, fillthe remaining capacity b − A with other items using the density greedy method (i.e.,ordering the items by non-increasing profit-to-weight ratios), which yields the total profitϕ(b − A) for these items.

The following section provides two lemmas which are important for the proof ofthe main result in Section 3.

2. Preparation

An item j is called “heavy” if pj/ α√

wj > D; it is called “large” if α√

wj > W andpj/ α

√wj ≤ D, where D, W, α > 0 are to be determined later.

Lemma 1. If the knapsack problem is calculated using arbitrary combination of“heavy” and “large” items only and with the remaining capacity filled with other itemsusing the density greedy method, then the optimal approximation profit P is bounded by0 ≤ P� − P ≤ DW .

Proof. Suppose P� is achieved by an item set J ⊆ N .If J contains only “heavy” and “large” items, then P = P�; if not, suppose all

“heavy” and “large” items in J consist of a subset I ⊂ J . Note that∑

i∈I pi + R ≤ P ,where R is the total profit filling the capacity b − ∑

i∈I wi with other items throughthe density greedy method. Also notice that

∑j∈J−I pj − R ≤ DW since the profit

of every other item is no more than DW . Adding the above two inequalities we haveP� − P ≤ DW .

Page 3: The Fully Polynomial Approximation Algorithm for the 0-1 Knapsack Problem

The Fully Polynomial Approximation Algorithm for the 0-1 Knapsack Problem 561

We scale the profit space with a factor K > 0, and the new profit qj is defined as

qj =

�pj/K � if item j is “heavy”,�pj/K � + 1 if item j is “large”,pj otherwise.

We have the following lemma.

Lemma 2. If J is the set of “heavy” items and I is the set of “large” items, then

−K |J | ≤ K

(∑j∈I

qj +∑j∈J

qj

)−(∑

j∈I

pj +∑j∈J

pj

)≤ K |I |.

Proof.

1

K

∑j∈J

pj − |J | ≤∑j∈J

qj ≤ 1

K

∑j∈J

pj ,

1

K

∑j∈I

pj ≤∑j∈I

qj ≤ 1

K

∑j∈I

pj + |I |,

therefore(∑j∈I

pj +∑j∈J

pj

)− K |J | ≤ K

(∑j∈I

qj +∑j∈J

qj

)

≤(∑

j∈I

pj +∑j∈J

pj

)+ K |I |.

3. Algorithm

Separate the items into three sets called “heavy”, “large” and others, and solve theproblem using the “heavy” and “large” items only in the scaled profit space, then we geta final list of (Q, A) pairs. For each pair (Q, A) in the list, fill the remaining capacityb − A with other items.

Let P = max(Q,A) K Q + ϕ(b − A). Suppose there exists an optimal solution(Qo, Ao) in which the sum of the profits (unscaled) and the sum of the weights are PH

and AH, respectively, for “heavy” items; PL and AL for “large” items; and Ps and As arethe sum of the profits and the sum of the weights, respectively, for other items used to fillb−Ao. Let QH, QL be the sum of the scaled profits corresponding to PH, PL, respectively,then there exists a pair (Q, A) in the final list such that Q ≥ QH + QL, A ≤ AH + AL .Therefore

P ≥ K Q + ϕ(b − A) ≥ K (QH + QL) + ϕ(b − A). (2)

Page 4: The Fully Polynomial Approximation Algorithm for the 0-1 Knapsack Problem

562 Y. Liu

The number of “heavy” items contributing to the optimal cannot exceed P�/D α√

w0 ≤P�/D, where w0 is the smallest weight which is no less than 1. Similarly, the numberof “large” items cannot exceed b/W α . From Lemma 2 we have

P� = PH + PL + Ps ≤ K (QH + QL) + KP�

D+ Ps. (3)

Subtracting (2) from (3) gives

P� − P ≤ KP�

D+ Ps − ϕ(b − A). (4)

From Lemma 1, it is easy to know that Ps − ϕ(b − A) ≤ DW , therefore

P� − P ≤ KP�

D+ DW. (5)

We know P may be greater than P� only because of the scaling of profit space, and wehave an estimate of the number of “large” and “heavy” items for the pair (Q, A). In fact,from Lemma 2 and the computation of ϕ(b − A), we have

P − P� ≤ Kb

W α.

There exists a P0 (it is determined by the corresponding linear programming problemin O(n) time, for details, see [5]) such that P0 ≤ P� ≤ 2P0. Therefore in order to have|P − P�| ≤ εP�, we can set

K

2P0

D+ DW = εP0,

Kb

W α= εP0.

(6)

Canceling out W from (6), we have

K2P0

D+ D

(K b

εP0

)1/α

= εP0. (7)

Assume

P0

K≥ c

ε1+δ, (8)

where c is a constant, c > 0, 0 < δ < 1 are to be determined later. Since (8) impliesK ≤ P0ε

1+δ/c, we get from (7) K (2P0/D)+ Dεδ/αb1/αc−1/α ≥ εP0. Solving for K wehave

P0

K≤ 2

ε(D/P0) − (D/P0)2εδ/αb1/αc−1/α. (9)

Choose

D = ε1−δ/αc1/α P0

2b1/α, (10)

Page 5: The Fully Polynomial Approximation Algorithm for the 0-1 Knapsack Problem

The Fully Polynomial Approximation Algorithm for the 0-1 Knapsack Problem 563

Table 1. Comparative δ values for different b and c.

b

c 102 103 104 105

16 0.726 0.856 0.903 0.92732 0.451 0.713 0.806 0.85364 0.177 0.569 0.708 0.780

then the right-hand side of (9) has a local minimum, and we get

P0

K≤ 8b1/αc−1/α

ε2−δ/α. (11)

Comparing (11) with (8) we can set 1 + δ = 2 − δ/α and c = 8b1/αc−1/α , whichimplies

α = log b − log c

log c − log 8, δ = α

1 + αand

P0

K= c

ε1+δ. (12)

Finally, solving the second equation of (6), we get W = (εδb/c)1/α .

Theorem 1. Let c > 8 be a constant. For any ε > 0 and b > c, choose

α = log b − log c

log c − log 8, δ = α

1 + α, K = P0ε

1+δ

c,

D = ε1−δ/αc1/α P0

2b1/αand W =

(εδb

c

)1/α

,

then |P − P�| ≤ εP�, and P�/K ≤ 2P0/K = 2c/ε1+δ .

Note that α = O(log b), we cannot obtain a problem-independent lower bound.However, it is interesting to see how large the profit space would be through someexamples. In Table 1 we calculate the size of δ with different values of c and b.

4. Remarks

Notice that D/K = O(1/ε) according to Theorem 1, which is parallel to [5] withthe partition parameter T replaced by D, therefore we can apply the techniques in[5] almost identically here, treating the “large” and “heavy” items indiscriminately.Therefore the time for computing the (Q, A) list of “large” and “heavy” items will beO([P�/K ] · [P�/K ]) = O(1/ε2+2δ). The time for computing ϕ(b − A) for all (Q, A)

pairs in the final list will be O(n log (1/ε)+ [P�/K ] · log (1/ε)). Therefore the total timewill be O(n log(1/ε)+1/ε2+2δ). Similarly the space will be O(n + (D/K ) · [P�/K ]) =O(n + 1/ε2+δ).

Using the technique in [7] it is possible to trade more time for less space but we donot discuss the technical details here.

Page 6: The Fully Polynomial Approximation Algorithm for the 0-1 Knapsack Problem

564 Y. Liu

Acknowledgments

I thank Dr. Ramesh Krishnamurti for his wonderful course in linear programming at Simon FraserUniversity, 1992. I became interested in the KP problem during the class and did a project on it[6], which is what this paper is based on. Also I thank one referee for the valuable suggestions.

References

[1] L. G. Babat. Linear functions on the n-dimensional unit cube. Dokl. Akad. Nauk SSSR, 222:761–762,1975.

[2] G. Gens and E. Levner. A fast approximation algorithm for the subset-sum problem. INFOR, 32:143–148,1994.

[3] O. H. Ibarra and C. E. Kim. Fast approximation algorithms for the knapsack and sum of subset problems.J. Assoc. Comput. Mach., 22(4):463–468, 1975.

[4] R. Kohli and R. Krishnamurti. A total-value greedy heuristic for the integer knapsack problem. Oper.Res. Lett., 12:65–71, 1992.

[5] E. L. Lawler. Fast approximation algorithm for knapsack problem. In Proceedings of the 18th AnnualSymposium on Foundation of Computer Science, pages 206–213. IEEE Computer Society, Long Beach,CA, 1977.

[6] Y. Liu. A fast approximation algorithm for the 0-1 knapsack problem with adjustable complexity.Graduate Course Project in Simon Fraser University, 1992.

[7] M. J. Magazine and O. Oguz. A fully polynomial approximation algorithm for the 0-1 knapsack problem.European J. Oper. Res., 8:270–273, 1981.

[8] S. Martello and P. Toth. Algorithm for knapsack problems. In Surveys in Combinatorial Optimization,pages 213–258. North-Holland, Amsterdam, 1987.

[9] S. Sahni. Approximate algorithms for the 0/1 knapsack problem. J. Assoc. Comput. Mach., 22(1):115–124, 1975.

Received in final form March 4, 2002. Online publication July 24, 2002.