Documentation

CslibFrontier.Algorithms.Lean.OptimalBST.Basic

Optimal binary search trees: the interval recurrence #

A binary search tree on the keys i + 1, …, j is a BinaryTree ℕ satisfying IsBST i j. For an interval weight w : ℕ → ℕ → β, the cost treeCost w i j t charges w i' j' at every node whose subtree holds the keys i' + 1, …, j'. With Knuth's weight knuthWeight p q i j = ∑_{i < k ≤ j} p k + ∑_{i ≤ g ≤ j} q g, this is the weighted search cost of Knuth's optimum-search-tree problem: each key k costs p k · (depth of k + 1), and each gap g costs q g times the number of keys compared by an unsuccessful search in that gap (treeCost_knuthWeight, stated with the node-visit counts of Cslib.DataStructure.SearchTree). Gap g holds the values strictly between keys g and g + 1. The expected cost e[1, n] of CLRS counts the dummy leaf of each unsuccessful search as one more level, so it equals treeCost plus ∑ q g.

The interval recurrence c(i, i) = 0, c(i, j) = w(i, j) + min_{i < k ≤ j} (c(i, k - 1) + c(k, j)) is optCost. Its value is the least cost of a tree on the keys i + 1, …, j (isLeast_optCost), attained by the tree optTree read off the largest minimizing roots optRoot. The recurrence and its optimality hold for every weight; no hypothesis on w is needed here.

Main definitions #

Main results #

References #

IsBST i j t: t is a binary search tree whose keys are exactly i + 1, …, j.

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

The interval-weighted cost of a tree on the keys i + 1, …, j: every node whose subtree holds the keys i' + 1, …, j' contributes w i' j'.

def Cslib.Algorithms.Lean.OptimalBST.knuthWeight {β : Type u_1} [AddCommMonoid β] (p q : ℕ → β) (i j : ℕ) :
β

Knuth's interval weight: the access weights p k of the keys i < k ≤ j and the failure weights q g of the gaps i ≤ g ≤ j.

Knuth's weight gives the weighted search cost: each key k is charged its access weight once per node visited by a search for k, and each gap g its failure weight once per node visited by an unsuccessful search between keys g and g + 1.

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

The interval recurrence with explicit fuel.

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

The optimal cost c(i, j) of the interval recurrence c(i, j) = w(i, j) + min_{i < k ≤ j} (c(i, k - 1) + c(k, j)), with c(i, j) = 0 for j ≤ i.

theorem Cslib.Algorithms.Lean.OptimalBST.optCostAux_eq {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) {f g i j : ℕ} (hf : j - i ≤ f) (hg : j - i ≤ g) :
optCostAux w f i j = optCostAux w g i j
theorem Cslib.Algorithms.Lean.OptimalBST.optCostAux_eq_optCost {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) {f i j : ℕ} (hf : j - i ≤ f) :
optCostAux w f i j = optCost w i j
def Cslib.Algorithms.Lean.OptimalBST.splitCost {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) (i j k : ℕ) :
β

The cost of choosing root k for the keys i + 1, …, j, beyond the weight w i j.

theorem Cslib.Algorithms.Lean.OptimalBST.optCost_of_le {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) {i j : ℕ} (h : j ≤ i) :
optCost w i j = 0
theorem Cslib.Algorithms.Lean.OptimalBST.optCost_eq {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) {i j : ℕ} (h : i < j) :
optCost w i j = w i j + (Finset.Ioc i j).inf' ⋯ (splitCost w i j)

The recurrence for optCost.

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

The largest optimal root of the keys i + 1, …, j (and 0 when j ≤ i).

theorem Cslib.Algorithms.Lean.OptimalBST.optRoot_spec {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) {i j : ℕ} (h : i < j) :
optRoot w i j ∈ Finset.Ioc i j ∧ ∀ k ∈ Finset.Ioc i j, splitCost w i j (optRoot w i j) ≤ splitCost w i j k
theorem Cslib.Algorithms.Lean.OptimalBST.optRoot_mem {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) {i j : ℕ} (h : i < j) :
theorem Cslib.Algorithms.Lean.OptimalBST.splitCost_optRoot_le {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) {i j k : ℕ} (hk : k ∈ Finset.Ioc i j) :
splitCost w i j (optRoot w i j) ≤ splitCost w i j k
theorem Cslib.Algorithms.Lean.OptimalBST.le_optRoot {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) {i j k : ℕ} (hk : k ∈ Finset.Ioc i j) (hopt : ∀ k' ∈ Finset.Ioc i j, splitCost w i j k ≤ splitCost w i j k') :
k ≤ optRoot w i j

Every optimal root is at most optRoot.

theorem Cslib.Algorithms.Lean.OptimalBST.optRoot_of_le {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) {i j : ℕ} (h : j ≤ i) :
optRoot w i j = 0
theorem Cslib.Algorithms.Lean.OptimalBST.optCost_eq_optRoot {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) {i j : ℕ} (h : i < j) :
optCost w i j = w i j + splitCost w i j (optRoot w i j)

The tree read off a root table R, with fuel: the root of the keys i + 1, …, j is R i j.

An optimal tree on the keys i + 1, …, j, read off the largest optimal roots.

theorem Cslib.Algorithms.Lean.OptimalBST.buildTree_congr {R R' : ℕ → ℕ → ℕ} {f i j : ℕ} (hR : ∀ (a b : ℕ), i ≤ a → a < b → b ≤ j → R a b = R' a b) (hmem : ∀ (a b : ℕ), i ≤ a → a < b → b ≤ j → R a b ∈ Finset.Ioc a b) :
buildTree R f i j = buildTree R' f i j

Two root tables that agree on all subintervals of (i, j] and choose roots inside them build the same tree.

theorem Cslib.Algorithms.Lean.OptimalBST.buildTree_optRoot_spec {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) {f i j : ℕ} (hf : j - i ≤ f) (hij : i ≤ j) :
IsBST i j (buildTree (optRoot w) f i j) ∧ treeCost w i j (buildTree (optRoot w) f i j) = optCost w i j
theorem Cslib.Algorithms.Lean.OptimalBST.isBST_optTree {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) {i j : ℕ} (hij : i ≤ j) :
IsBST i j (optTree w i j)
theorem Cslib.Algorithms.Lean.OptimalBST.treeCost_optTree {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → β) {i j : ℕ} (hij : i ≤ j) :
treeCost w i j (optTree w i j) = optCost w i j
theorem Cslib.Algorithms.Lean.OptimalBST.optCost_le {β : Type u_1} [AddCommMonoid β] [LinearOrder β] [IsOrderedAddMonoid β] (w : ℕ → ℕ → β) {i j k : ℕ} (hk : k ∈ Finset.Ioc i j) :
optCost w i j ≤ w i j + splitCost w i j k
theorem Cslib.Algorithms.Lean.OptimalBST.optCost_le_treeCost {β : Type u_1} [AddCommMonoid β] [LinearOrder β] [IsOrderedAddMonoid β] (w : ℕ → ℕ → β) {i j : ℕ} {t : BinaryTree ℕ} (h : IsBST i j t) :
optCost w i j ≤ treeCost w i j t

The recurrence is at most the cost of every tree.

theorem Cslib.Algorithms.Lean.OptimalBST.isLeast_optCost {β : Type u_1} [AddCommMonoid β] [LinearOrder β] [IsOrderedAddMonoid β] (w : ℕ → ℕ → β) {i j : ℕ} (hij : i ≤ j) :
IsLeast {c : β | ∃ (t : BinaryTree ℕ), IsBST i j t ∧ treeCost w i j t = c} (optCost w i j)

Optimality of the interval recurrence: optCost w i j is the least cost of a binary search tree on the keys i + 1, …, j, attained by optTree w i j.