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 #
Cslib.Algorithms.Lean.OptimalBST.Entry,Table,Window: table entries, tables, and candidate-root window rules (Window.plain,Window.knuth).Cslib.Algorithms.Lean.OptimalBST.scan,cell,diag,table,solve: pure window scan and diagonal-by-diagonal dynamic programming.Cslib.Algorithms.Lean.OptimalBST.plainBST: the cubic dynamic program.Cslib.Algorithms.Lean.OptimalBST.knuthBST: Knuth's window dynamic program.Cslib.Algorithms.Lean.OptimalBST.Solution,Solution.IsOptimal: the output pair and optimality predicate.
Main results #
Cslib.Algorithms.Lean.OptimalBST.plainBST_eq,isOptimal_plainBST: correctness of the cubic algorithm, for every weight.Cslib.Algorithms.Lean.OptimalBST.knuthBST_eq,isOptimal_knuthBST: correctness of Knuth's algorithm under the quadrangle inequality and interval monotonicity.Cslib.Algorithms.Lean.OptimalBST.sum_knuth_window_le: Knuth's telescoping bound (≤ 2 ncandidate roots per diagonal).Cslib.Algorithms.Lean.OptimalBST.optCost_congr,optCost_le_nsmul: subinterval congruence and linear bound(j - i) • MonoptCost w i jfrom a uniform boundMon interval weights.Cslib.Algorithms.Lean.OptimalBST.knuthBST_knuthWeight: Knuth's optimality theorem for nonnegative access and failure weights.
References #
Tables and windows #
A table entry: a cost and a root.
- cost : β
The cost of the interval.
- root : ℕ
The chosen root.
A dynamic-programming table, indexed by the endpoints i, j of the key interval
i + 1, …, j.
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.
The output of an optimal-BST algorithm: a cost and a tree.
- cost : β
The optimal cost.
- tree : BinaryTree ℕ
A tree attaining it.
The scan of a window #
Keep the cheaper of the current best entry and candidate k, preferring k on ties.
Scan the candidates lo, …, hi for a largest minimizer of c.
The table algorithms #
The cost of root k for the keys i + 1, …, j, read from the table.
The entry for the keys i + 1, …, j, scanning the candidate roots lo, …, hi.
Fill the diagonal of intervals (i, i + d] with i + d ≤ n, reading only T.
The table after filling the diagonals 1, …, d.
Fill the table for the keys 1, …, n and read off the cost and the tree.
The cubic dynamic program for optimal binary search trees.
Knuth's window dynamic program for optimal binary search trees.
Correctness #
T holds the optimal cost and the largest optimal root of every interval (a, b] with
b ≤ n and b - a ≤ d.
A window is valid if, whenever the table is correct on shorter intervals, it lies inside
(i, j] and contains the largest optimal root.
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.
A scan over a window containing the largest optimal root computes the correct entry.
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.
Correctness of Knuth's algorithm when the weight satisfies the quadrangle inequality and is monotone on intervals.
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 #
optCost w i j depends on w only through nonempty subintervals (a, b] inside (i, j].
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.
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.
Knuth's theorem for nonnegative access weights p and failure weights q: the window
algorithm returns an optimal binary search tree.