First-fit (FF) is an
online algorithm
In computer science, an online algorithm is one that can process its input piece-by-piece in a serial fashion, i.e., in the order that the input is fed to the algorithm, without having the entire input available from the start.
In contrast, an o ...
for
bin packing
The bin packing problem is an optimization problem, in which items of different sizes must be packed into a finite number of bins or containers, each of a fixed given capacity, in a way that minimizes the number of bins used. The problem has m ...
. 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
A heuristic (; ), or heuristic technique, is any approach to problem solving or self-discovery that employs a practical method that is not guaranteed to be optimal, perfect, or rational, but is nevertheless sufficient for reaching an immediat ...
:
* 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.
Approximation ratio
Denote by FF(L) the number of bins used by First-Fit, and by OPT(L) the optimal number of bins possible for the list L. The analysis of FF(L) was done in several steps.
* The first upper bound of
for FF was proven by Ullman
in 1971.
* In 1972, this upper bound was improved to
by Garey, Graham and Ullman,
Johnson and Demers.
* In 1976, it was improved by Garey, Graham, Johnson, Yao and Chi-Chih
to
, which is equivalent to
due to the integrality of
and
.
* The next improvement, by Xia and Tan
in 2010, lowered the bound to
.
* Finally, in 2013, this bound was improved to
by Dósa and Sgall.
They also present an example input list
, for which
matches this bound.
Refined first-fit
Refined-First-Fit (RFF) is another
online algorithm
In computer science, an online algorithm is one that can process its input piece-by-piece in a serial fashion, i.e., in the order that the input is fed to the algorithm, without having the entire input available from the start.
In contrast, an o ...
for
bin packing
The bin packing problem is an optimization problem, in which items of different sizes must be packed into a finite number of bins or containers, each of a fixed given capacity, in a way that minimizes the number of bins used. The problem has m ...
, that improves on the previously developed FF algorithm. It was presented by Andrew Chi-Chih Yao.
The algorithm
The items are categorized in four classes, according to their sizes:
*
-piece - size in
.
*
-piece - size in
.
*
-piece - size in
.
*
-piece - size in
.
Similarly, the bins are categorized into four classes: 1, 2, 3 and 4.
Let
be a fixed integer. The next item
is assigned into a bin in -
* Class 1, if
is an
-piece,
* Class 2, if
is an
-piece,
* Class 3, if
is an
-piece, but not the
th
-piece seen so far, for any integer
.
* Class 1, if
is the
th
-piece seen so far,
* Class 4, if
is an
-piece.
Once the class of the item is selected, it is placed inside items of that class using first-fit bin packing.
Note that RFF is not an Any-Fit algorithm since it may open a new bin despite the fact that the current item fits inside an open bin (from another class).
Approximation ratio
RFF has an approximation guarantee of
. There exists a family of lists
with
for
.
See also
*
First-fit-decreasing bin packing, First-Fit-Decreasing (FFD) is the
offline
In computer technology and telecommunications, online indicates a state of connectivity and offline indicates a disconnected state. In modern terminology, this usually refers to an Internet connection, but (especially when expressed "on line" o ...
variant of First-Fit: it accepts all input items, orders them by descending size, and calls First-Fit. Its asymptotic approximation ratio is much better: about 1.222 instead of 1.7.
Implementations
* Python: Th
prtpy packagecontain
an implementation of first-fit
References
Bin packing