First-fit bin packing explained

First-fit (FF) is an online algorithm for bin packing. Its input is a list of items of different sizes. Its output is a packing - a partition of the items into bins of fixed capacity, such that the sum of sizes of items in each bin is at most the capacity. Ideally, we would like to use as few bins as possible, but minimizing the number of bins is an NP-hard problem. The first-fit algorithm uses the following heuristic:

Approximation ratio

Denote by FF(L) the number of bins used by First-Fit, and by OPT(L) the optimal number of bins possible for the list L. The analysis of FF(L) was done in several steps.

FF(L)\leq1.7OPT+3

for FF was proven by Ullman[1] in 1971.

FF(L)\leq1.7OPT+2

by Garey, Graham and Ullman,[2] Johnson and Demers.[3]

FF(L)\leq\lceil1.7OPT\rceil

, which is equivalent to

FF(L)\leq1.7OPT+0.9

due to the integrality of

FF(L)

and

OPT

.

FF(L)\leq1.7OPT+0.7

.

FF(L)\leq\lfloor1.7OPT\rfloor

by Dósa and Sgall.[6] They also present an example input list

L

, for which

FF(L)

matches this bound.Below we explain the proof idea.

Asymptotic ratio at most 2

Here is a proof that the asymptotic ratio is at most 2. If there is an FF bin with sum less than 1/2, then the size of all remaining items is more than 1/2, so the sum of all following bins is more than 1/2. Therefore, all FF bins except at most one have sum at least 1/2. All optimal bins have sum at most 1, so the sum of all sizes is at most OPT. Therefore, number of FF bins is at most 1+OPT/(1/2) = 2*OPT+1

Asymptotic ratio at most 1.75

Consider first a special case in which all item sizes are at most 1/2. If there is an FF bin with sum less than 2/3, then the size of all remaining items is more than 1/3. Since the sizes are at most 1/2, all following bins (except maybe the last one) have at least two items, and sum larger than 2/3. Therefore, all FF bins except at most one have sum at least 2/3, and the number of FF bins is at most 2+OPT/(2/3) = 3/2*OPT+1.

The "problematic" items are those with size larger than 1/2. So, to improve the analysis, let's give every item larger than 1/2 a bonus of R. Define the weight of an item as its size plus its bonus. Define the weight of a set of items as the sum of weights of its contents.

Now, the weight of each FF bin with one item (except at most one) is at least 1/2+R, and the weight of each FF bin with two or more items (except at most one) is 2/3. Taking R=1/6 yields that the weight of all FF bins is at least 2/3.

On the other hand, the weight of every bin in the optimal packing is at most 1+R = 7/6, since each such bin has at most one item larger than 1/2. Therefore, the total weight of all items is at most 7/6*OPT, and the number of FF bins is at most 2+(7/6*OPT/(2/3)) = 7/4*OPT+2.

Asymptotic ratio at most 1.7

The following proof is adapted from. Define the weight of an input item as the item size x some bonus computed as follows:

bonus(x):=\begin{cases} 0&x\leq1/6 \\ x/2-1/12&1/6<x<1/3 \\ 1/12&1/3\leqx\leq1/2 \\ 1/3&1/2<x \end{cases}

weight(x):=x+bonus(x)

.

The asymptotic approximation ratio follows from two claims:

  1. In the optimal packing, the weight of each bin is at most 17/12;
  2. In the First-Fit packing, the average weight of each bin is at least 5/6 = 10/12.

Therefore, asymptotically, the number of bins in the FF packing must be at most 17/10 * OPT.

For claim 1, it is sufficient to prove that, for any set B with sum at most 1, bonus(B) is at most 5/12. Indeed:

Therefore, the weight of B is at most 1+5/12 = 17/12.

For claim 2, consider first an FF bin B with a single item.

Consider now the FF bins B with two or more items.

Therefore, the total weight of all FF bins is at least 5/6*(FF - 3) (where we subtract 3 for the single one-item bin with sum<1/2, single two-item bin with sum<2/3, and the k-1 from the two-item bins with sum ≥ 2/3).

All in all, we get that 17/12*OPT ≥ 5/6*(FF-3), so FF ≤ 17/10*OPT+3.

Dósa and Sgall present a tighter analysis that gets rid of the 3, and get that FF ≤ 17/10*OPT.

Lower bound

There are instances on which the performance bound of 1.7OPT is tight. The following example is based on.[7] [8] The bin capacity is 101, and:

Performance with divisible item sizes

An important special case of bin-packing is that which the item sizes form a divisible sequence (also called factored). A special case of divisible item sizes occurs in memory allocation in computer systems, where the item sizes are all powers of 2. If the item sizes are divisible, and in addition, the largest item sizes divides the bin size, then FF always finds an optimal packing.[9]

Refined first-fit

Refined-First-Fit (RFF) is another online algorithm for bin packing, that improves on the previously developed FF algorithm. It was presented by Andrew Chi-Chih Yao.[10]

The algorithm

The items are categorized in four classes, according to their sizes (where the bin capacity is 1):

A

-piece - size in

(1/2,1]

.

B1

-piece - size in

(2/5,1/2]

.

B2

-piece - size in

(1/3,2/5]

.

X

-piece - size in

(0,1/3]

.

Similarly, the bins are categorized into four classes: 1, 2, 3 and 4.

Let

m\in\{6,7,8,9\}

be a fixed integer. The next item

i\inL

is assigned into a bin in -

i

is an

A

-piece,

i

is an

B1

-piece,

i

is an

B2

-piece, but not the

(mk)

th

B2

-piece seen so far, for any integer

k\geq1

.

i

is the

(mk)

th

B2

-piece seen so far,

i

is an

X

-piece.

Once the class of the item is selected, it is placed inside bins of that class using first-fit bin packing.

Note that RFF is not an Any-Fit algorithm since it may open a new bin despite the fact that the current item fits inside an open bin (from another class).

Approximation ratio

RFF has an approximation guarantee of

RFF(L)\leq(5/3)OPT(L)+5

. There exists a family of lists

Lk

with

RFF(Lk)=(5/3)OPT(Lk)+1/3

for

OPT(L)=6k+1

.

See also

Implementations

References

  1. Ullman. J. D.. 1971. The performance of a memory allocation algorithm. Technical Report 100 Princeton Univ..
  2. Book: Garey. M. R. Graham. R. L. Ullman. J. D.. Proceedings of the fourth annual ACM symposium on Theory of computing - STOC '72 . Worst-case analysis of memory allocation algorithms . 1972. EN. 143–150. 10.1145/800152.804907. 26654056.
  3. David S. Johnson, Alan J. Demers, Jeffrey D. Ullman, M. R. Garey, Ronald L. Graham. Worst-Case Performance Bounds for Simple One-Dimensional Packing Algorithms. SICOMP, Volume 3, Issue 4. 1974.
  4. Garey. M. R. Graham. R. L. Johnson. D. S. Yao. Andrew Chi-Chih. 1976. Resource constrained scheduling as generalized bin packing. Journal of Combinatorial Theory, Series A. en. 21. 3. 257–298. 10.1016/0097-3165(76)90001-7. 0097-3165. free.
  5. Xia. Binzhou. Tan. Zhiyi. August 2010. Tighter bounds of the First Fit algorithm for the bin-packing problem. Discrete Applied Mathematics. 158. 15. 1668–1675. 10.1016/j.dam.2010.05.026. free.
  6. Dósa. György. Sgall. Jiri. 2013. First Fit bin packing: A tight analysis. 30th International Symposium on Theoretical Aspects of Computer Science (STACS 2013). Schloss Dagstuhl–Leibniz-Zentrum für Informatik. 20. 538–549. 10.4230/LIPIcs.STACS.2013.538. free .
  7. Book: Garey . M. R. . Graham . R. L. . Ullman . J. D. . Proceedings of the fourth annual ACM symposium on Theory of computing - STOC '72 . Worst-case analysis of memory allocation algorithms . 1972-05-01 . https://doi.org/10.1145/800152.804907 . New York, NY, USA . Association for Computing Machinery . 143–150 . 10.1145/800152.804907 . 978-1-4503-7457-6. 26654056 .
  8. Johnson . D. S. . Demers . A. . Ullman . J. D. . Garey . M. R. . Graham . R. L. . December 1974 . Worst-Case Performance Bounds for Simple One-Dimensional Packing Algorithms . SIAM Journal on Computing . en . 3 . 4 . 299–325 . 10.1137/0203025 . 0097-5397.
  9. Coffman . E. G . Garey . M. R . Johnson . D. S . 1987-12-01 . Bin packing with divisible item sizes . Journal of Complexity . 3 . 4 . 406–428 . 10.1016/0885-064X(87)90009-4 . 0885-064X.
  10. Yao. Andrew Chi-Chih. April 1980. New Algorithms for Bin Packing. Journal of the ACM. 27. 2. 207–227. 10.1145/322186.322187. 7903339. free.