TExcelTAD
Home › Approximation Algorithms › An Asymptotic PTAS for Bin Packing

An Asymptotic PTAS for Bin Packing

Bin packing has no approximation ratio below 32\frac{3}{2} unless P = NP, because of instances with OPT = 2 (Bin Packing: First Fit and First-Fit Decreasing). For large instances the picture is completely different. Fernandez de la Vega and Lueker (1981) showed that for every ε>0\varepsilon > 0 there is a polynomial algorithm using at most (1+ε) OPT+1(1 + \varepsilon)\,\text{OPT} + 1 bins: an asymptotic PTAS (APTAS). The construction combines three ideas that recur in approximation schemes everywhere: set aside small items and add them greedily at the end, round the large items to few distinct sizes, and solve the rounded instance exactly because it has only a constant number of "types".

Step 1: small items do not matter

Call an item small if its size is at most ε2\frac{\varepsilon}{2}, and large otherwise.

Lemma. Suppose the large items are packed into kk bins. Adding the small items with First Fit (opening new bins only when needed) gives at most max⁡(k, (1+ε) OPT+1)\max\left(k,\ (1 + \varepsilon)\,\text{OPT} + 1\right) bins.

Proof. If no new bin is opened, there are kk bins. Otherwise, when the last bin was opened, every other bin had less than ε2\frac{\varepsilon}{2} free space, so with bb bins in total, S>(1−ε2)(b−1)S > (1 - \frac{\varepsilon}{2})(b - 1), and b<S1−ε/2+1≤(1+ε) OPT+1b < \frac{S}{1 - \varepsilon/2} + 1 \le (1 + \varepsilon)\,\text{OPT} + 1 for ε≤1\varepsilon \le 1. □\square

So it suffices to pack the large items into at most (1+ε) OPT(1 + \varepsilon)\,\text{OPT} bins.

Step 2: linear grouping

Sort the nLn_L large items by decreasing size and cut the list into consecutive groups of gg items each (the last group may be smaller). Round every item up to the largest size in its group. Then:

  • Groups 2,3,…2, 3, \ldots of the rounded instance are dominated by groups 1,2,…1, 2, \ldots of the original: each rounded item of group j+1j + 1 is no larger than any original item of group jj. So the rounded instance without its first group fits in OPT bins: take an optimal packing and put each rounded item of group j+1j + 1 where an item of group jj was.
  • The first group is packed separately, one item per bin: gg extra bins.

Choose g=⌊εSL⌋g = \lfloor \varepsilon S_L \rfloor, where SLS_L is the total size of the large items. (If g=0g = 0, then SL<1εS_L < \frac{1}{\varepsilon} and there are fewer than 2ε2\frac{2}{\varepsilon^2} large items, a constant number that brute force packs optimally.) Then g≤ε OPTg \le \varepsilon\,\text{OPT} and the number of groups is nLg=O(1ε2)\frac{n_L}{g} = O(\frac{1}{\varepsilon^2}), since every large item has size above ε2\frac{\varepsilon}{2}, so nL<2SLεn_L < \frac{2 S_L}{\varepsilon}.

The program performs the grouping on 200 random large items and checks the domination property group by group. It also counts the distinct sizes left after rounding, which is what the exact step has to cope with.

python · try it yourself
import random

rng = random.Random(17)
items = sorted((rng.randint(11, 70) for _ in range(200)), reverse=True)   # sizes in % of a bin
for eps in [0.5, 0.3, 0.2]:
    large = [s for s in items if s > eps * 100 / 2]
    g = max(1, int(eps * sum(large) / 100))
    groups = [large[i:i + g] for i in range(0, len(large), g)]
    rounded = [[grp[0]] * len(grp) for grp in groups]                 # round up to the group's largest
    dominated = all(max(rounded[j + 1]) <= min(groups[j]) for j in range(len(groups) - 1))
    print(f"eps = {eps}: {len(large)} large items, group size {g}, {len(groups)} groups -> "
          f"{len({r[0] for r in rounded[1:]})} distinct sizes, group j+1 rounded <= group j original: {dominated}")
eps = 0.5: 153 large items, group size 36, 5 groups -> 4 distinct sizes, group j+1 rounded <= group j original: True
eps = 0.3: 181 large items, group size 23, 8 groups -> 7 distinct sizes, group j+1 rounded <= group j original: True
eps = 0.2: 200 large items, group size 16, 13 groups -> 12 distinct sizes, group j+1 rounded <= group j original: True

The number of distinct sizes grows like 1ε2\frac{1}{\varepsilon^2}, independent of the number of items: that is what makes the exact step polynomial for fixed ε\varepsilon.

Step 3: few sizes means exact solvability

The rounded instance has d=O(1ε2)d = O(\frac{1}{\varepsilon^2}) distinct sizes, all above ε2\frac{\varepsilon}{2}, so a bin holds fewer than 2ε\frac{2}{\varepsilon} items. A configuration is a multiset of sizes that fits in one bin; there are at most (2ε+1)d(\frac{2}{\varepsilon} + 1)^d of them, a constant for fixed ε\varepsilon. A packing is determined by how many bins use each configuration, at most nLn_L each, so trying all of them, or a dynamic program over the vector of remaining counts per size, takes time nO(const)n^{O(\text{const})}: polynomial for fixed ε\varepsilon, though with a horrible constant.

Theorem (Fernandez de la Vega and Lueker, 1981). For every fixed ε∈(0,1]\varepsilon \in (0, 1], the algorithm uses at most (1+ε) OPT+1(1 + \varepsilon)\,\text{OPT} + 1 bins and runs in polynomial time.

Proof. The large items use at most OPT+g≤(1+ε) OPT\text{OPT} + g \le (1 + \varepsilon)\,\text{OPT} bins (step 2, with an exact packing in step 3), and step 1 adds the small ones. □\square

python · try it yourself
import random
from functools import lru_cache

CAP = 100

def first_fit_into(rooms, items):
    rooms = list(rooms)
    for s in items:
        for i, r in enumerate(rooms):
            if s <= r:
                rooms[i] -= s
                break
        else:
            rooms.append(CAP - s)
    return rooms

def exact_rounded(sizes, counts):
    """Fewest bins for counts[i] items of size sizes[i]; returns the free room of each bin."""
    configs = []
    def gen(i, room, cfg):
        if i == len(sizes):
            if any(cfg):
                configs.append((tuple(cfg), room))
            return
        for c in range(room // sizes[i] + 1):
            gen(i + 1, room - c * sizes[i], cfg + [c])
    gen(0, CAP, [])
    @lru_cache(None)
    def best(state):
        if not any(state):
            return 0, ()
        result = None
        for cfg, room in configs:
            if all(c <= s for c, s in zip(cfg, state)):
                rest = tuple(s - c for c, s in zip(cfg, state))
                k, rooms = best(rest)
                if result is None or k + 1 < result[0]:
                    result = (k + 1, rooms + (room,))
        return result
    return best(tuple(counts))[1], len(configs)

def aptas(items, eps):
    small = [s for s in items if s <= eps * CAP / 2]
    large = sorted((s for s in items if s > eps * CAP / 2), reverse=True)
    g = max(1, int(eps * sum(large) / CAP))
    groups = [large[i:i + g] for i in range(0, len(large), g)]
    rooms = [CAP - s for s in groups[0]]                  # first group: one item per bin
    sizes = [grp[0] for grp in groups[1:]]                # round up to the group's largest
    counts = [len(grp) for grp in groups[1:]]
    packed, n_configs = exact_rounded(sizes, counts) if sizes else ((), 0)
    rooms += list(packed)
    rooms = first_fit_into(rooms, sorted(small, reverse=True))
    return len(rooms), len(groups), n_configs

def ffd(items):
    return len(first_fit_into([], sorted(items, reverse=True)))

rng = random.Random(23)
items = [rng.choice([rng.randint(3, 12), rng.randint(26, 60)]) for _ in range(40)]
lb = -(-sum(items) // CAP)
print(f"{len(items)} items, total size {sum(items)}, lower bound {lb} bins, FFD {ffd(items)} bins, so OPT = {lb}")
for eps in [0.8, 0.6, 0.4]:
    bins, groups, n_configs = aptas(items, eps)
    print(f"eps = {eps}: {groups} groups, {n_configs:3} configurations, {bins} bins <= (1 + eps) OPT + 1 = {(1 + eps) * lb + 1:.1f}")
40 items, total size 1009, lower bound 11 bins, FFD 11 bins, so OPT = 11
eps = 0.8: 4 groups,   8 configurations, 11 bins <= (1 + eps) OPT + 1 = 20.8
eps = 0.6: 5 groups,  16 configurations, 11 bins <= (1 + eps) OPT + 1 = 18.6
eps = 0.4: 7 groups,  29 configurations, 12 bins <= (1 + eps) OPT + 1 = 16.4

On this instance FFD already meets the lower bound, so the scheme cannot beat it, and with ε=0.4\varepsilon = 0.4 it even uses one bin more, which its guarantee allows: rounding up wastes space that FFD uses. What the program shows is the mechanism. A smaller ε\varepsilon means more groups and more configurations, and the cost of the exact step grows quickly. Asymptotic schemes are theorems about what is possible, and practical solvers use the configuration idea through linear programming instead.

In C++

The exact step on its own: a dynamic program over the vector of remaining counts, for three rounded sizes.

cpp · try it yourself
#include <cstdio>
#include <map>
#include <vector>

std::vector<int> sizes = {45, 34, 27};
std::vector<std::vector<int>> configs;
std::map<std::vector<int>, int> memo;

void gen(size_t i, int room, std::vector<int>& cfg) {
    if (i == sizes.size()) {
        for (int c : cfg) if (c) { configs.push_back(cfg); break; }
        return;
    }
    for (int c = 0; c * sizes[i] <= room; c++) {
        cfg.push_back(c);
        gen(i + 1, room - c * sizes[i], cfg);
        cfg.pop_back();
    }
}

int best(const std::vector<int>& state) {
    bool empty = true;
    for (int s : state) if (s) empty = false;
    if (empty) return 0;
    auto it = memo.find(state);
    if (it != memo.end()) return it->second;
    int result = 1 << 30;
    for (auto& cfg : configs) {
        std::vector<int> rest = state;
        bool ok = true;
        for (size_t i = 0; i < rest.size(); i++) if ((rest[i] -= cfg[i]) < 0) ok = false;
        if (ok) result = std::min(result, 1 + best(rest));
    }
    return memo[state] = result;
}

int main() {
    std::vector<int> cfg;
    gen(0, 100, cfg);
    std::printf("%zu configurations fit in a bin of size 100\n", configs.size());
    std::vector<int> counts = {7, 9, 11};
    std::printf("7 x 45, 9 x 34, 11 x 27: %d bins (total size %d)\n", best(counts), 7 * 45 + 9 * 34 + 11 * 27);
    std::printf("states explored: %zu\n", memo.size());
}
13 configurations fit in a bin of size 100
7 x 45, 9 x 34, 11 x 27: 10 bins (total size 918)
states explored: 959

The configuration LP and additive guarantees

Instead of enumerating packings, write one variable xC≥0x_C \ge 0 per configuration CC and require that every size is covered: min⁡∑CxC\min \sum_C x_C subject to ∑C(copies of size i in C) xC≥ni\sum_C (\text{copies of size } i \text{ in } C)\, x_C \ge n_i for each size ii. This LP, of Gilmore and Gomory (1961) for cutting stock, has exponentially many variables but can be solved approximately with a knapsack separation oracle for its dual (Separation Oracles and Exponentially Many Constraints). A basic solution has at most dd fractional variables; rounding them up costs at most dd extra bins.

  • Karmarkar and Karp (1982) combined the configuration LP with a smarter geometric grouping and obtained OPT+O(log⁡2OPT)\text{OPT} + O(\log^2 \text{OPT}) bins, an asymptotic FPTAS.
  • Hoberg and Rothvoß (2017) improved this to OPT+O(log⁡OPT)\text{OPT} + O(\log \text{OPT}).
  • Whether OPT+1\text{OPT} + 1 is achievable in polynomial time is open; the configuration LP is conjectured to have an additive integrality gap of at most 1 (the modified integer round-up property).

Test yourself

Why can small items (size at most ε2\frac{\varepsilon}{2}) be added greedily at the end?

  1. Because they always fit in one bin
  2. If First Fit opens a new bin for them, all other bins are more than 1−ε21 - \frac{\varepsilon}{2} full, so the total is at most about (1+ε) OPT+1(1 + \varepsilon)\,\text{OPT} + 1
  3. Because they are packed optimally by the DP
  4. Because there are few of them

In linear grouping, why does the rounded instance without its first group fit into OPT bins?

  1. Because rounding makes items smaller
  2. Each rounded item of group j+1j + 1 is no larger than every original item of group jj, so it can take that item's place in an optimal packing
  3. Because the first group is the smallest
  4. Because of First Fit

Why is the rounded instance solvable exactly in polynomial time for fixed ε\varepsilon?

  1. Bin packing is in P
  2. It has O(1ε2)O(\frac{1}{\varepsilon^2}) sizes and fewer than 2ε\frac{2}{\varepsilon} items per bin, so the number of configurations is a constant and packings are count vectors over them
  3. Because all items are equal
  4. Because the LP is integral