www.elsevier.com/locate/dsw
Linear time-approximation algorithms for bin packing
(
Guochuan Zhang
a;1, Xiaoqiang Cai
b, C.K. Wong
c;∗;2aDepartment of Mathematics, Zhejiang University, Hangzhou 310027, People’s Republic of China
bDepartment of Systems Engineering & Engineering Management, The Chinese University of Hong Kong, Shatin, N.T., Hong Kong cDepartment of Computer Science & Engineering, The Chinese University of Hong Kong, Shatin, N.T., Hong Kong
Received 1 April 1999; received in revised form 1 October 1999
Abstract
Simchi-Levi (Naval Res. Logist. 41 (1994) 579 –585) proved that the famous bin packing algorithms FF and BF have an absolute worst-case ratio of no more than 7
4, and FFD and BFD have an absolute worst-case ratio of 3
2, respectively. These algorithms run in time O(nlogn). In this paper, we provide a linear time constant space (number of bins kept during the execution of the algorithm is constant) o-line approximation algorithms with absolute worst-case ratio 3
2. This result is best possible unless P = NP. Furthermore, we present a linear time constant space on-line algorithm and prove that the absolute worst-case ratio is 7
4. c2000 Elsevier Science B.V. All rights reserved. Keywords:Bin packing; Absolute worst-case ratio; Linear time algorithm
1. Introduction
In the classical one-dimensional bin packing prob-lem, we are given a list of itemsL= (a1; a2; : : : ; an),
(Research partially supported by a Hong Kong Government
RGC Earmarked Grant, Ref. No. CUHK356=96E.
∗Corresponding author.
E-mail addresses: [email protected] (G. Zhang), xqcai@ se.cuhk.edu.hk (X. Cai), [email protected] (C.K. Wong) 1Research partially supported by National Natural Science Foundation of China (Grant No. 19801032) and National 973 Fundamental Research Project of China on “Applied Theory and High-Performance Software for IT”. This work was done while visiting Department of Computer Science & Engineering and De-partment of Systems Engineering & Engineering Management, The Chinese University of Hong Kong.
2On leave from IBM T.J. Watson Research Center, Yorktown Heights, NY 10598, USA.
each with a sizes(ai)∈(0;1], and are asked to pack
them into a minimum number of unit-capacity bins. This problem is of fundamental theoretical signi-cance, serving as an early example for many of the classical approaches in analyzing the performance ratios, identifying lower bounds on the best-possible on-line performance, and analyzing the average-case behavior of algorithms. Bin packing also has many potential real-world applications, e.g., in memory allocation in paged computer systems, in assigning newspaper articles to newspaper pages, in loading trucks, in packet routing in communication networks, in assigning commercials to station breaks on tele-visions and in cutting-stock problems, etc. Since the bin packing problem is well known to be strongly NP-hard [3], much work has been done in the study of approximation algorithms. A survey of these results is given in [1].
A bin packing algorithm is calledon-lineif it packs all items ai solely on the basis of the sizes of the
items aj, 16j6i, and without any information on
subsequent items. The decisions of the algorithm are irrevocable. Once an item is packed, it is not allowed to be moved again. A bin packing algorithm is called o-lineif all items are known before it starts to con-struct a packing. There are also algorithms which work in situations dierent from both the on-line and o-line cases, e.g., partial information on future items are known in advance, some items are allowed to be repacked, etc. We call such algorithmssemi on-line.
In the case of bin packing, the standard metric for
worst-case performance is theasymptotic worst-case
ratio. For a given listLof items and an approximation AlgorithmA, letA(L) denote the number of bins used by AlgorithmAand let OPT(L) denote the number of
bins in an optimal packing. Theabsolute worst-case
ratioRA for AlgorithmAis given by
RA= sup
L
{A(L)=OPT(L)}:
Theasymptoticworst-case ratioR∞
A of AlgorithmA
is dened to be
R∞
A = lim
k→∞supL {A(L)=OPT(L)|OPT(L)¿k}:
Note that for any algorithm its asymptotic worst-case ratio is never larger than its absolute worst-case ratio. Most of worst-case results concentrate on asymptotic worst-case ratios, but the question of ab-solute worst-case ratios is also of interest, especially when we are considering relatively short lists of items. In addition to the worst-case performance ratio, we also consider the time and the space complexity of bin packing algorithms. The time complexity of AlgorithmArefers to the total time required byAto pack listLwhose size isn. AlgorithmAis called a lin-ear time algorithm if the time complexity is O(n). On the other hand, the space complexity means the max-imum number of storage locations needed by Algo-rithmAduring the processing of listLwhose size isn. Using the uniform cost criterion for space, we need one storage location for each open bin. As soon as a bin is declared closed, it is among the output of the algorithm used, and we do not count its storage loca-tion in dening the space complexity of the algorithm. A constant space algorithm uses constant storage locations at any time.
In this paper, we present a linear time constant space o-line algorithm with absolute worst-case ratio 32. This result is best possible unless P = NP. We point out that this algorithm is also semi on-line. Further-more, we give a linear time constant space on-line al-gorithm with absolute worst-case ratio 74. To the best of our knowledge, this result is the best so far. The rest of this paper is organized as follows. Section 2 in-troduces previous related results and some denitions. The linear time o-line algorithm and the linear time on-line algorithm are presented in Sections 3 and 4, respectively.
2. Previous results and basic denitions
An item is called alargeitem if its size is greater than 12; otherwise it is called a smallitem. A bin is calledopenif it has accepted an item and is not closed yet. Once an open bin is declared to be closed, it will never accept items anymore.
Let us rst consider on-line algorithms. Perhaps the simplest algorithm for the bin packing problem is Next-Fit (NF), apparently rst described under this name by Johnson [4]. In packing itemai, NF tests if
the current open bin has enough room for ai. If so,
it placesai in the bin and leaves the bin open.
Oth-erwise, it closes the bin and places ai in a new bin
which now becomes the open bin. This algorithm can be implemented to run in linear time and use con-stant space. It is not dicult to show that for any list L, NF(L)62OPT(L)−1. Furthermore, there exists a list L such that NF(L) = 2OPT(L)−1 holds. Thus
we conclude that RNF = 2. Moreover, bound 2 still
holds even for lists without large items. The famous on-line algorithms First-Fit (FF) and Best-Fit (BF) never close any open bin before all items are packed.
FF places itemai in the rst (lowest indexed) open
bin into which it ts. If such a bin does not exist, FF starts a new bin withaias its rst item. Note that FF is
not a linear time algorithm. Using an appropriate data structure [4], FF runs in time O(nlogn). Clearly, FF is an O(n) space algorithm since all non-empty bins remain open until the end of the packing. BF is sim-ilar to FF except that it places itemaiinto the fullest
bin into which ai ts (ties broken in favor of lower
et al. [5] provedR∞
FF=R
∞
BF= 1:7 and Simchi-Levi [10]
showed that FF and BF have an absolute worst-case ratio not greater than 74. Lee and Lee [6] presented a
linear time on-line algorithm called HAMRMONICM
and proved that it achieved asymptotic worst-case ra-tio of less than 1.692, which is better than that of FF.
A revised version of HAMRMONICM, an O(n) space
and linear time algorithm, was given and was shown to have an asymptotic worst-case ratio of less than 1.636. Ramanan et al. [9] provided a linear time on-line al-gorithm called Modied Harmonic (MH) and showed R∞
MH¡1:615. All of these harmonic algorithms are
based on the harmonic partition of the interval (0,1]. They group items into dierent types depending on their sizes and creates dierent classes of bins. In gen-eral, dierent type of items will be packed into dif-ferent class of bins. If the list is short and covers all types of items, it will result in a very large abso-lute worst-case ratio. In this paper, we give an on-line algorithm with absolute worst-case ratio74 which runs in linear time and uses only constant space. To the best of our knowledge, in terms of absolute worst-case ratio, our algorithm is the best among linear time con-stant space on-line algorithms so far.
As to o-line algorithms, it is generally recognized that rst-t decreasing (FFD) and best-t decreasing (BFD) are two of the best-known algorithms in bin packing. In these algorithms, the items are rst sorted in the order of non-increasing size and then FF and BF algorithms are applied, respectively. Johnson [4] proved the asymptotic worst-case ratios of FFD and
BFD to be 119. Simchi-Levi [10] showed FFD and
BFD have an absolute worst-case ratio 32 which are
the best possible unless P = NP. However neither FFD nor BFD is a linear time algorithm. A number of linear time bin-packing algorithms were developed. Fernandez and Leuker [2] showed that for every pos-itivethere is an AlgorithmAsuch thatA(L)6(1 + )OPT(L) +C, andAruns in time O(n) +D, where
C andD are constants that only depend on . The
authors did not calculate D precisely but remarked
that it is “certainly huge”. ClearlyDgrows
exponen-tially in 1=. Martel [8] presented a linear time bin packing algorithm OBP and showed that it uses at the most 4OPT(L)=3+2 bins for any listL. In this paper we focus on the absolute worst-case ratio. A simple linear time algorithm is derived. It is shown that the absolute worst-case ratio is 32, i.e., it uses at most 3OPT(L)=2
bins for any listL. Simchi-Levi [10] and Lenstra and Shmoys [7] proved that there does not exist any
poly-nomial time AlgorithmAfor the bin packing problem
and a constant ¡32 such thatRA6, unless P = NP.
Thus in terms of the absolute worst-case ratio, our algorithm is the best possible for the bin packing prob-lem, unless P = NP.
3. The o-line case
We give an algorithm as follows:
Algorithm A1.
1. Put large items each into a bin. Index the unlled bins in arbitrary order. Set all these bins asactive bins. Repeatedly arrange small items as follows. 2. If there is an active bin open, put the current item
aiinto the open active bin with the lowest index if
the bin has enough room forai, otherwise close this
active bin and consider theextrabin as follows. (a) If there is anextrabin open and it has enough
room forai, placeaiinto it.
(b) Otherwise close theextrabin. Open a new bin
foraiand set this bin as theextrabin.
3. If there is noactivebin open, create a new bin for aiand set this new bin as theactivebin.
In fact, in Step 1 of AlgorithmA1, we only need to
arrange one large item at a time. As soon as theactive bin containing a large item is closed, we open a new bin and put a remaining large item into it rst and set it as theactivebin. At any time we only keep at the most oneactivebin and at the most oneextrabin open. Thus, the algorithm only uses constant space.
The following example shows how AlgorithmA1
works. We have nine itemsa1; : : : ; a9 wheres(a1) =
0:8; s(a2) = 0:6; s(a3) = 0:3; s(a4) = 0:4; s(a5) =
0:2; s(a6) = 0:3; s(a7) = 0:4; s(a8) = 0:5; s(a9) = 0:2:
Then the packing is given in Fig. 1.
Theorem 1. AlgorithmA1 is a linear time algorithm
with absolute worst-case ratio3
2.MoreoverA1 is the
best possible algorithm unlessP = NP.
Proof. A1 is clearly a linear time algorithm sinceA1
Fig. 1.
AlgorithmA1consists of two bin setsX andY, where
X contains theactivebins andY is the set of theextra bins. LetNaandNebe the numbers of bins ofX and
Y, respectively. LetNcbe the number of closedactive
bins. Note that anactivebin is closed if and only if an item cannot t in the bin. And this item is packed into anextrabin. Thus the total number of items packed intoextrabins is equal toNc. Since all items inextra
bins are small items, except the last one allextrabins contain at least two items. It implies thatNe6⌈Nc=2⌉.
Case1:Nc6Na−1. In this case,
A1(L) =Na+Ne6Na+⌈Nc=2⌉6Na+⌈(Na−1)=2⌉
63Na=2:
IfNc=Na−1,
OPT(L)¿
n X
i=1
s(ai) = X
ai∈X
s(ai) + X
ai∈Y
s(ai)
¿ Nc=Na−1;
i.e., OPT(L)¿Na. If Nc6Na−2, there are at least
two active bins which are not closed. It happens only when each active bin contains a large item. Hence OPT(L)¿Na. Therefore, we getA1(L)=OPT(L)632.
Case2:Nc=Na. In this case,
A1(L) =Na+Ne6Na+⌈Nc=2⌉=Na+⌈Na=2⌉
63Na=2 + 12:
On the other hand,
OPT(L)¿
n X
i=1
s(ai) = X
ai∈X
s(ai) + X
ai∈Y
s(ai)¿ Na;
i.e., OPT(L)¿Na+ 1.A1(L)=OPT(L)632 also holds.
We have provenRA1632.
Simchi-Levi [10] and Lenstra and Shmoys [7] proved that there does not exist any polynomial time
AlgorithmAfor the bin packing problem and a
con-stant ¡32 such thatRA6, unless P = NP. Thus in
terms of the absolute worst-case ratio, the linear time AlgorithmA1is the best possible for the bin packing
problem, unless P = NP.
Observe that AlgorithmA1 is asemi on-line
algo-rithm. It only requires the information on large items in advance. If there is no large item,A1 serves as an
on-line algorithm.
In the next section, we will study the on-line version of the bin packing problem.
4. The on-line case
In the following algorithm, a bin is called an L-bin if the rst item it accepts is a large item and has not been set as anactivebin. Once such a bin becomes an activebin, it will not be called an L-bin any longer.
Algorithm A2.
1. Open a bin for the rst item. Set it as theactivebin. 2. If there is an active bin Bj open, put the current
itemaiintoBjifBjhas enough room for this item.
Suppose that Bj does not have enough space for
the item.
(a) Ifai is a large item, then create a new bin for
ai. If there are two L-bins then close them.
(b) Ifai is a small item, then close the active bin.
Otherwise open a new bin for ai and set the
new bin as theextrabin.
3. If there is noactivebin open, create a new bin for itemai and set it as theactivebin.
Clearly, A2 is a constant space algorithm since it
keeps at most oneactive bin, at most oneextra bin
and at most two L-bins open at any time.
We applyA2to the same item sizes as in the
exam-ple for AlgorithmA1except with the following
incom-ing ordera3; a4; a5; a2; a1; a6; a7; a8; a9. The packing is
shown in Fig. 2.
For any setLof items, consider the packing gener-ated byA2. LetX andY be the sets ofactivebins and
extrabins, respectively. LetZ be the set of remaining bins. From AlgorithmA2, it is easy to see thatZ is the
if allactivebins are closed;
NL+ 3Na=2
otherwise:
Proof. Let Nc be the number of closed active bins.
With the same analysis as that in the proof of Theorem 1, we can proveNe6⌈Nc=2⌉. Thus,
if allactivebins are closed;
NL+ 3Na=2
otherwise:
Lemma 3. OPT(L)¿NL:
Proof. In any optimal packing, each bin contains at
most one large item. Thus OPT(L) is not less than
the total number of all large items. Hence the lemma holds.
if allactivebins are closed;
Na+NL=2
otherwise:
Proof. We consider two cases below.
Case1: Allactivebins are closed. In this case, from AlgorithmA2the number of L-bins are even. The total
size of items ofZ bins is greater than NL=2. On the
other hand, the total size of items ofX bins and Y
bins is greater thanNa. Thus,
Case2: There is anactivebin which is not closed. If theactivebin is the last open bin, from Algorithm
A2 the number of L-bins is even. The total size of
items ofZ bins is greater thanNL=2 and the total size
of items ofX bins andY bins is greater thanNa−1.
Analogous to Case 1, we have OPT(L)¿Na+NL=2.
If theactivebin is not the last open bin, then the last open bin must be an L-bin. The sum of item sizes of theactivebin and the L-bin is greater than 1; the total size of items in all closed L-bins is greater than (NL−1)=2; the total size of items in allextrabins and
all closedactivebins is greater thanNa−1. Totally,
the sum of all items is greater thanNa+ (NL−1)=2.
It implies that OPT(L)¿Na+NL=2.
Theorem 5. AlgorithmA2runs in linear time and its
absolute worst-case ratio is 7 4.
Fig. 2.
On the other hand, ifNa+NL=26NLthenNa¡ NL=2,
we have
A2(L)=OPT(L)6(3Na=2 +NL)=NL
= 1 + (3Na)=(2NL)61 +34 =74:
Hence the absolute worst-case ratio ofA2is not greater
than 74.
Suppose that k is an arbitrarily large integer and
= 1=(6k) is a positive number. Consider an instance where we have three types of items. There are 4kitems of typeA with sizes 1−, 2k items of typeB with sizes 3and 4kitems of typeC with sizes 1 +. The incoming order is given below.
L= ((1−; 3; 1−); : : : ;(1−; 3; 1−)
| {z }
2k
;
1 +; : : : ;1 + | {z }
4k
):
Clearly, one item of type Aand one item of type C
can be packed into a bin. All items of typeBcan be placed into a bin. All bins are completely full. Thus OPT = 4k+ 1. In the packing made byA2, there are
7kbins where 2kbins each contain one item of types AandB,kbins each contain two items of typeAand
4k bins each contain one item of type C. Then the
ratio of A2(L) and OPT(L) is arbitrarily close to 74.
We have proved that the absolute worst-case ratio of AlgorithmA2is 74.
Corollary 6. If there is no large item; the absolute worst-case ratio of algorithmA2is 32.
Proof. If there is no large item in any list L, then AlgorithmA2 is the same asA1. We immediately get
A2(L)=OPT(L)632. On the other hand, the following
instance shows that 32 is tight. Given six items with sizes25;25;103;103;103 and103, the optimal packing needs two bins and AlgorithmA2requires three bins.
Acknowledgements
We would like to thank the anonymous referee and the Editor for their comments and suggestions that have improved the presentation.
References
[1] E.G. Coman, M.R. Garey, D.S. Johnson, Approximation algorithms for bin packing: a survey, in: D. Hochbaum (Ed.), Approximation Algorithms for NP-Hard Problems, PWS Publishing Company, Boston, 1996, pp. 46–93. [2] V.W. Fernandez, G.S. Lueker, Bin packing can be solved
within 1 +in linear time, Combinatorica 1 (1981) 349–355. [3] M.R. Garey, D.S. Johnson, Computers and Intractability, A Guide to the Theory of NP-Completeness, Freeman, San Francisco, 1979.
[4] D.S. Johnson, Near-optimal bin packing algorithms, Ph.D. Thesis, MIT, Department of Mathematics, Cambridge, 1973. [5] D.S. Johnson, A. Demers, J.D. Ullman, M.R. Garey, R.L. Graham, Worst-case performance bounds for simple one-dimensional packing algorithms, SIAM J. Comput. 3 (1974) 256–278.
[6] C.C. Lee, D.T. Lee, A simple online bin packing algorithm, J. Assoc. Comput. Mach. 32 (1985) 562–572.
[7] J.K. Lenstra, D.B. Shmoys, Computing near-optimal schedules, in: P. Chretienne et al., (Eds.), Scheduling Theory and Its Application, Wiley, New York, 1995.
[8] C.U. Martel, A linear time bin-packing algorithm, Oper. Res. Lett. 4 (1985) 189–192.
[9] P. Ramanan, D.J. Brown, C.C. Lee, D.T. Lee, On-line bin packing in linear time, J. Algorithms 10 (1989) 305–326. [10] D. Simchi-Levi, New worst-case results for the bin packing