learngraphtheory.org

Guest User

Using app without sign in

Cover of the book Graph Theory Through Problems

Practice book

Graph Theory Through Problems

300 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.

Available inKindlePaperback

13 chapters

Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on Amazon
Algorithm Selection

Select an algorithm and generate steps to begin visualization

Bin Packing Calculator

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

Time: O(n log n + n·b)
Space: O(n)
Use Case: Truck and container loading, cutting stock, server placement
Showing the example graph for this algorithm.
10200
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Bin Packing (First Fit Decreasing)

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.

How it works

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.

Applications

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.

Pseudocode

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 bins

Placing 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.

Worked example, step by step

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.

  1. Large items first. A (30) opens bin 1. B (25) does not fit beside A and opens bin 2. C (20) would fit in bin 1 but conflicts with A, so it goes into bin 2, which reaches 45.
  2. Fill bin 1. D (20) does not conflict with A and fills bin 1 exactly: 50 out of 50.
  3. The conflict that costs a bin. E (15) fits in neither full bin and opens bin 3. F (15) cannot join E because of the conflict E-F, and bins 1 and 2 are too full, so F opens bin 4.
  4. Small items. G (10) and H (10) go into bin 3, which reaches 35. I (5) fills bin 2 to exactly 50. Bin 4 holds only F.
  5. Compare with the optimum. The visualizer also searches for the best packing, and finds three full bins: {A, D}, {B, E, H} and {C, F, G, I}. None of them contains a conflicting pair.

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.

Complexity, and where it comes from

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.

When to use Bin Packing (First Fit Decreasing), and when not to

Packing heuristics trade speed for quality, and exact methods are practical on surprisingly large plain instances.

AlternativePrefer it whenCost
First fitItems 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 decreasingLike 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 boundYou need the optimum on instances up to a few hundred items. Martello and Toth bounds prune well.exponential worst case
Graph colouringSizes do not matter, only conflicts. Then the bins are colour classes, and DSatur is a good heuristic.NP-hard
Capacitated vehicle routingThe bins are trucks that must also drive a route, so travel distance matters as well as the packing.NP-hard

Common pitfalls

  • Not sorting the items. Plain first fit, in arrival order, can waste a lot of space when small items fill bins early and large items later need new bins. Sorting by decreasing size is cheap and lowers the worst case from 1.7 to about 1.22 times the optimum.
  • Ignoring the lower bound. The sum of sizes divided by the capacity, rounded up, is a lower bound. When a heuristic matches it, the solution is optimal and further search is pointless. When it does not, the gap shows how much there may be to gain.
  • Adding conflicts after packing. Packing by size and then moving conflicting items out produces many half-empty bins. The conflict check must be part of the placement decision, as in the pseudocode.
  • Items larger than a bin. An item larger than the capacity can never be placed. Check for it before packing, otherwise the algorithm opens a bin that is already overloaded.

Frequently asked questions

What is the bin packing problem?
Given items with sizes and bins of a fixed capacity, the bin packing problem asks for the smallest number of bins that can hold all the items without exceeding any capacity. It is NP-hard and appears in loading trucks, cutting stock, memory allocation and cloud server placement.
What is first fit decreasing?
A heuristic that sorts the items from largest to smallest and places each one into the first open bin where it fits, opening a new bin only when none does. It uses at most 11/9 of the optimal number of bins plus a small constant.
What is bin packing with conflicts?
A variant in which some pairs of items must not be placed in the same bin, for example chemicals that react or goods for different customers. The conflicts form a graph, and every bin must be an independent set of that graph.
Is bin packing NP-hard?
Yes. Deciding whether items fit into two bins is already the NP-complete partition problem. In practice, however, exact methods solve many instances with hundreds of items, and simple heuristics come very close to the optimum.
How is bin packing related to graph colouring?
When the items are so small that capacity never matters, bin packing with conflicts becomes graph colouring: each bin is a colour class and conflicting items need different colours. Bin packing with conflicts therefore generalises both problems.

Related algorithms: Graph Coloring, DSatur Coloring, Capacitated Vehicle Routing (CVRP), p-Median (Facility Location)

Interactive Controls

Zoom Controls

49%
Nodes: 9
Edges: 4