Next-fit bin packing

From HandWiki

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 [math]\displaystyle{ \mathcal{O}(n) }[/math], where [math]\displaystyle{ n }[/math] 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 [math]\displaystyle{ L }[/math], [math]\displaystyle{ NF(L) \leq 2 \cdot \mathrm{OPT}(L) -1 }[/math]. 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 [math]\displaystyle{ B/2 }[/math]. But since the first one has at least a space of [math]\displaystyle{ B/2 }[/math], the algorithm will not open a new bin for any item whose size is at most [math]\displaystyle{ B/2 }[/math]. Only after the bin fills with more than [math]\displaystyle{ B/2 }[/math] or if an item with a size larger than [math]\displaystyle{ B/2 }[/math] arrives, the algorithm may open a new bin. Thus if we have [math]\displaystyle{ K }[/math] bins, at least [math]\displaystyle{ K-1 }[/math] bins are more than half full. Therefore, [math]\displaystyle{ \sum_{i \in I} s(i)\gt \tfrac{K-1}{2}B }[/math]. Because [math]\displaystyle{ \tfrac{\sum_{i \in I} s(i)}{B} }[/math] is a lower bound of the optimum value [math]\displaystyle{ \mathrm{OPT} }[/math], we get that [math]\displaystyle{ K-1\lt 2\mathrm{OPT} }[/math] and therefore [math]\displaystyle{ K \leq 2\mathrm{OPT} }[/math].[3]:74

Lower bound

For each [math]\displaystyle{ N \in \mathbb{N} }[/math], there exists a list [math]\displaystyle{ L }[/math] such that [math]\displaystyle{ \mathrm{OPT}(L) = N }[/math] and [math]\displaystyle{ NF(L) = 2 \cdot \mathrm{OPT}(L) -2 }[/math].

The family of lists for which it holds that [math]\displaystyle{ NF(L) = 2 \cdot \mathrm{OPT}(L) - 2 }[/math] is given by [math]\displaystyle{ 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) }[/math] with [math]\displaystyle{ |L| = 4(N-1) }[/math]. The optimal solution for this list has [math]\displaystyle{ N - 1 }[/math] bins containing two items with size [math]\displaystyle{ 1/2 }[/math] and one bin with [math]\displaystyle{ 2(N-1) }[/math] items with size [math]\displaystyle{ 1/2(N-1) }[/math] (i.e., [math]\displaystyle{ N }[/math] bins total), while the solution generated by NF has [math]\displaystyle{ 2(N-1) }[/math] bins with one item of size [math]\displaystyle{ 1/2 }[/math] and one item with size [math]\displaystyle{ 1/(2(N-1)) }[/math].

Bounded item size

If the maximum size of an item is [math]\displaystyle{ \alpha }[/math], then the asymptotic approximation ratio ratio [math]\displaystyle{ R_{NF}^\infty }[/math] satisfies:

  • [math]\displaystyle{ R_{NF}^\infty(\text{size}\leq\alpha) \leq 2 }[/math] for all [math]\displaystyle{ \alpha \geq 1/2 }[/math];
  • [math]\displaystyle{ R_{NF}^\infty(\text{size}\leq\alpha) \leq 1/(1-\alpha) }[/math] for all [math]\displaystyle{ \alpha \leq 1/2 }[/math].

Other properties

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

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 [math]\displaystyle{ k }[/math] bins open and chooses the first bin in which the item fits.

For [math]\displaystyle{ k\geq 2 }[/math], NkF delivers results that are improved compared to the results of NF, however, increasing [math]\displaystyle{ k }[/math] to constant values larger than [math]\displaystyle{ 2 }[/math] improves the algorithm no further in its worst-case behavior. If algorithm [math]\displaystyle{ A }[/math] is an AlmostAnyFit-algorithm and [math]\displaystyle{ m = \lfloor 1/\alpha\rfloor \geq 2 }[/math] then [math]\displaystyle{ R_{A}^{\infty}(\text{size}\leq\alpha)\leq R_{N2F}^{\infty}(\text{size}\leq\alpha) = 1+1/m }[/math].[1]

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