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 #
Cslib.Algorithms.Lean.OptimalBST.IsBST: binary search trees on the keysi + 1, …, j.Cslib.Algorithms.Lean.OptimalBST.treeCost: the interval-weighted cost of a tree.Cslib.Algorithms.Lean.OptimalBST.knuthWeight: Knuth's weight from access and failure weights.Cslib.Algorithms.Lean.OptimalBST.optCost,optRoot,optTree: the optimal cost, the largest optimal root and the tree read off it.
Main results #
Cslib.Algorithms.Lean.OptimalBST.isBST_iff:IsBSTis the search-tree invariant ofCslib.DataStructure.SearchTreewith key set(i, j].Cslib.Algorithms.Lean.OptimalBST.treeCost_knuthWeight: Knuth's weight gives the weighted search cost.Cslib.Algorithms.Lean.OptimalBST.optCost_le_treeCost,treeCost_optTree,isLeast_optCost: the recurrence is the minimum cost over all trees, attained byoptTree.
References #
IsBST i j t: t is a binary search tree whose keys are exactly i + 1, …, j.
- nil (i : ℕ) : IsBST i i BinaryTree.nil
- node {i j k : ℕ} {l r : BinaryTree ℕ} (hik : i < k) (hkj : k ≤ j) : IsBST i (k - 1) l → IsBST k j r → IsBST i j (BinaryTree.node k l r)
IsBST i j is the search-tree invariant with key set (i, j].
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'.
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.
The interval recurrence with explicit fuel.
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.
The cost of choosing root k for the keys i + 1, …, j, beyond the weight w i j.
The recurrence for optCost.
The largest optimal root of the keys i + 1, …, j (and 0 when j ≤ i).
Every optimal root is at most optRoot.
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.
Two root tables that agree on all subintervals of (i, j] and choose roots inside them build
the same tree.
The recurrence is at most the cost of every tree.
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.