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:
- It keeps a current bin, which is initially empty.
- When an item arrives, it checks whether the item fits into the current bin.
- If it fits, it is placed inside it.
- Otherwise, the current bin is closed, a new bin is opened and the coming item is placed inside this new bin.
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
, where
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
,
. 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
. But since the first one has at least a space of
, the algorithm will not open a new bin for any item whose size is at most
. Only after the bin fills with more than
or if an item with a size larger than
arrives, the algorithm may open a new bin. Thus if we have
bins, at least
bins are more than half full. Therefore,
\sumis(i)>\tfrac{K-1}{2}B
. Because
is a lower bound of the optimum value
, we get that
and therefore
.
Lower bound
For each
, there exists a list
such that
and
.
The family of lists for which it holds that
is given by
with
. The optimal solution for this list has
bins containing two items with size
and one bin with
items with size
(i.e.,
bins total), while the solution generated by NF has
bins with one item of size
and one item with size
.
Bounded item size
If the maximum size of an item is
, then the asymptotic approximation ratio ratio
satisfies:
for all
;
for all
.
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
bins open and chooses the first bin in which the item fits.
For
, NkF delivers results that are improved compared to the results of NF, however, increasing
to constant values larger than
improves the algorithm no further in its worst-case behavior. If algorithm
is an AlmostAnyFit-algorithm and
m=\lfloor1/\alpha\rfloor\geq2
then
(size\leq\alpha)\leq
(size\leq\alpha)=1+1/m
.
See also
- Next-fit-decreasing (NFD) is the offline variant of Next-Fit: it accepts all input items, orders them by descending size, and calls Next-Fit. Its asymptotic approximation ratio is much better: less than 1.7, instead of 2.
References
- Johnson. David S. 1973. Near-optimal bin packing algorithms. Massachusetts Institute of Technology.
- Web site: Suri . Subhash . Bin Packing . UCSB Computer Science . 7 October 2021.
- 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. .