Documentation

CslibFrontier.Algorithms.Lean.Knapsack.ProfitDP

The knapsack dynamic program by profit #

Items of a type ι have weights w : ι → ℕ and profits p : ι → ℕ. A selection is a subsequence of the list of items; it is feasible for a capacity B when its total weight is at most B. The dynamic program by profit computes, for every profit q up to the total profit, a minimum-weight subsequence of profit exactly q (Cslib.Algorithms.Lean.Knapsack.profitTable), and then returns a most profitable feasible entry (Cslib.Algorithms.Lean.Knapsack.profitDP). The table has (l.map p).sum + 1 entries, so the program is pseudo-polynomial: polynomial in the number of items and the total profit Williamson and Shmoys, Section 3.1. Williamson and Shmoys's Algorithm 3.1 keeps the non-dominated (weight, profit) pairs instead; indexing by profit gives the same optimum with a simpler invariant.

Adding an item a maps the table T to the table whose entry q is the lighter of T[q] and T[q - p a] extended by a (Cslib.Algorithms.Lean.Knapsack.profitStep). By induction on the items, every entry of profit q is a subsequence of profit q, and for every subsequence s the entry at profit p(s) weighs at most w(s) (Cslib.Algorithms.Lean.Knapsack.TableSpec).

Main definitions #

Main results #

References #

The dynamic program #

def Cslib.Algorithms.Lean.Knapsack.lighter {ι : Type u_1} (w : ι → ℕ) (x y : Option (List ι)) :

The lighter of two optional selections, preferring the first on ties.

def Cslib.Algorithms.Lean.Knapsack.profitStep {ι : Type u_1} (w p : ι → ℕ) (T : List (Option (List ι))) (a : ι) :

One step of the dynamic program by profit: entry q of the new table is the lighter of the old entry q and the old entry q - p a extended by a.

def Cslib.Algorithms.Lean.Knapsack.profitTable {ι : Type u_1} (w p : ι → ℕ) (l : List ι) :

The profit table of l: entry q is a minimum-weight subsequence of l of profit q, or none if there is none.

def Cslib.Algorithms.Lean.Knapsack.selectStep {ι : Type u_1} (w p : ι → ℕ) (B : ℕ) (best : List ι) (x : Option (List ι)) :
List ι

One step of the selection scan: replace the current choice by an entry within the capacity of at least its profit.

def Cslib.Algorithms.Lean.Knapsack.profitDP {ι : Type u_1} (w p : ι → ℕ) (B : ℕ) (l : List ι) :
List ι

The dynamic program by profit with capacity B.

Correctness #

theorem Cslib.Algorithms.Lean.Knapsack.lighter_eq_some {ι : Type u_1} {w : ι → ℕ} {x y : Option (List ι)} {s : List ι} (h : lighter w x y = some s) :
x = some s ∨ y = some s

The lighter of two selections is one of them.

theorem Cslib.Algorithms.Lean.Knapsack.exists_lighter_left {ι : Type u_1} {w : ι → ℕ} (s : List ι) (y : Option (List ι)) :
∃ (r : List ι), lighter w (some s) y = some r ∧ (List.map w r).sum ≤ (List.map w s).sum

The lighter of a selection and anything weighs at most the selection.

theorem Cslib.Algorithms.Lean.Knapsack.exists_lighter_right {ι : Type u_1} {w : ι → ℕ} (x : Option (List ι)) (t : List ι) :
∃ (r : List ι), lighter w x (some t) = some r ∧ (List.map w r).sum ≤ (List.map w t).sum

The lighter of anything and a selection weighs at most the selection.

theorem Cslib.Algorithms.Lean.Knapsack.getElem?_profitStep {ι : Type u_1} {w p : ι → ℕ} (T : List (Option (List ι))) (a : ι) (q : ℕ) :
(profitStep w p T a)[q]? = if q < T.length + p a then some (lighter w (T.getD q none) (if p a ≤ q then Option.map (fun (x : List ι) => x ++ [a]) (T.getD (q - p a) none) else none)) else none

The entries of one step of the dynamic program.

structure Cslib.Algorithms.Lean.Knapsack.TableSpec {ι : Type u_1} (w p : ι → ℕ) (l : List ι) (T : List (Option (List ι))) :

The invariant of the profit table of l: its length, and soundness and completeness of its entries.

The initial table holds the empty selection at profit 0.

theorem Cslib.Algorithms.Lean.Knapsack.TableSpec.step {ι : Type u_1} {w p : ι → ℕ} {l : List ι} {T : List (Option (List ι))} (hT : TableSpec w p l T) (a : ι) :
TableSpec w p (l ++ [a]) (profitStep w p T a)

One step of the dynamic program preserves the invariant.

theorem Cslib.Algorithms.Lean.Knapsack.TableSpec.foldl {ι : Type u_1} {w p : ι → ℕ} {l₀ : List ι} {T : List (Option (List ι))} (hT : TableSpec w p l₀ T) (l : List ι) :
TableSpec w p (l₀ ++ l) (List.foldl (profitStep w p) T l)

Folding the dynamic program over more items preserves the invariant.

theorem Cslib.Algorithms.Lean.Knapsack.tableSpec_profitTable {ι : Type u_1} {w p : ι → ℕ} (l : List ι) :
TableSpec w p l (profitTable w p l)

Correctness of the profit table.

theorem Cslib.Algorithms.Lean.Knapsack.foldl_selectStep_sublist {ι : Type u_1} {w p : ι → ℕ} {B : ℕ} {l : List ι} {L : List (Option (List ι))} (hL : ∀ (s : List ι), some s ∈ L → s.Sublist l) {best : List ι} (hbest : best.Sublist l) :
(List.foldl (selectStep w p B) best L).Sublist l

The selection scan over the profit table stays a subsequence of the items.

theorem Cslib.Algorithms.Lean.Knapsack.sublist_of_mem_profitTable {ι : Type u_1} {w p : ι → ℕ} {l s : List ι} (hs : some s ∈ profitTable w p l) :

Every entry of the profit table is a subsequence of the items.

theorem Cslib.Algorithms.Lean.Knapsack.profitDP_sublist {ι : Type u_1} {w p : ι → ℕ} (B : ℕ) (l : List ι) :
(profitDP w p B l).Sublist l

The dynamic program returns a subsequence of the items.

theorem Cslib.Algorithms.Lean.Knapsack.sum_map_profitDP_le {ι : Type u_1} {w p : ι → ℕ} (B : ℕ) (l : List ι) :
(List.map w (profitDP w p B l)).sum ≤ B

The dynamic program respects the capacity.

theorem Cslib.Algorithms.Lean.Knapsack.sum_map_le_profitDP {ι : Type u_1} {w p : ι → ℕ} {B : ℕ} {l s : List ι} (hs : s.Sublist l) (hsw : (List.map w s).sum ≤ B) :
(List.map p s).sum ≤ (List.map p (profitDP w p B l)).sum

The dynamic program is optimal: its profit is at least that of every subsequence of the items within the capacity.