AA10, AA11 - Bin packing algorithms
Bin packing places indivisible items into containers of fixed capacity, usually aiming to use as few containers as possible. You will apply first fit, first fit decreasing and full-bin strategies, justify optimal results using bounds, and count the comparisons made by a precisely stated packing procedure.
First fit keeps the original item order
Could filling one bin perfectly make the eventual packing worse? An attractive local choice can leave awkward items behind. First learn a completely specified rule, so that you can distinguish following an algorithm from improving its answer by inspection.
You already know
A heuristic can give a feasible solution efficiently without guaranteeing an optimum. Exact operation counts depend on the rules, and a counterexample can disprove a general guarantee.
Assume every item has positive size at most the bin capacity , and items cannot be split. Number bins in the order they are opened.
First fit
Take the items in their given order. For each item, inspect existing bins from bin upwards, and place it in the first bin with enough remaining capacity. If none fits, open the next bin.
Do not reorder the items or move an earlier item to improve the packing.
“First” refers to the first suitable bin, not the emptiest bin, the fullest bin or the most recently opened one. A bin that failed to fit an earlier large item may fit a later smaller item.
Worked example 1: follow the original order
Question. Apply first fit to items , in that order, using bins of capacity .
Plan. Try each existing bin in increasing numerical order, opening a new one only when necessary.
| Item | Placement and reason | Bin loads afterwards |
|---|---|---|
| Open bin | ||
| Bin has room | ||
| Bin has only spare; open bin | ||
| Bins have only spare; open bin |
Why this step? The first two items must be placed together by first fit, even if a different arrangement might later be better.
Answer. Bin : ; bin : ; bin : . Three bins are used.
Check. All four items appear once, and the loads are at most .
Change only the second item's size from to . The original order is still compulsory.
Worked example 2: a smaller item does not change this bin count
Question. Apply first fit to , with capacity .
Plan. Update the remaining capacity after each placement and inspect bins from the beginning each time.
| Item | Placement and reason | Bin loads afterwards |
|---|---|---|
| Open bin | ||
| Fits bin | ||
| Bin has spare; open bin | ||
| Neither existing bin fits; open bin |
Why this step? A smaller second item leaves more space, but still not enough to fit either later item of size .
Answer. Bins contain , , : three bins.
Check. The total size is , matching loads , with no capacity exceeded.
First fit is not in the formula booklet. Its output is determined by the input order and bin capacity, not just the total size.
Watch out
Wrong: put the next item into the last opened bin automatically.
Right: restart the search at bin for each item and use the first bin that can hold it.
Change the second item once more.
Sort first, then apply first fit
Large items can be harder to place after small items have used the available gaps. First fit decreasing changes the item order before applying the same bin-selection rule.
First fit decreasing
First sort the items into descending order. Then apply first fit to that sorted list, again trying bins in increasing bin-number order for every item.
This remains a heuristic: it is not guaranteed to use the minimum possible number of bins.
Show the sorted list as evidence of the first stage. Sorting does not turn the second stage into a different rule such as choosing the bin with the smallest remaining gap.
Worked example 3: sorting improves the packing
Question. Apply first fit decreasing to , with capacity .
Plan. Sort to , then follow first fit.
| Next sorted item | Placement | Bin loads afterwards |
|---|---|---|
| Bin | ||
| Bin , because bin cannot fit it | ||
| Bin , which has room for exactly | ||
| Bin , after bin fails |
Why this step? Each smaller item returns to the first suitable earlier bin; it is not automatically paired with the most recent item.
Answer. Two bins, each containing .
Check. The total size is , and two full capacity- bins hold all items. First fit on the unsorted list used three bins.
Change only the second original item's size to , matching the variation in part 1.
Worked example 4: the same two-stage rule
Question. Apply first fit decreasing to , with capacity .
Plan. Sort to , then inspect existing bins in order.
| Next sorted item | Placement | Bin loads afterwards |
|---|---|---|
| Open bin | ||
| Open bin | ||
| Fill bin | ||
| Bin fails; use bin |
Why this step? The sorted order changes the packing, while the first-fit search order remains unchanged.
Answer. Bin : ; bin : .
Check. Loads sum to and stay within capacity.
The rule is not in the formula booklet. A good result on these examples is not a proof that decreasing order always finds an optimum.
Watch out
Wrong: call any descending-order packing first fit decreasing.
Right: sorting is only the first stage; the actual placements must still use the first suitable bin.
Repeat with the next variation.
A full-bin strategy needs a separate optimality argument
A full-bin strategy looks for combinations that fill a bin exactly, or make good use of its space, then considers what remains. It can improve a first-fit result, but is a strategy with choices rather than one prescribed ordering rule. Filling one bin does not guarantee a globally optimal packing.
Capacity lower bound
If total item size is and bin capacity is , any feasible packing uses at least
bins. The ceiling means the smallest integer at least . A feasible packing using bins is optimal.
Not in the formula booklet: learn it.
A lower bound need not be attainable. For example, three items of size , with capacity , have total and capacity bound , but no two can share a bin. They need three bins. The contents matter as well as the total.
Worked example 5: improving both first-fit heuristics
Question. Items have sizes , with capacity . Find an optimal packing using a full-bin strategy, and explain what this shows about first fit decreasing.
Plan. Look for exact totals of , then compare the resulting bin count with a capacity lower bound.
These groups use every item once, so they form two full bins. Also
First fit decreasing processes the already decreasing list and instead gives , , , using three bins.
Why this step? A feasible two-bin construction reaches the lower bound, proving its optimality rather than merely improving one attempted packing.
Answer. Bins and are optimal. First fit decreasing uses three bins here, so it does not guarantee an optimum.
Check. Both bin totals equal , and the three items of size are all accounted for.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account