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.
The running time of NextFit can be bounded by , where is the number of items in the list. [2]
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.
Then, for each list , . For the sake of convenience, assume that each bin has a maximum capacity of . The intuition behind the proof is the following. If is the optimal number of bins, then . This follows from the fact that otherwise, at least one bin in an optimal solution would exceed the capacity, and that is an integer.
Due to the structure of the algorithm, the total size of two consecutive bins (except for the last one) exceeds . This means that if is the number of bins produced by the algorithm, then .
Combining both inequalities, we get , thus .
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 .
If the maximum size of an item is , then the asymptotic approximation ratio ratio satisfies:
Next-Fit packs a list and its inverse into the same number of bins. [3]
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 then . [1]