Documentation

CslibFrontier.Algorithms.Lean.OptimalBST.Knuth

Optimal binary search trees and Knuth's window algorithm #

The dynamic program for optimal binary search trees fills the table of intervals (i, i + d] by increasing length d. Each entry scans a window lo, …, hi of candidate roots k and keeps the largest one minimizing T(i, k - 1) + T(k, j) (cell). The cubic algorithm (plainBST) scans every root i + 1, …, j. Knuth's algorithm (knuthBST) scans only root(i, j - 1), …, root(i + 1, j), the window allowed by the monotonicity of optimal roots.

Both algorithms return the optimal cost optCost w 0 n and the tree optTree w 0 n read off the largest optimal roots: the cubic one for every weight (plainBST_eq), and Knuth's when the weight satisfies the quadrangle inequality and is monotone on intervals (knuthBST_eq). Along each diagonal d + 1, Knuth's windows telescope to at most 2 n candidate roots (sum_knuth_window_le). Without the quadrangle and monotonicity hypotheses Knuth's algorithm can return a suboptimal cost; see CslibFrontierTests.OptimalBST. Checked Word-RAM O(n³) and O(n²) running-time bounds are proved in CslibFrontier.Algorithms.WordRAM.OptimalBST.

Main definitions #

Main results #

References #

Tables and windows #

A table entry: a cost and a root.

  • cost : β

    The cost of the interval.

  • root : ℕ

    The chosen root.

theorem Cslib.Algorithms.Lean.OptimalBST.Entry.ext {β : Type u_2} {x y : Entry β} (cost : x.cost = y.cost) (root : x.root = y.root) :
x = y
def Cslib.Algorithms.Lean.OptimalBST.instDecidableEqEntry.decEq {β✝ : Type u_2} [DecidableEq β✝] (x✝ x✝¹ : Entry β✝) :
Decidable (x✝ = x✝¹)
@[instance_reducible]
@[reducible, inline]

A dynamic-programming table, indexed by the endpoints i, j of the key interval i + 1, …, j.

A rule choosing the window lo, …, hi of candidate roots for (i, j] from the table.

Every root i + 1, …, j (the cubic dynamic program).

Knuth's window root(i, j - 1), …, root(i + 1, j), or the only root j when j = i + 1.

def Cslib.Algorithms.Lean.OptimalBST.setCell {β : Type u_1} (d : ℕ) (T : Table β) (i : ℕ) (e : Entry β) :

Overwrite the entry of the interval (i, i + d].

theorem Cslib.Algorithms.Lean.OptimalBST.foldl_setCell {β : Type u_1} (d : ℕ) (F : ℕ → Entry β) (l : List ℕ) (U : Table β) (a b : ℕ) :
List.foldl (fun (U : Table β) (i : ℕ) => setCell d U i (F i)) U l a b = if a ∈ l ∧ b = a + d then F a else U a b

The output of an optimal-BST algorithm: a cost and a tree.

  • cost : β

    The optimal cost.

  • A tree attaining it.

The scan of a window #

def Cslib.Algorithms.Lean.OptimalBST.bestStep {β : Type u_1} [LinearOrder β] (c : ℕ → β) (best : Entry β) (k : ℕ) :

Keep the cheaper of the current best entry and candidate k, preferring k on ties.

def Cslib.Algorithms.Lean.OptimalBST.scan {β : Type u_1} [LinearOrder β] (c : ℕ → β) (lo hi : ℕ) :

Scan the candidates lo, …, hi for a largest minimizer of c.

def Cslib.Algorithms.Lean.OptimalBST.ScanInv {β : Type u_1} [LinearOrder β] (c : ℕ → β) (lo hi : ℕ) (e : Entry β) :

Invariant satisfied by scan c lo hi when lo ≤ hi: the returned entry is the largest minimizer of c on [lo, hi].

theorem Cslib.Algorithms.Lean.OptimalBST.scanInv_foldl {β : Type u_1} [LinearOrder β] (c : ℕ → β) (lo m : ℕ) :
ScanInv c lo (lo + m) (List.foldl (bestStep c) { cost := c lo, root := lo } (List.range' (lo + 1) m))
theorem Cslib.Algorithms.Lean.OptimalBST.scanInv_scan {β : Type u_1} [LinearOrder β] (c : ℕ → β) {lo hi : ℕ} (h : lo ≤ hi) :
ScanInv c lo hi (scan c lo hi)

The table algorithms #

The cost of root k for the keys i + 1, …, j, read from the table.

def Cslib.Algorithms.Lean.OptimalBST.cell {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) (T : Table β) (i j lo hi : ℕ) :

The entry for the keys i + 1, …, j, scanning the candidate roots lo, …, hi.

def Cslib.Algorithms.Lean.OptimalBST.diag {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) (W : Window β) (n d : ℕ) (T : Table β) :

Fill the diagonal of intervals (i, i + d] with i + d ≤ n, reading only T.

def Cslib.Algorithms.Lean.OptimalBST.table {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) (W : Window β) (n : ℕ) :
ℕ → Table β

The table after filling the diagonals 1, …, d.

def Cslib.Algorithms.Lean.OptimalBST.solve {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) (W : Window β) (n : ℕ) :

Fill the table for the keys 1, …, n and read off the cost and the tree.

def Cslib.Algorithms.Lean.OptimalBST.plainBST {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) (n : ℕ) :

The cubic dynamic program for optimal binary search trees.

def Cslib.Algorithms.Lean.OptimalBST.knuthBST {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) (n : ℕ) :

Knuth's window dynamic program for optimal binary search trees.

Correctness #

def Cslib.Algorithms.Lean.OptimalBST.Agrees {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) (n d : ℕ) (T : Table β) :

T holds the optimal cost and the largest optimal root of every interval (a, b] with b ≤ n and b - a ≤ d.

def Cslib.Algorithms.Lean.OptimalBST.Window.Valid {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (W : Window β) (w : ℕ → ℕ → β) (n : ℕ) :

A window is valid if, whenever the table is correct on shorter intervals, it lies inside (i, j] and contains the largest optimal root.

theorem Cslib.Algorithms.Lean.OptimalBST.entry_eq_opt_of_scanInv {β : Type u_1} [AddCommMonoid β] [LinearOrder β] {w : ℕ → ℕ → β} {n d : ℕ} {T : Table β} (hT : Agrees w n d T) {i lo hi : ℕ} (hj : i + d + 1 ≤ n) (h₁ : i < lo) (h₂ : lo ≤ optRoot w i (i + d + 1)) (h₃ : optRoot w i (i + d + 1) ≤ hi) (h₄ : hi ≤ i + d + 1) {e : Entry β} (he : ScanInv (T.candidate i (i + d + 1)) lo hi e) :
{ cost := w i (i + d + 1) + e.cost, root := e.root } = { cost := optCost w i (i + d + 1), root := optRoot w i (i + d + 1) }

Any entry satisfying ScanInv (T.candidate i (i + d + 1)) lo hi over a window containing optRoot w i (i + d + 1) recovers both optCost and optRoot.

theorem Cslib.Algorithms.Lean.OptimalBST.cell_eq_opt {β : Type u_1} [AddCommMonoid β] [LinearOrder β] {w : ℕ → ℕ → β} {n d : ℕ} {T : Table β} (hT : Agrees w n d T) {i lo hi : ℕ} (hj : i + d + 1 ≤ n) (h₁ : i < lo) (h₂ : lo ≤ optRoot w i (i + d + 1)) (h₃ : optRoot w i (i + d + 1) ≤ hi) (h₄ : hi ≤ i + d + 1) :
cell w T i (i + d + 1) lo hi = { cost := optCost w i (i + d + 1), root := optRoot w i (i + d + 1) }

A scan over a window containing the largest optimal root computes the correct entry.

theorem Cslib.Algorithms.Lean.OptimalBST.agrees_diag {β : Type u_1} [AddCommMonoid β] [LinearOrder β] {w : ℕ → ℕ → β} {W : Window β} {n d : ℕ} (hW : W.Valid w n) {T : Table β} (hT : Agrees w n d T) :
Agrees w n (d + 1) (diag w W n (d + 1) T)
theorem Cslib.Algorithms.Lean.OptimalBST.agrees_table {β : Type u_1} [AddCommMonoid β] [LinearOrder β] {w : ℕ → ℕ → β} {W : Window β} {n : ℕ} (hW : W.Valid w n) (d : ℕ) :
Agrees w n d (table w W n d)
theorem Cslib.Algorithms.Lean.OptimalBST.solve_eq {β : Type u_1} [AddCommMonoid β] [LinearOrder β] {w : ℕ → ℕ → β} {W : Window β} {n : ℕ} (hW : W.Valid w n) :
solve w W n = { cost := optCost w 0 n, tree := optTree w 0 n }
theorem Cslib.Algorithms.Lean.OptimalBST.plainBST_eq {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) (n : ℕ) :
plainBST w n = { cost := optCost w 0 n, tree := optTree w 0 n }

Correctness of the cubic dynamic program, for every weight: it returns the optimal cost and the tree read off the largest optimal roots.

Knuth's window validity and telescoping bound #

Under the quadrangle inequality and interval monotonicity, Knuth's window is valid.

theorem Cslib.Algorithms.Lean.OptimalBST.knuthBST_eq {β : Type u_1} [AddCommMonoid β] [LinearOrder β] [IsOrderedCancelAddMonoid β] {w : ℕ → ℕ → β} (hqi : QuadrangleIneq w) (hmono : MonotoneOnIntervals w) (n : ℕ) :
knuthBST w n = { cost := optCost w 0 n, tree := optTree w 0 n }

Correctness of Knuth's algorithm when the weight satisfies the quadrangle inequality and is monotone on intervals.

theorem Cslib.Algorithms.Lean.OptimalBST.sum_knuth_window_le {β : Type u_1} [AddCommMonoid β] [LinearOrder β] [IsOrderedCancelAddMonoid β] {w : ℕ → ℕ → β} (hqi : QuadrangleIneq w) (hmono : MonotoneOnIntervals w) {n d : ℕ} {T : Table β} (hT : Agrees w n d T) :
∑ i ∈ Finset.range (n + 1 - (d + 1)), (Window.knuth.hi T i (i + (d + 1)) - Window.knuth.lo T i (i + (d + 1)) + 1) ≤ 2 * n

Knuth's telescoping bound: under the quadrangle inequality and interval monotonicity, the lengths of Knuth's candidate-root windows along diagonal d + 1 sum to at most 2 n.

Cost bounds and optimality of the output #

theorem Cslib.Algorithms.Lean.OptimalBST.optCost_congr {β : Type u_1} [AddCommMonoid β] [LinearOrder β] {w₁ w₂ : ℕ → ℕ → β} {i j : ℕ} (h : ∀ (a b : ℕ), i ≤ a → a < b → b ≤ j → w₁ a b = w₂ a b) :
optCost w₁ i j = optCost w₂ i j

optCost w i j depends on w only through nonempty subintervals (a, b] inside (i, j].

theorem Cslib.Algorithms.Lean.OptimalBST.optCost_le_nsmul {β : Type u_1} [AddCommMonoid β] [LinearOrder β] [IsOrderedAddMonoid β] {w : ℕ → ℕ → β} {M : β} {i j : ℕ} (hw : ∀ (a b : ℕ), i ≤ a → a < b → b ≤ j → w a b ≤ M) :
optCost w i j ≤ (j - i) • M

If every nonempty subinterval (a, b] with i ≤ a < b ≤ j has weight at most M, then optCost w i j ≤ (j - i) • M (attained, up to bound, by the right-spine tree).

A solution for the keys 1, …, n is optimal for the weight w if its tree is a binary search tree on these keys of the returned cost, and no binary search tree on them is cheaper.

theorem Cslib.Algorithms.Lean.OptimalBST.isOptimal_optTree {β : Type u_1} [AddCommMonoid β] [LinearOrder β] [IsOrderedAddMonoid β] (w : ℕ → ℕ → β) (n : ℕ) :
Solution.IsOptimal w n { cost := optCost w 0 n, tree := optTree w 0 n }

The cubic dynamic program returns an optimal binary search tree, for every weight.

Knuth's algorithm returns an optimal binary search tree when the weight satisfies the quadrangle inequality and is monotone on intervals.

theorem Cslib.Algorithms.Lean.OptimalBST.knuthBST_knuthWeight {β : Type u_1} [AddCommMonoid β] [LinearOrder β] [IsOrderedCancelAddMonoid β] {p q : ℕ → β} (hp : ∀ (k : ℕ), 0 ≤ p k) (hq : ∀ (g : ℕ), 0 ≤ q g) (n : ℕ) :

Knuth's theorem for nonnegative access weights p and failure weights q: the window algorithm returns an optimal binary search tree.