Bin packing problem
Not to be confused with Bin picking.
The bin packing problem is an optimization problem. In this problem, items of different sizes must be packed into a limited number of bins or containers, each with a fixed capacity. The goal is to use as few bins as possible. The problem has many uses, such as filling up containers, loading trucks with weight limits, making file backups on media, splitting a network prefix into several subnets, and mapping technology in FPGA semiconductor chip design.
The problem is NP-hard, which means it is very difficult to solve in the worst case. The related decision problem, which asks whether items can fit into a set number of bins, is NP-complete. Even so, the best solutions for very large problems can be found with advanced algorithms. Many approximation algorithms also exist. For example, the first fit algorithm is fast but often does not give the best answer. It places each item into the first bin where it will fit. Its running time is Θ of n log n, where n is the number of items. The algorithm works much better if the items are first sorting into decreasing order, a method sometimes called first-fit decreasing. Even so, this does not always give the best answer, and for long lists it can make the algorithm take longer. It is known, however, that there is always at least one order of items that lets first fit find the best packing.
There are many variations of this problem, including 2D packing, linear packing, packing by weight, and packing by cost. The bin packing problem can also be seen as a special case of the cutting stock problem. When there is only one bin and each item has both a volume and a value, the task of fitting the most valuable items into the bin is called the knapsack problem.
One variant that appears in practice allows items to share space when they are packed together. A group of items may take up less room together than the sum of their separate sizes. This is called VM packing. When virtual machines (VMs) are placed on a server, their total memory requirement can go down because some pages are shared between VMs and only need to be stored once. If items can share space in any way, the bin packing problem is very hard to even approximate. However, if the sharing follows a hierarchy, as memory sharing in virtual machines does, the problem can be approximated efficiently.
Another practical variant is called online bin packing. Here, items of different sizes arrive one after another, and the decision maker must choose whether to pack each item when it is seen or let it pass. Each decision cannot be undone. In contrast, offline bin packing allows items to be rearranged in the hope of a better packing once more items arrive. This requires extra storage to hold the items that may be rearranged.
Formal statement
In Computers and Intractability, Garey and Johnson list the bin packing problem as number SR1. They describe a yes-or-no version of the problem. You are given a list of items, each with a whole-number size, a bin capacity B, and a number K. The question is whether the items can be split into K groups, called disjoint sets, so that the total size in each group is B or less. Many researchers use a different but related way of writing the problem, where the bin capacity is 1 and item sizes are fractions between 0 and 1. Most research focuses on finding the smallest possible K, which is the best answer, called the optimal solution. The optimal K for a set of items is written as OPT(I), or just OPT.
One way to write the problem is as an integer linear programming formula, which uses yes-or-no choices to show which bins are used and which items go in which bin:
Hardness of bin packing
The bin packing problem is strongly NP-complete. This can be shown by connecting it to the strongly NP-complete 3-partition problem.
There also cannot be an approximation algorithm with a very small error ratio, unless P equals NP. On the other hand, the problem can be solved in pseudo-polynomial time for any fixed number of bins, and in polynomial time for any fixed bin capacity.
Approximation algorithms for bin packing
To measure how well an approximation algorithm works, there are two ratios used in research. One compares the number of bins the algorithm uses with the best possible number for a list of items. The other looks only at lists that need a large best possible number of bins, and allows a small fixed extra amount. Lists can also be limited to items that are no bigger than a certain size.
Approximation algorithms for bin packing fall into two groups. Online heuristics take items in a given order and place them one by one into bins, and they can also be used when all the items are known in advance. Offline heuristics change the list first, for example by sorting items by size. They give better guarantees while still running quickly, but they cannot be used for the online version. A sub-category, asymptotic approximation schemes, get very close to the best answer for large lists, but they need a much greater time complexity than the simpler heuristics.
Online heuristics
In the online version of the bin packing problem, items arrive one after another. Each item must be placed right away, without knowing what comes next or whether more items will come. David S. Johnson studied a wide range of offline and online packing methods, called heuristics, in his Ph.D. thesis.
Many simple methods follow the same idea: put an item into an open bin if it fits, and otherwise open a new bin. They differ in how they choose the bin. Next Fit keeps only one bin open at a time, while Next-k-Fit keeps the last k bins open. First-Fit uses the first bin where the item fits, and Best-Fit uses the fullest bin where it fits. Some better methods, such as Refined-first-fit bin packing and Harmonic-k, sort items into groups by size and keep separate bins for each group.
Offline algorithms
In the offline version of bin packing, the method can see all the items before it starts placing them into bins. This helps it get better results. A simple technique is to sort the items from largest to smallest and then run a method that places items one at a time. First-fit-decreasing sorts the items from largest to smallest and then uses First-Fit. Next-fit-decreasing sorts the items and then uses Next-Fit. Modified first-fit-decreasing improves on the first method for large items by sorting items into four size groups. Fernandez de la Vega and Lueker presented a PTAS method that groups and rounds up item sizes, then solves the simpler problem with a configuration linear program. The Karmarkar-Karp bin packing algorithm finds a solution that uses only a few extra bins compared with the best possible answer.
For finding the exact best answer, Martello and Toth developed an exact algorithm called MTP. A faster option is Bin Completion, proposed by Richard E. Korf in 2002. When there are only a few different item sizes, the problem is called high-multiplicity bin packing, and it can be solved more efficiently.
| Algorithm | Approximation guarantee | Worst case instance |
|---|---|---|
| First-fit-decreasing (FFD) | FFD(I)≤119OPT(I)+69 | FFD(I)=119OPT(I)+69 |
| Modified-first-fit-decreasing (MFFD) | MFFD(I)≤7160OPT(I)+1 | RMFFD∞≥7160 |
| Karmarkar and Karp | KK(I)≤OPT(I)+O(log2OPT(I)) | |
| Rothvoss | HB(I)≤OPT(I)+O(logOPT(I)loglogOPT(I)) | |
| Hoberg and Rothvoss | HB(I)≤OPT(I)+O(logOPT(I)) |
Bin packing with fragmentation
Bin packing with fragmentation or fragmentable object bin packing is a version of the bin packing problem where items may be broken into parts, and each part can go into a different bin. Breaking items up can help use fewer bins overall and can make finding the best plan easier, because some choices become continuous. However, breaking items apart can be costly. The problem was first introduced by Mandal, Chakrabarti and Ghose. Finding the best plan is a computational problem that can be hard to solve.
Mandal, Chakrabarti and Ghose proved that one version is strongly NP-hard, and Menakerman and Rom showed that both main versions are too. Even so, researchers have built methods based on classic ideas like next-fit and first-fit decreasing. Other researchers studied a version where each item can be split in only one way, which is useful for the vehicle routing problem. Some also developed approximation schemes, including an asymptotic FPTAS. A related problem, fractional knapsack with penalties, was introduced by Malaguti, Monaci, Paronuzzi and Pferschy, who developed an FPTAS and a dynamic program for it.
See also: Fractional job scheduling
Performance with divisible item sizes
An important special case of bin packing is when the item sizes form a divisible sequence (also called factored). One example is memory allocation in computer systems, where all the item sizes are powers of 2. When the item sizes are divisible, some of the heuristic algorithms for bin packing find an optimal solution.
Cardinality constraints on the bins
There is a version of the bin packing problem with cardinality limits on the bins. Each bin can hold at most k items, where k is a fixed whole number. Krause, Shen and Schwetman describe this as a version of optimal job scheduling. They offer several methods that find solutions using at most 2OPT bins.
Kellerer and Pferschy present a method that finds a solution with at most ⌈3/2 OPT⌉ bins. Their method uses a binary search to look for the best value of OPT.
Non-additive functions
Researchers have studied ways to change the bin packing problem so it can handle more general costs and loads. Anily, Bramel and Simchi-Levi look at a case where the cost of a bin is a concave function of how many items it holds, and the goal is to lower the total cost instead of the number of bins. They show that next-fit-increasing bin packing has a worst-case approximation ratio of at most 7/4, and an asymptotic worst-case ratio of 1.691 for any concave and monotone cost function.
Cohen, Keller, Mirrokni and Zadimoghaddam study a case where the size of each item is not known ahead of time but is a random variable. This is common in cloud computing, where users usually need much less than the most they could use. Because of this, a cloud manager may gain a lot by slight overcommitment. They show that this problem can be treated as a related problem, called submodular bin packing, where the load in each bin is a certain submodular function of the items rather than their simple sum.
Related problems
In the bin packing problem, the size of the bins is fixed, and their number can grow, but it should be as small as possible. In contrast, in the multiway number partitioning problem, the number of bins is fixed and their size can grow. The aim is to make the bins as nearly equal in size as possible. In the variant called multiprocessor scheduling, or the minimum makespan problem, the goal is to make the largest bin as small as possible. Other related problems change the rules in different ways. In the bin covering problem, the bins must be at least a certain size, and the goal is to fill as many bins as possible. In the fair chore problem, a variant of fair item allocation, the items are chores that different people find more or less difficult, and each person gets a set of chores that is not too hard. In guillotine cutting, the items and bins are flat rectangles instead of single numbers.
Other versions include two-dimensional bin packing, three-dimensional bin packing, and bin packing with delivery. In logistics and online shopping, three-dimensional bin packing helps choose a box and decide how to fit a set of items inside it. Related industrial problems include loading containers and pallets.
Resources
- BPPLIB - a library of surveys, codes, benchmarks, generators, solvers, and bibliography.

Quiz time!
5 questions about Bin packing problem
What is the main goal of the bin packing problem?
Keep exploring
This article is a child-friendly adaptation of the Wikipedia article on Bin packing problem, available under CC BY-SA 4.0.