Guest User
Using app without sign in

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
Fewest bins, nothing that clashes side by side
Packs the vertices (items, size = demand) into the fewest bins, largest first into the first bin that fits, and an edge keeps two items apart
Select an algorithm and generate steps to begin visualization
The bin packing problem puts items of different sizes into as few bins of a fixed capacity as possible: parcels into trucks, files onto disks, jobs onto machines, cuts from stock lengths. It is NP-hard and one of the classic problems of operations research. This page also handles conflicts, drawn as edges: two items joined by an edge must not share a bin, as with chemicals and food in one truck or two exams with the same students in the same time slot. With conflicts the problem mixes bin packing with graph colouring, and with no edges it is plain bin packing.
First Fit Decreasing sorts the items from largest to smallest, because big items are the hardest to place. It then takes them one at a time and puts each into the first bin that still has room and holds none of its conflicts, opening a new bin only when no bin can take it. Without conflicts FFD never uses more than 11/9 of the optimal number of bins plus 6/9, a bound György Dósa proved tight in 2007. The total size divided by the capacity, rounded up, is a lower bound on the number of bins. For up to twelve items the page also searches the possible packings for the true minimum, and shows that packing when it beats FFD.
Bin packing plans truck, container and pallet loading, the cutting of steel bars, timber and paper rolls (the cutting stock problem), storage allocation on disks and in memory, and the placement of virtual machines on servers in data centres. Conflicts appear when dangerous goods must be kept apart, when exams that share students cannot run in the same slot, and when tasks that use the same resource cannot share a processor.
Bin packing puts items of given sizes into as few bins of fixed capacity as possible. In the version shown here some pairs of items conflict and must not share a bin. The visualizer uses first fit decreasing, extended with a conflict check.
FirstFitDecreasing(items, capacity, conflicts):
sort the items by size, largest first
bins = []
for each item x in that order:
for each bin b in bins, oldest first:
if load(b) + size(x) <= capacity
and no item in b conflicts with x:
put x in b, next item
if x was not placed:
open a new bin containing x
return binsPlacing the large items first leaves the small ones to fill the gaps, which is why sorting improves plain first fit so much. Without conflicts, Dósa proved in 2007 the tight bound FFD at most 11/9 of the optimum plus 6/9. Conflicts break that guarantee, because they can forbid exactly the combinations that fill the bins.
Load nine consignments into trucks of capacity 50 on the example this page loads. An edge between two items means they must travel in different trucks.
Example graph: Items A 30, B 25, C 20, D 20, E 15, F 15, G 10, H 10 and I 5, which add up to 150, so at least three trucks are needed. Conflicts A-C, B-D, E-F and H-I.
First fit decreasing uses 4 bins, while 3 are enough, the lower bound of 150 / 50. Interestingly, without the conflict edges FFD would have found three full bins on its own: {A, C}, {B, D, I} and {E, F, G, H}. Three of the four conflicts forbid exactly those bins, which shows how conflicts turn an easy packing problem into a colouring problem as well.
Time: NP-hard, O(n log n + b (n + m)) for FFD · Space: O(n + m)
Sorting the n items costs O(n log n). Each item is then tried against up to b open bins. The capacity test is O(1) per bin, and the conflict test looks at the conflicting neighbours of the item, so over the whole run the conflict checks cost O(b m) for m conflict edges. Plain bin packing is NP-hard, which follows from the partition problem, so no polynomial method is exact unless P = NP. With conflicts it is harder still: if all items are tiny, the problem is exactly graph colouring, so no constant approximation factor is possible on general conflict graphs.
Packing heuristics trade speed for quality, and exact methods are practical on surprisingly large plain instances.
| Alternative | Prefer it when | Cost |
|---|---|---|
| First fit | Items arrive online and cannot be sorted. Proven to use at most 1.7 times the optimum. | O(n log n) with a tree |
| Best fit decreasing | Like FFD, but each item goes into the fullest bin that fits. Same worst-case bound, often slightly better in practice. | O(n log n) |
| Exact branch and bound | You need the optimum on instances up to a few hundred items. Martello and Toth bounds prune well. | exponential worst case |
| Graph colouring | Sizes do not matter, only conflicts. Then the bins are colour classes, and DSatur is a good heuristic. | NP-hard |
| Capacitated vehicle routing | The bins are trucks that must also drive a route, so travel distance matters as well as the packing. | NP-hard |
Related algorithms: Graph Coloring, DSatur Coloring, Capacitated Vehicle Routing (CVRP), p-Median (Facility Location)