Next-fit bin packing

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 in 1973.

Run time
The running time of NextFit can be bounded by $$\mathcal{O}(n)$$, where $$n$$ is the number of items in the list.

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) \leq 2 \cdot \mathrm{OPT}(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, $$\sum_{i \in I} s(i)>\tfrac{K-1}{2}B$$. Because $$\tfrac{\sum_{i \in I} s(i)}{B}$$ is a lower bound of the optimum value $$\mathrm{OPT}$$, we get that $$K-1<2\mathrm{OPT}$$ and therefore $$K \leq 2\mathrm{OPT}$$.

Lower bound
For each $$N \in \mathbb{N}$$, there exists a list $$L$$ such that $$\mathrm{OPT}(L) = N$$ and $$NF(L) = 2 \cdot \mathrm{OPT}(L) -2$$.

The family of lists for which it holds that $$NF(L) = 2 \cdot \mathrm{OPT}(L) - 2$$ is given by $$L := \left(\frac{1}{2},\frac{1}{2(N-1)},\frac{1}{2},\frac{1}{2(N-1)}, \dots, \frac{1}{2},\frac{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 $$R_{NF}^\infty$$ satisfies:


 * $$R_{NF}^\infty(\text{size}\leq\alpha) \leq 2$$ for all $$\alpha \geq 1/2$$;
 * $$R_{NF}^\infty(\text{size}\leq\alpha) \leq 1/(1-\alpha)$$ for all $$\alpha \leq 1/2$$.

Other properties
Next-Fit packs a list and its inverse into the same number of bins.

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\geq 2$$, 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 = \lfloor 1/\alpha\rfloor \geq 2$$ then $$R_{A}^{\infty}(\text{size}\leq\alpha)\leq R_{N2F}^{\infty}(\text{size}\leq\alpha) = 1+1/m$$.