Documentation

CslibFrontier.Algorithms.Lean.BinPacking.Basic

Bin packing #

An instance of bin packing is a list l : List ℕ of item sizes and a bin capacity C : ℕ. A packing (Cslib.Algorithms.Lean.BinPacking.IsPacking) is a list of bins, each a list of items, that together hold exactly the items of l, counted with multiplicity, and each hold at most C. The number of bins is the length of the packing, and an optimal packing minimizes it. This is the problem of Williamson and Shmoys, Section 3.3 with the sizes aᵢ ∈ (0, 1] scaled to integers aᵢ ≤ C.

Every packing uses at least l.sum / C bins (IsPacking.sum_le_mul_length). Next Fit and First Fit both return packings in which every two consecutive bins are jointly overfull: their contents do not fit together into one bin. Pairing consecutive bins, such a packing with m bins holds more than C * ⌊m / 2⌋ (add_one_mul_div_two_le_of_isChain), so it uses fewer than twice as many bins as any packing of the same items (length_lt_two_mul_of_isChain).

Main definitions #

Main results #

References #

bins packs the items l into bins of capacity C: together the bins hold exactly the items of l, counted with multiplicity, and each bin holds at most C.

theorem Cslib.Algorithms.Lean.BinPacking.IsPacking.sum_map_eq {C : ℕ} {l : List ℕ} {bins : List (List ℕ)} {M : Type u_1} [AddCommMonoid M] (h : IsPacking C l bins) (f : ℕ → M) :
(List.map f l).sum = (List.map (fun (b : List ℕ) => (List.map f b).sum) bins).sum

The total weight of the items is the sum of the weights of the bins.

theorem Cslib.Algorithms.Lean.BinPacking.IsPacking.sum_eq {C : ℕ} {l : List ℕ} {bins : List (List ℕ)} (h : IsPacking C l bins) :

The items' total size is the sum of the bins' loads.

Volume bound: the items' total size is at most C times the number of bins.

theorem Cslib.Algorithms.Lean.BinPacking.IsPacking.sum_map_le_mul_length {C : ℕ} {l : List ℕ} {bins : List (List ℕ)} (h : IsPacking C l bins) (f : ℕ → ℕ) {K : ℕ} (hK : ∀ (b : List ℕ), b.sum ≤ C → (List.map f b).sum ≤ K) :
(List.map f l).sum ≤ K * bins.length

Weighted volume bound: if every list of items of total size at most C has total weight at most K under a weight f, then the items of a packing have total weight at most K times the number of bins.

theorem Cslib.Algorithms.Lean.BinPacking.IsPacking.count_le_mul_length {C : ℕ} {l : List ℕ} {bins : List (List ℕ)} (h : IsPacking C l bins) (x : ℕ) {k : ℕ} (hk : ∀ (b : List ℕ), b.sum ≤ C → List.count x b ≤ k) :
List.count x l ≤ k * bins.length

Counting bound: if every list of items of total size at most C holds at most k copies of an item x, then a packing holds at most k copies of x per bin.

theorem Cslib.Algorithms.Lean.BinPacking.IsPacking.le_of_mem {C : ℕ} {l : List ℕ} {bins : List (List ℕ)} (h : IsPacking C l bins) {a : ℕ} (ha : a ∈ l) :
a ≤ C

Every packed item fits into a bin.

theorem Cslib.Algorithms.Lean.BinPacking.IsPacking.ne_nil {C : ℕ} {l : List ℕ} {bins : List (List ℕ)} (h : IsPacking C l bins) (hl : l ≠ []) :
bins ≠ []

A packing of a nonempty list has a bin.

theorem Cslib.Algorithms.Lean.BinPacking.isPacking_map_singleton {C : ℕ} {l : List ℕ} (hl : ∀ a ∈ l, a ≤ C) :
IsPacking C l (List.map (fun (a : ℕ) => [a]) l)

One bin for each item packs a list of items exactly when every item fits into a bin.

theorem Cslib.Algorithms.Lean.BinPacking.exists_isPacking_iff {C : ℕ} {l : List ℕ} :
(∃ (bins : List (List ℕ)), IsPacking C l bins) ↔ ∀ a ∈ l, a ≤ C

A packing exists exactly when every item fits into a bin.

theorem Cslib.Algorithms.Lean.BinPacking.add_one_mul_div_two_le_of_isChain {C : ℕ} {bins : List (List ℕ)} :
List.IsChain (fun (b b' : List ℕ) => C < b.sum + b'.sum) bins → (C + 1) * (bins.length / 2) ≤ (List.map List.sum bins).sum

Pairing consecutive bins: if every two consecutive bins are jointly overfull, then the bins hold more than C for each pair, so (C + 1) * ⌊m / 2⌋ is at most their total load.

theorem Cslib.Algorithms.Lean.BinPacking.length_lt_two_mul_of_isChain {C : ℕ} {l : List ℕ} {bins Q : List (List ℕ)} (hperm : bins.flatten.Perm l) (hchain : List.IsChain (fun (b b' : List ℕ) => C < b.sum + b'.sum) bins) (hQ : IsPacking C l Q) (hl : l ≠ []) :
bins.length < 2 * Q.length

A packing of a nonempty list whose consecutive bins are jointly overfull uses fewer than twice as many bins as any packing of the same items, that is at most 2 * OPT - 1 bins.