Worst-case analysis of greedy algorithms for the ... - ScienceDirect.com

Report 3 Downloads 13 Views
Operations Research Letters 14 (1993) 215-220 North-Holland

November 1993

Worst-case analysis of greedy algorithms for the unbounded knapsack, subset-sum and partition problems Tsung-Chyan Lai Department of Industrial and Business Administration, College of Management, National Taiwan University, Taipei, Taiwan Received October 1992 Revised July 1993

We present on O(n log n) greedy algorithm with a worst-case performance ratio > 4 for the unbounded knapsack problem, an O(n log n) greedy algorithm with a worst-case performance ratio of ~ for the subset-sum problem, and an O(n log n) greedy algorithm with a worst-case performance ratio of ~- for the partition problem. These greedy algorithms, in the sense of worst-case performance, are better than other known O(n log n) greedy algorithms. knapsack; subset-sum; partition; NP-hardness; greedy algorithms; worst-case performance ratio

1. I n t r o d u c t i o n

Given a knapsack with a capacity c and a set of n types of items with each having a profit pj, and a weight w~, the Unbounded Knapsack Problem (UKP) is to fill the knapsack, without exceeding c, such that the total profit is a maximum, where an unlimited number of items of each type is available. Alternatively, UKP can be defined by

maximize{Z=~&wi: ~wixj-Oandinteger, j=l,...,n }. j=l

j=l

We assume that 0 < wj < c and 0 < pj for all j. If there is only one item available for each type, then UKP reduces to the 0-1 Knapsack Problem (KP) (Martello and Toth [6]), where xy can only take either 0 or 1, j = 1. . . . . n. If pj = wj for all j, then KP reduces to the Subset-Sum Problem (SSP), i.e.,

m mize{ =

j=l

j=l

..... n} wj

Alternatively, given a set of n items with each having a weight and a knapsack with a capacity c, SSP is to select a subset of the n items whose total weight is closest to but < c. We assume that 0 < w~ _< c for all j. Given a set of n items with each having an associated size ws, the Partition Problem (PP) is to v'n 1w j" We partition the n items into two groups such that the total size of the larger group is closest to 7,-,j= l~7'n ~Wi,] for all j. assume that wj < c ( = b-,i= Correspondence to." Tsung-Chyan Lai, Associate Professor, College of Management, National Taiwan University, 1 Roosevelt Road Sect. 4, Taipei 10764, Taiwan. 0167-6377/93/$06.00 © 1993 - Elsevier Science Publishers B.V. All rights reserved

215

Volume 14, N u m b e r 4

OPERATIONS RESEARCH LETTERS

November 1993

We note that UKP, SSP and PP are all NP-hard (Garey and Johnson [3]). The NP-hardness of UKP was established by Lueker [5] by transformation from SSP, while the NP-hardness of SSP can be easily established by transformation from PP, which is a basic NP-hard problem (Karp [4]). Given a problem instance I, let Z H ( I ) represent the objective value obtained by using heuristic H and Z * ( I ) represent the optimal solution. Letting ~Tn denote the worst-case performance ratio associated with H, then ~Tn = inf{r/H(I)} = inf

Z Z H* (( II )) }

for all problem instances I.

In what follows, when no confusion arises, we will use Z n and Z * instead of Z H ( I ) and Z * (I). The standard O(n log n) greedy algorithm for UKP is to sequentially fill the knapsack by selecting the item type with the largest ratio of profit over weight among the remaining item types. For each chosen item type, we fill as many items as possible, without exceeding c. It is easy to show (Fisher [1]) that the 1 greedy algorithm, denoted by G, has a worst-case performance ratio of 3- Suppose that the item types are reindexed such that r 1 > r 2 > • • • > rn, where rj = ( p J w i ) . Then, zG

[c 1

- - >

-~l

1 >

Consider the example: n = 2, Pl = K + 2, w ~ - - K + 1, p 2 = w 2 - - K and c = 2K, where z G / z * = ( K + 1 2 ) / 2 K will go to 5 as K goes to infinity. In this paper we present an O(n log n) greedy algorithm with a 4 worst-case performance ratio > 7. This greedy rule gives the highest priority to the item type with the largest value of ajpj, where % = [c/Nl. For SSP, we can insert each new item into the knapsack in any order or in nonincreasing order of weight. In either case, we all have a worst-case performance ratio of 51 (Martello and Toth [6]). In this paper we use two individual greedy algorithms, which involve only sorting, to separately generate two solutions and then choose the better one as the solution. This composite greedy algorithm has a worst-case performance ratio of 3" z For PP, Fischetti and Martello [2] proved that the differencing method by Karmarkar and Karp has a worst-case performance ratio of g. 7 In this paper we present a composite greedy algorithm with a worst-case performance ratio of 7" 8 The paper is organized as follows. After the presentation of our greedy algorithms in Section 2, we establish their worst-case performance ratios in Section 3.

2. Greedy algorithms

For UKP, we present one greedy algorithm, denoted by Heuristic A, which considers both the ratio of the profit over the weight and the value of the profit for each item type in selecting the item types in the knapsack. Let a i = [ c / w i ] and V~= a~p~. Heuristic A selects the item type sequentially according to value Vj with a high value of V~ having a high priority. Each time for the chosen item type k, heuristic A fills a k items of type k. Assume that the item types have been sorted such that V1 >_ V2 _> • • • > Vn. Heuristic A can be described as follows: Heuristic A Step 1. Set j = 1 and C = c . Set Xl = X z . . . . . x n =0. Step 2. If wj _< C, then set xj = and C = C - %w~. Step 3. Stop if j = n or C = 0; otherwise, set j = j + 1 and go to Step 2.

[C/w~l

216

Volume 14, Number 4

OPERATIONS RESEARCH LETTERS

November 1993

For SSP, since each item is either inserted or not inserted into the knapsack, we define a dummy knapsack, which we call the complementary knapsack, to collect all those items not inserted into the knapsack. We define the capacity of the complementary knapsack to be ET=lWj- c. We note that minimizing the excess of overfilling the complementary knapsack is equivalent to minimizing the slack capacity of the knapsack and thus equivalent to optimally filling, without exceeding c, the knapsack. Our heuristic, denoted by B, consists of a pair of individual greedy algorithms, denoted by B1 and B2, which involve sorting. In other words, B uses B1 and B2 to separately generate two solutions and chooses the better one as the solution. Assume that the items have been sorted in nonincreasing order of weight, i.e., w~ > • • • > wn. B1 is the standard greedy algorithm. For B2, we insert the items one by one into the complementary knapsack until we overfill (exceed) the complementary knapsack, and then we insert the remaining items into the knapsack. B1 and B2 are described as follows: Heuristic B1

Step 1. Set j = 1 and C = c . Set x I = x 2 . . . . . xn=0. Step 2. If wj < C, then set xj = 1 and C = C - wj. Step 3. Step if j = n or C = 0; otherwise, set j = j + 1 and go to Step 2. Heuristic B2

Step 1. Set j = 1 and C = E T = l W j - C . Set x 1 = x 2 . . . . . X n = 1. Step 2. If wj < C, then set xi = 0 and C = C - wj; otherwise, set x j = 0 and stop. Step 3. Set j" = j" + 1 and go to Step 2. For PP, our heuristic, denoted by D, consists of a pair of individual greedy algorithms, denoted by D1 and D2, where D uses D1 and D2 to generate two solutions and chooses the better one as the solution. Assume that the items have been sorted in nonincreasing order of weight, i.e., w~ > " • > w,. D1 and D2 are described as follows: Heuristic D1

Step 1. Set j = 1 and C = c = ~,_,j=l ~V'n Wj. Set X 1 = X 2 . . . . . X n = 1. Step 2. If wj < C, then set xj = 0 and C = C - wj. Step 3. Stop if j = n or C = 0; otherwise, set j = j + 1 and go to Step 2. Heuristic D2 1w'n w j, s = w~ and j * = 1. Step 1. Set j = 1, C = c - - ~,_,j=~ Step 2. If w j < C , then set x j = l and C = C - w j ; otherwise: set x j = 0 and if wi - C < s s=wj-Candj*=j. Step 3. If j = n or C = 0, go to step 4; otherwise, set j = j + 1 and go to Step 2. Step 4. Set xj. = 1 and set xj.+~ = x j . + 2 . . . . . x n = 0. Stop.

then set

We note that Heuristic D1 selects the items not collected in the knapsack, i.e., Heuristic D1 selects the items collected in the complementary knapsack, while Heuristic D2 selects the items collected in the knapsack plus one item that makes the overfilling a minimum. We note that Z °2 = c + s, where s is the value returned by Heuristic D2 described above. 217

Volume 14, Number 4

OPERATIONS RESEARCH LETTERS

November 1993

3. Worst-case performance of greedy algorithms 4 T h e following t h e o r e m shows that heuristic A has a worst-case p e r f o r m a n c e ratio > 7.

4

Theorem 1. ~TA > 7" Proof. Let {i~, i 2 . . . . . ih} be the items selected in the optimal solution. W i t h o u t loss of generality, we assume that the items are reindexed such that rq >_ ri2 > • • • > rgh. If h = 1, then Heuristic A generates • 2 an optimal solution since Z A = V 1 > Vii = Z * H e n c e we assume that h > 2. If ai~ > 2, the r/A > ~ since Z "4 >_ o~itPi I and Z * < (ai~ + 1)pfi and thus zA --> Z* -

OLilPil

2 > (aft + 1)Pil -- 3"

Consider the case as~ = 1. Since (ai, + l)wfi = 2wit > c, (ai2 + l)wi2 > c and Wil + wi2 < c (since h > 2), we have

and thus

1

1

2

O~i2+

ai2

__2. Let Oli2 +

Wij z u I =Pil +Pi2 - - 2 , Z U = pi t + Pi 2 C -wi-----~-,

Z 1

1

= p i l , and

Z 2

=

OlizPi 2.

Clearly, Z U > Z * and Z A > max{Z ~, Z2}. Since 2wi~ > c and (ai2 + 1)w,2 > c, we have Z U~ > Z U and thus Z U1 > Z * . If Z 1 _ -7

olizPi z

> --

Oli2 +

1

(since

ai2 ~_~2).

0li2Pi2 + T P i 2

Similarly, if Z 1 > Z 2, i.e., Pij ~ O~izPi2, then

max{ ZI, Z2} 7~A > --

zU

1

Pil > --

+ 1 20~i z Pfi

0[i2

Pil +

2ai2 --

_

_

301i2 + 1

4 >

--•

[]

7

W e note that the above worst-case b o u n d is not tight. It is expected that a lengthy p r o o f would be required to obtain an exact bound• T h e following l e m m a is useful in deriving ~TB.

L e m m a 1. In deriving ~?n, where Heuristic H selects the items in nonincreasing order o f wj: i f ~Tn = r, we only need to consider the p r o b l e m instances with the items h a v i n g a weight > (1 - r ) c f o r a m a x i m i z i n g p r o b l e m a n d >_ ( r - 1)c f o r a m i n i m i z i n g p r o b l e m . Proof. W e consider the maximizing problem. T h e p r o o f for the minimizing p r o b l e m is similar• Given a p r o b l e m instance I with n items, we assume, without loss of generality, that the items are reindexed such that w~ > w 2 > • • • > wn. Let S H and S* denote, respectively, the items selected by Heuristic H and in the optimal solution. Suppose that wn < (1 - r)c. Consider four cases: 218

Volume 14, Number 4

O P E R A T I O N S R E S E A R C H LETTERS

November 1993

(i) n ~ S I4 a n d n ~ S * : Let I ' be the i n s t a n c e with the first ( n - 1) items a n d with a capacity c - wn. T h e n , we have

Z14( I ) - w, ~714(1')

Z*(I)

-w,

zn( I) < - Z*(I)

01_1(I).

(ii) n ~ S n a n d n ~ S * : Let I ' be the i n s t a n c e with the first ( n - 1) items a n d with a capacity c. T h e n , we have

Zn(l) ~7.( I ' )

-w,

Z*(I)

zn(I) < - Z*(I)

On(I).

(iii) n ~ S n a n d n ~ S * : Let I ' be the i n s t a n c e with the first ( n T h e n , we have % 4 ( 1 ' ) = W/n(I). (iv) n ~ S n a n d n ~ S * : Since n ~ S / - / a n d thus r l n ( I ) ___1 - ( w J c ) > i g n o r e d in deriving r/n. In case that t h e r e are m o r e t h a n o n e item having a weight less t h a n the smallest item a n d r e p e a t the above process u n t i l all the r e m a i n i n g []

1) items a n d with a capacity c.

r, the p r o b l e m i n s t a n c e I can be (1 - r)c, we each time consider items have a weight > (1 - r)c.

T h e following t h e o r e m establishes the worst-case p e r f o r m a n c e ratio of Heuristic B. Theorem

2

2. r/B = 3.

Proof. Let {i~, i 2. . . . .

ih}

be the items selected in the o p t i m a l solution. By l e m m a 1, we can assume that t h e n Z B I >. z * C o n s. i d e r the case h > 2 . . Since h > 2 ,. we have ET~-lZwi> . n lwi - c a n d thus Z B2 > wn_ 1 + wn > 3c 2 > ~-Z*. Ei= T o show that the b o u n d is tight, c o n s i d e r the example: n = 5, Wl = 2 K + 2, w 2 = 2 K - 1, w 3 = w4 = w 5 = K + 1 a n d c = 3K. H e u r i s t i c B selects item 1 (or items 4 a n d 5 ) w i t h Z 8 = 2 K + 2 while the o p t i m a l 2 solution selects items 2 a n d 3 with Z * = 3K. H e n c e , Z B / Z * = ( 2 K + 2 ) / 3 K will go to 3 as K goes to infinity. []

w i >- - ±3c for all i. If h = l ,

It is i n t e r e s t i n g to n o t e that the d i f f e r e n c i n g m e t h o d by K a r m a r k a r a n d K a r p g e n e r a t e s a n o p t i m a l solution if n < 4 (Fischetti a n d M a r t e l l o [2]), while H e u r i s t i c D g e n e r a t e s a n o p t i m a l solution if n < 5 as the following l e m m a shows. L e m m a 2. I f n < 5, then Z m = Z * Proof. Let S 1 d e n o t e the set of the items n o t selected by Heuristic D1. Cases n = 1, n = 2 a n d n = 3 are trivial. If n = 4, two cases c a n occur. (i) S 1 = {1}: Z D = min{w I + w4, w2 + w3 + w4} = Z * . (ii) S 1 = {1, 4}: Z ° = w 2 + w 3 = Z * If n = 5, six cases c a n occur. (i) S 1 = {1}: Z D = min{w 1 + w 5, w2 + w3 + w4 + w5} = Z * . (ii) S l = {1, 2}: Z ° = min{w 1 + w 2 + w 5, w 3 + w 4 + w 5} = w 3 + w 4 + w 5 = Z * . (iii) S 1 = {1, 3}: Z ° = w e + w4 + w5 = Z * . (iv) S 1 = {1, 4}: Z ° = min{w I + w4 + ws, w2 + w3 + ws} = Z * . (v) S 1 = {1, 5}: Z D = min{w I + w4, w 2 + w 3 + w4} = Z * . (vi) S l = { 1 , 4 , 5 } : Z o = w 2 + w 3 - - - Z * . [] T h e following t h e o r e m establishes the worst-case p e r f o r m a n c e ratio of Heuristic D. 219

Volume

14, Number

4

OPERATIONS

RESEARCH

LETTERS

November

1993

Theorem 3. rlD = $. Proof. Given a problem instance I, let S, = Ii,, i,, . . . , ih} denote the set of the items not selected by Dl and Wi = CiES, w.,, where i, si, I ... si,. If II E S,, then we can construct another instance I’ such

that qD(Z’) 2 ~~(1). Note that item II will not be selected by D2 if II E S,. Let I’ represent the instance with the first (n - 1) items only. Note that we have Zoi(Z’) = Zo$Z), i = 1, 2, and thus ZD(Z’) = ZD(Z). Also, we have Z*(Z’) < Z*(Z) and thus qo(Z’) 2 ~~(1). Hence, without loss of generality, we can assume that n E S,. In this case we have IV, + w, > c. Let y = c - IV,. Clearly, if h = 1, then ZD = Z*. Hence we can assume that h 2 2. Since Zo’ = 2c - W, = c + y, ZD2 ~W,+w,,=c-y+w,,andZ*rc, we have ZD F’

min{c+y,

c-y+ww,} =l+lmin(y,wn-y)gI+z. C

C

We note that the last inequality holds since the supremum of the minimand occurs when y = iwn. Consider two cases. (i) 2c 2 7w,: In this case, we have ZD/Z* I 1 + (wJ2c) I $. (ii) 2c < 7w,: In this case, we have Cy=,wi = 2c < 7w,, and thus n I 6. If n I 5, then, by lemma 2, we have ZD = Z*. Consider the case n = 6. If h 2 3, then we have ZD1 I 2c - 3w, < $c and thus ZD/Z* < :. Consider h = 2. Let wi, =xwg (x 2 1). Since ZD’ I 2c - 2xwg, we have ZD’ I : if x 2 3. Hence we assume that x < t. Since h = 2, there may be three or four items in the optimal solution. Since ZD’ = Z * if there are four items in the optimal solution, we assume that there are three items in the optimal solution. In this case we have wi, + 2w, I Z*. This is true since, if item i, is in the optimal solution, each of the other two items is at least as large as wg and since wi, + 2w, I 2c - Z* I Z* if item i, is not in the optimal solution. Also, we have ZD2 I wi, + wi, + wg and thus obtain ZD - Z* I wiz - wg = (x - l)w,. Thus we have ZD-Z*Imin{y,w,-y,(x-l)w,}I(x-l)w,