7.03l, 7.03m - Packing algorithms

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 6,6,4,46,6,4,4 arrive in that order, and each bin holds at most 1010. 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 66 and 44 exactly fill capacity 1010. An item of size 1111 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 6,6,4,46,6,4,4, in that order, into bins of capacity 1010, 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 11 every time.

ItemNext-fit placementFirst-fit placement
first 66bin 11: (6)(6)bin 11: (6)(6)
second 66bin 22: (6)(6)bin 22: (6)(6)
first 44bin 22: (6,4)(6,4)bin 11: (6,4)(6,4)
second 44bin 33: (4)(4)bin 22: (6,4)(6,4)

Why this step? Next-fit cannot reopen bin 11, even though its spare 44 units would hold the next 44. First-fit explicitly checks that bin first.

Answer. Next-fit uses 33 bins: (6)(6), (6,4)(6,4), (4)(4). First-fit uses 22: (6,4)(6,4), (6,4)(6,4).

Check. Every bin total is at most 1010, and the total item size is 2020, so fewer than 22 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 7,6,4,37,6,4,3, with capacity 1010.

Plan. Keep the given order and test each new item against the bins allowed by the chosen rule.

next-fit:(7)(6,4)(3)closed bins cannot be revisitedfirst-fit:(7,3)(6,4)the final 3 returns to bin 1\begin{aligned} \text{next-fit}&:\quad(7)\quad(6,4)\quad(3) && \text{closed bins cannot be revisited}\\ \text{first-fit}&:\quad(7,3)\quad(6,4) && \text{the final }3\text{ returns to bin }1 \end{aligned}

The 44 does not fit after 77, so first-fit puts it with 66. The final 33 fits exactly after 77.

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 33 bins; first-fit uses 22.

Check. The total size is 2020, and both first-fit bins total 1010, 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.]
Items of sizes 6, 6, 4 and 4 need three bins under next-fit but only two under first-fit

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 bb bins each have capacity CC, then bCbC must be at least the total size SS. Thus b≥S/Cb\ge S/C, 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 6,5,3,2,2,26,5,3,2,2,2 into capacity-1010 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

(6,3)(5,2,2)(2)(6,3)\quad(5,2,2)\quad(2)

The first 22 cannot fit after 6+36+3, so it goes with 55; the second joins it, and the third opens a new bin.

For full-bin packing,

bin 1:6+2+2=10a full combination, chosen by the tie rulebin 2:5+3+2=10all remaining itemsb≥6+5+3+2+2+210=2capacity lower bound\begin{aligned} \text{bin }1&:\quad6+2+2=10 && \text{a full combination, chosen by the tie rule}\\ \text{bin }2&:\quad5+3+2=10 && \text{all remaining items}\\ b&\ge\frac{6+5+3+2+2+2}{10}=2 && \text{capacity lower bound} \end{aligned}

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 33 bins; full-bin uses 22. The minimum is 22.

Check. The full-bin packing uses all six items exactly once, and each bin totals 1010.

Worked example 4 changes only capacity, from 1010 to 1212. Now both methods attain the lower bound, although their bin contents differ.

Worked example 4: a changed capacity

Question. Apply the same methods to 6,5,3,2,2,26,5,3,2,2,2 with capacity 1212, 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.

first-fit decreasing:(6,5)(3,2,2,2)totals 11,9full-bin:(6,2,2,2)(5,3)totals 12,8b≥2012=53total size divided by capacityb≥2integer number of bins\begin{aligned} \text{first-fit decreasing}&:\quad(6,5)\quad(3,2,2,2) && \text{totals }11,9\\ \text{full-bin}&:\quad(6,2,2,2)\quad(5,3) && \text{totals }12,8\\ b&\ge\frac{20}{12}=\frac53 && \text{total size divided by capacity}\\ b&\ge2 && \text{integer number of bins} \end{aligned}

Why this step? Rounding 20/1220/12 down would suggest one bin, whose capacity is less than the total size.

Answer. Both methods use 22 bins, which is optimal for this input.

Check. Both sets of totals sum to 2020, and none exceeds 1212.

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 1010, items 6,6,66,6,6 have total size 1818, so the capacity bound is 22. Yet each pair exceeds capacity, forcing 33 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