Next-fit bin packing explained

Next-fit 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 next-fit algorithm uses the following heuristic:

Next-Fit is a bounded space algorithm - it requires only one partially-filled bin to be open at any time. The algorithm was studied by David S. Johnson in his doctoral thesis[1] in 1973.

Run time

The running time of NextFit can be bounded by

l{O}(n)

, where

n

is the number of items in the list.[2]

Approximation ratio

Denote by NF(L) the number of bins used by NextFit, and by OPT(L) the optimal number of bins possible for the list L.

Upper bound

Then, for each list

L

,

NF(L)\leq2OPT(L)-1

. The intuition to the proof s the following. The number of bins used by this algorithm is no more than twice the optimal number of bins. In other words, it is impossible for 2 bins to be at most half full because such a possibility implies that at some point, exactly one bin was at most half full and a new one was opened to accommodate an item of size at most

B/2

. But since the first one has at least a space of

B/2

, the algorithm will not open a new bin for any item whose size is at most

B/2

. Only after the bin fills with more than

B/2

or if an item with a size larger than

B/2

arrives, the algorithm may open a new bin. Thus if we have

K

bins, at least

K-1

bins are more than half full. Therefore,

\sumis(i)>\tfrac{K-1}{2}B

. Because

\tfrac{\sumis(i)}{B}

is a lower bound of the optimum value

OPT

, we get that

K-1<2OPT

and therefore

K\leq2OPT

.

Lower bound

For each

N\inN

, there exists a list

L

such that

OPT(L)=N

and

NF(L)=2OPT(L)-2

.

The family of lists for which it holds that

NF(L)=2OPT(L)-2

is given by

L:=\left(

1,
2
1,
2(N-1)
1,
2
1
2(N-1)

,...,

1,
2
1
2(N-1)

\right)

with

|L|=4(N-1)

. The optimal solution for this list has

N-1

bins containing two items with size

1/2

and one bin with

2(N-1)

items with size

1/2(N-1)

(i.e.,

N

bins total), while the solution generated by NF has

2(N-1)

bins with one item of size

1/2

and one item with size

1/(2(N-1))

.

Bounded item size

If the maximum size of an item is

\alpha

, then the asymptotic approximation ratio ratio
infty
R
NF
satisfies:
infty(size\leq\alpha)
R
NF

\leq2

for all

\alpha\geq1/2

;
infty(size\leq\alpha)
R
NF

\leq1/(1-\alpha)

for all

\alpha\leq1/2

.

Other properties

Next-Fit packs a list and its inverse into the same number of bins.[3]

Next-k-Fit (NkF)

Next-k-Fit is a variant of Next-Fit, but instead of keeping only one bin open, the algorithm keeps the last

k

bins open and chooses the first bin in which the item fits.

For

k\geq2

, NkF delivers results that are improved compared to the results of NF, however, increasing

k

to constant values larger than

2

improves the algorithm no further in its worst-case behavior. If algorithm

A

is an AlmostAnyFit-algorithm and

m=\lfloor1/\alpha\rfloor\geq2

then
infty
R
A

(size\leq\alpha)\leq

infty
R
N2F

(size\leq\alpha)=1+1/m

.

See also

References

  1. Johnson. David S. 1973. Near-optimal bin packing algorithms. Massachusetts Institute of Technology.
  2. Web site: Suri . Subhash . Bin Packing . UCSB Computer Science . 7 October 2021.
  3. 1988-12-01. Next-fit packs a list and its reverse into the same number of bins. Operations Research Letters. en. 7. 6. 291–293. 10.1016/0167-6377(88)90060-0. 0167-6377 . Fisher . David C. .