Documentation

CslibFrontier.Algorithms.Lean.Knapsack.FPTAS

The knapsack FPTAS #

An instance of the knapsack problem is a list of items (weight, value) and a capacity B; a feasible solution is a subsequence of the items of total weight at most B, the notion of Cslib.Complexity.Knapsack (Cslib.Complexity.mem_Knapsack_iff_sublist). The FPTAS with accuracy 1 / k (Cslib.Algorithms.Lean.Knapsack.fptas) discards the items heavier than B, rounds the value v of each of the n remaining items to ⌊v * n * k / P⌋, where P is their largest value, and solves the rounded instance exactly with the dynamic program by profit (Cslib.Algorithms.Lean.Knapsack.profitDP). The rounded values are at most n * k, so the dynamic program runs in time polynomial in n and k (Cslib.Algorithms.Lean.Knapsack.computableInPolyTime_fptas).

Rounding loses less than P / (n * k) per item, so less than P / k in total, and the most valuable fitting item alone is feasible, so the returned value is at least (1 - 1 / k) times the value of every feasible solution (Cslib.Algorithms.Lean.Knapsack.sub_one_mul_value_le_fptas) Williamson and Shmoys, Algorithm 3.2 and Theorem 3.5, Ibarra and Kim. Williamson and Shmoys round with μ = ε P / n and assume that every item fits; discarding the items that do not fit removes that assumption.

Main definitions #

Main results #

References #

The largest value of an item, 0 for no items.

theorem Cslib.Algorithms.Lean.Knapsack.foldl_max_eq (l : List (ℕ × ℕ)) (m : ℕ) :
List.foldl (fun (m : ℕ) (a : ℕ × ℕ) => max m a.2) m l = m ∨ ∃ a ∈ l, a.2 = List.foldl (fun (m : ℕ) (a : ℕ × ℕ) => max m a.2) m l

The running maximum is its start or the value of an item.

Every value is at most the largest value.

theorem Cslib.Algorithms.Lean.Knapsack.exists_maxValue_eq {l : List (ℕ × ℕ)} (hl : l ≠ []) :
∃ a ∈ l, a.2 = maxValue l

The largest value of a nonempty list is attained.

The items that fit into a knapsack of capacity B by themselves.

Pair the items with their values rounded to min N ⌊v * N / P⌋, which is ⌊v * N / P⌋ whenever v ≤ P.

The knapsack FPTAS with accuracy 1 / k, on the items x.1 and the capacity x.2: discard the items heavier than the capacity, round the values of the n others to ⌊v * n * k / P⌋ with P their largest value, and solve the rounded instance exactly with the dynamic program by profit.

The FPTAS returns a subsequence of the items.

The FPTAS respects the capacity.

theorem Cslib.Algorithms.Lean.Knapsack.sublist_fits {B : ℕ} {l s : List (ℕ × ℕ)} (hs : s.Sublist l) (hsw : (List.map Prod.fst s).sum ≤ B) :
s.Sublist (fits B l)

A subsequence within the capacity consists of items that fit by themselves.

The rounding error of the FPTAS: k times the value of a feasible solution is at most k times the value returned plus the largest value of a fitting item.

The FPTAS guarantee Williamson and Shmoys, Theorem 3.5: with accuracy 1 / k, the FPTAS returns at least 1 - 1 / k times the value of every feasible solution.

theorem Cslib.Algorithms.Lean.Knapsack.one_sub_mul_value_le_fptas {ε : ℝ} (hε : 0 < ε) {x : List (ℕ × ℕ) × ℕ} {s : List (ℕ × ℕ)} (hs : s.Sublist x.1) (hsw : (List.map Prod.fst s).sum ≤ x.2) :

The knapsack FPTAS Williamson and Shmoys, Theorem 3.5, Ibarra and Kim: for every real ε > 0, with k = ⌈1 / ε⌉, the FPTAS returns a feasible solution whose value is at least 1 - ε times that of every feasible solution.