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
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
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
, the algorithm will not open a new bin for any item whose size is at most
or if an item with a size larger than
arrives, the algorithm may open a new bin.
bins are more than half full.
is a lower bound of the optimum value
The family of lists for which it holds that
The optimal solution for this list has
bins containing two items with size
items with size
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
satisfies: Next-Fit packs a list and its inverse into the same number of bins.
[4] 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.
, 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.