7.03l, 7.03m - Packing algorithms
A packing rule must decide where each item goes, while respecting a capacity. You will use next-fit, first-fit, first-fit decreasing and full-bin packing, distinguish online from offline decisions, and extend the ideas to dimensions and knapsack problems where mass and profit compete.
Next-fit and first-fit packing
Items of sizes arrive in that order, and each bin holds at most . Can a simple packing rule use three bins when two would do? Yes. The difference is whether the rule is allowed to return to a bin it has already passed.
You already know
A greedy rule chooses what looks best at the current step. A heuristic can find a useful feasible answer without guaranteeing an optimum. The order in which a sorting or packing algorithm processes entries is part of its instructions.
In one-dimensional packing, each item has one relevant size, such as mass, volume or length. An item must fit wholly in one bin; bins have equal capacity unless specified otherwise. For example, two items of sizes and exactly fill capacity . An item of size cannot be packed in such a bin at all.
Next-fit and first-fit
Next-fit: consider items in their given order. Put the next item in the current bin if it fits. Otherwise close that bin, open a new one and place it there. Never return to a closed bin.
First-fit: consider items in their given order. For each item, inspect existing bins in the order they were opened and place it in the first bin with enough space. Open a new bin only if none can hold it.
These packing methods are not in the formula booklet: learn the rules. “Fits” includes equality with the remaining capacity. Neither method sorts the input first.
Worked example 1: the price of closing a bin
Question. Pack , in that order, into bins of capacity , using next-fit and first-fit. State each bin's contents.
Plan. Track the remaining space after each item; for first-fit, restart the search at bin every time.
| Item | Next-fit placement | First-fit placement |
|---|---|---|
| first | bin : | bin : |
| second | bin : | bin : |
| first | bin : | bin : |
| second | bin : | bin : |
Why this step? Next-fit cannot reopen bin , even though its spare units would hold the next . First-fit explicitly checks that bin first.
Answer. Next-fit uses bins: , , . First-fit uses : , .
Check. Every bin total is at most , and the total item size is , so fewer than bins is impossible. First-fit is optimal for this input.
Worked example 2 changes the item sizes while retaining the pattern of two large items followed by their complements. The first-fit search must still start from the earliest bin.
Worked example 2: two different complements
Question. Use both methods on , with capacity .
Plan. Keep the given order and test each new item against the bins allowed by the chosen rule.
The does not fit after , so first-fit puts it with . The final fits exactly after .
Why this step? First-fit means the first bin that fits, not the bin most recently used and not the bin that would leave the least spare space.
Answer. Next-fit uses bins; first-fit uses .
Check. The total size is , and both first-fit bins total , proving that two bins achieve the lower bound.
The opening example is shown below. Empty space remains usable in first-fit even after a later bin has been opened.
[DIAGRAM: asset_name: Next-fit versus first-fit; asset_slug: h245_7_03_next_first_fit; caption: Items of sizes 6, 6, 4 and 4 need three bins under next-fit but only two under first-fit; description: Two panels show horizontal bins of capacity ten on a common scale. Next-fit has bin one containing six with four empty, bin two containing six and four, and bin three containing four with six empty. First-fit has two full bins, each containing six and four. Item sizes are labelled inside pale blue and sage segments, with empty space neutral.]

Watch out
Wrong: put an item in a later bin because it fills that bin exactly. Right: first-fit tests bins in opening order and takes the first feasible one, even if a later bin would be a tighter fit.
For the next input, write the contents as well as the bin count so that the rule is visible.
Sorting first and filling a bin
Knowing all the items in advance lets you change the order or search for combinations that fill a bin well.
First-fit decreasing and full-bin packing
First-fit decreasing: sort the items into decreasing order, then apply first-fit to that sorted list.
Full-bin packing: look across the remaining items for a combination that fills a bin exactly, if possible; otherwise choose a combination with the largest total not exceeding capacity. Remove those items and repeat. If several combinations tie, use the tie rule stated in the question, or show which valid choice you make.
These methods are not in the formula booklet: learn them. For a deterministic implementation of full-bin packing, the tie rule must be specified. In the examples here, choose a tied combination with the fewest items, then prefer the one containing the larger item at the first difference when its items are written in decreasing order.
The total size also gives a lower bound. If bins each have capacity , then must be at least the total size . Thus , and the bin count must be rounded up to an integer. This capacity bound is not in the formula booklet; derive it from total capacity.
Worked example 3: decreasing order can still miss the optimum
Question. Pack into capacity- bins using first-fit decreasing and full-bin packing, with the tie rule above. Determine the minimum possible bin count.
Plan. Follow each rule, then compare its bin count with the total-size lower bound.
The list is already decreasing. First-fit decreasing produces
The first cannot fit after , so it goes with ; the second joins it, and the third opens a new bin.
For full-bin packing,
Why this step? A feasible two-bin packing and a lower bound of two together prove optimality; the rule's name alone does not.
Answer. First-fit decreasing uses bins; full-bin uses . The minimum is .
Check. The full-bin packing uses all six items exactly once, and each bin totals .
Worked example 4 changes only capacity, from to . Now both methods attain the lower bound, although their bin contents differ.
Worked example 4: a changed capacity
Question. Apply the same methods to with capacity , retaining the full-bin tie rule. Determine the minimum bin count.
Plan. Recompute every fit using the new capacity, then round the capacity bound upwards.
Why this step? Rounding down would suggest one bin, whose capacity is less than the total size.
Answer. Both methods use bins, which is optimal for this input.
Check. Both sets of totals sum to , and none exceeds .
Finding the best fill for the current bin can require examining many combinations. Even when that search is exact for one bin, filling it first need not minimise the total number of bins over every possible input.
Watch out
Wrong: the capacity lower bound is automatically achievable. Right: it is only a lower bound until you exhibit a packing that reaches it. Item sizes may prevent all the spare space from being combined usefully.
A fresh list exposes the same difference between a good decreasing-order rule and a proof of optimality.
Online decisions and heuristic guarantees
A packing rule's information matters as much as its choice of bin.
Suppose sealed parcels arrive one at a time and must be assigned immediately. First-fit can act now: it needs the current parcel and the spaces already available. First-fit decreasing cannot perform its first sorting step until it has the whole list. You cannot know which parcel is largest while larger ones may still arrive.
Online and offline packing
An online method processes each arriving item without knowing future items and makes its placement before the next arrives. An offline method has the complete item list available before making its packing decisions.
Next-fit and first-fit can be used online. First-fit decreasing is offline; full-bin searches across the remaining collection and is also offline. Using first-fit on a list known in advance is possible, but the rule itself does not require future information. “Online” concerns available information, not whether a computer has an internet connection.
All four named rules are heuristics for minimising bin count: none guarantees an optimal packing on every input. One success is not a proof of a general guarantee; one counterexample is enough to disprove such a guarantee. For example, with capacity , items have total size , so the capacity bound is . Yet each pair exceeds capacity, forcing bins even with complete information. The online/offline meanings and heuristic status are not printed in the formula booklet: learn them.
Key idea
Separate feasibility, the result produced by the named rule, and a proof of optimality. They are three different claims.
The following claim confuses a lower bound with a feasible construction.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account