First-fit bin packing
First-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 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.
Approximation ratio
- 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.
Asymptotic ratio at most 2
Here is a proof that the asymptotic ratio is at most 2. If there is an FF bin with sum less than 1/2, then the size of all remaining items is more than 1/2, so the sum of all following bins is more than 1/2. Therefore, all FF bins except at most one have sum at least 1/2. All optimal bins have sum at most 1, so the sum of all sizes is at most OPT. Therefore, number of FF bins is at most 1+OPT/ = 2*OPT+1Asymptotic ratio at most 1.75
Consider first a special case in which all item sizes are at most 1/2. If there is an FF bin with sum less than 2/3, then the size of all remaining items is more than 1/3. Since the sizes are at most 1/2, all following bins have at least two items, and sum larger than 2/3. Therefore, all FF bins except at most one have sum at least 2/3, and the number of FF bins is at most 2+OPT/ = 3/2*OPT+1.The "problematic" items are those with size larger than 1/2. So, to improve the analysis, let's give every item larger than 1/2 a bonus of R. Define the weight of an item as its size plus its bonus. Define the weight of a set of items as the sum of weights of its contents.
Now, the weight of each FF bin with one item is at least 1/2+R, and the weight of each FF bin with two or more items is 2/3. Taking R=1/6 yields that the weight of all FF bins is at least 2/3.
On the other hand, the weight of every bin in the optimal packing is at most 1+R = 7/6, since each such bin has at most one item larger than 1/2. Therefore, the total weight of all items is at most 7/6*OPT, and the number of FF bins is at most 2+ = 7/4*OPT+2.
Asymptotic ratio at most 1.7
The following proof is adapted from. Define the weight of an input item as the item size x some bonus computed as follows:The asymptotic approximation ratio follows from two claims:
- In the optimal packing, the weight of each bin is at most 17/12;
- In the First-Fit packing, the average weight of each bin is at least 5/6 = 10/12.
For claim 1, it is sufficient to prove that, for any set B with sum at most 1, bonus is at most 5/12. Indeed:
- If B has no item larger than 1/2, then it has at most five items larger than 1/6, and the bonus of each of them is at most 1/12;
- If B has an item larger than 1/2 but no item in , then it has room for at most two items in, and the sum of their bonuses is at most = 1/12, so the total bonus is 4/12+1/12=5/12.
- If B has an item larger than 1/2 and an item in , then it has no more room for items of size larger than 1/6, so the total bonus is again 4/12+1/12 = 5/12.
For claim 2, consider first an FF bin B with a single item.
- If sum<1/2, then - by the way FF works - all items processed after B must be larger than 1/2. Therefore, there is at most one FF bin with sum<1/2.
- Consider now all other bins B with a single item with sum>1/2. For all these bins, weight>1/2+1/3 = 5/6.
- If sum<2/3, then - by the way FF works - all items processed after B must be larger than 1/3. Therefore, all following bins with two or more items are larger than 2/3. So there is at most one FF bin with two or more items and sum<2/3.
- Consider now all other bins with two or more items and sum>2/3. Denote them by B,B,...B, by the order they are opened. For each i in 1,...,k, we prove that the sum of B plus the bonus of B is at least 5/6: sum+bonus ≥ 5/6. Indeed, if sum ≥ 5/6 then the inequality is trivial. Otherwise, let sum := 1 - x. Note that x is between 1/6 and 2/6, since sum is between 2/3 and 5/6. Since B is opened after B, B contains at least two items, say c1 and c2, that do not fit into B, that is: c1,c2 > 1-sum = x > 1/6. Then the bonus of each of c1 and c2 is at least x/2 - 1/12. Therefore, the bonus of B is at least x-1/6, so sum + bonus ≥ + = 5/6.
- We can apply the above inequality to successive pairs, and get: sum + bonus + sum + bonus +... + sum + bonus ≥ 5/6*.
All in all, we get that 17/12*OPT ≥ 5/6*, so FF ≤ 17/10*OPT+3.
Dósa and Sgall present a tighter analysis that gets rid of the 3, and get that FF ≤ 17/10*OPT.
Lower bound
There are instances on which the performance bound of 1.7OPT is tight. The following example is based on. The bin capacity is 101, and:- The sequence is: 6, 10, 16, 34, 51.
- The optimal packing contains 10 bins: , . All bin sums are 101.
- The First Fit packing contains 17 bins: , , , .
- * The bin sums are: 92, 68, 68, 51.
- * The rewards are 0, 0, 16.8, 33.7.
- * The total weights are 92, 68, 84.8, 84.7. It can be seen that almost all weights are close to 101*5/6=84.1.
Performance with divisible item sizes
Refined first-fit
Refined-First-Fit is another online algorithm for bin packing, 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.
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.
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.
Approximation ratio
RFF has an approximation guarantee of. There exists a family of lists with for.Implementations
- Python: The contains .