Documentation

CslibFrontier.Algorithms.Lean.BinPacking.FirstFit

First Fit #

First Fit keeps every bin open. It places each item into the first bin with enough room, opening a new last bin when no bin has room. So every item of a bin did not fit into any earlier bin, even into that bin's final contents (Cslib.Algorithms.Lean.BinPacking.pairwise_forall_lt_sum_add_firstFit), and as no bin is empty (Cslib.Algorithms.Lean.BinPacking.ne_nil_of_mem_firstFit), any two bins it returns are jointly overfull (Cslib.Algorithms.Lean.BinPacking.pairwise_firstFit). So First Fit uses fewer than twice as many bins as any packing (Cslib.Algorithms.Lean.BinPacking.length_firstFit_lt), that is at most 2 * OPT - 1 bins, a slight sharpening of the bound 2 * OPT + 1 of Williamson and Shmoys, Section 3.3 by the same pairing argument. Johnson's thesis Johnson analyses Next Fit and First Fit; by the notes to Chapter 3 of Williamson and Shmoys, their First Fit argument uses the thesis's analysis of Next Fit. The asymptotic bound 17/10 * OPT + 2 of Johnson, Demers, Ullman, Garey and Graham, sharpened to 17/10 * OPT + 4/5, is in CslibFrontier.Algorithms.Lean.BinPacking.FirstFitWeight.

Main definitions #

Main results #

References #

Insert the item a into the first bin with enough room, or into a new last bin if no bin has room.

First Fit with bin capacity C.

Inserting an item adds it to the bins' contents.

theorem Cslib.Algorithms.Lean.BinPacking.mem_firstFitInsert {C a : ℕ} {bins : List (List ℕ)} {c : List ℕ} :
c ∈ firstFitInsert C a bins → c ∈ bins ∨ (∃ b ∈ bins, b.sum + a ≤ C ∧ c = b ++ [a]) ∨ c = [a]

A bin after inserting a is an old bin, an old bin with room for a extended by a, or the new bin [a].

First Fit's bins hold exactly the items.

theorem Cslib.Algorithms.Lean.BinPacking.pairwise_forall_lt_sum_add_firstFit (C : ℕ) (l : List ℕ) :
List.Pairwise (fun (b b' : List ℕ) => ∀ c ∈ b', C < b.sum + c) (firstFit C l)

The First Fit rule: every item of a bin opened later did not fit into an earlier bin, even into that bin's final contents.

First Fit opens no empty bin.

Any two bins of First Fit are jointly overfull.

theorem Cslib.Algorithms.Lean.BinPacking.isPacking_firstFit {C : ℕ} {l : List ℕ} (hl : ∀ a ∈ l, a ≤ C) :

First Fit returns a packing whenever every item fits into a bin.

theorem Cslib.Algorithms.Lean.BinPacking.length_firstFit_lt {C : ℕ} {l : List ℕ} {Q : List (List ℕ)} (hQ : IsPacking C l Q) (hl : l ≠ []) :

First Fit is a 2-approximation: on a nonempty list of items it uses fewer than twice as many bins as any packing, that is at most 2 * OPT - 1 bins.