First-fit bin packing

First-fit (FF) 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 first-fit algorithm uses the following heuristic:

  • It keeps a list of open bins, which is initially empty.
  • When an item arrives, find the first bin into which the item can fit, if any.
    • If such a bin is found, the new item is placed inside it.
    • Otherwise, a new bin is opened and the coming item is placed inside it.