Documentation

CslibFrontier.Algorithms.WordRAM.OptimalBST

Word-RAM optimal binary search trees and Knuth's quadratic speedup #

Checked Word-RAM implementations and O(n³) / O(n²) running-time bounds for the dynamic programs of CslibFrontier.Algorithms.Lean.OptimalBST.Knuth:

Both programs instantiate a shared private dynamic-programming engine parameterized by the candidate-window subprogram. Every header read, row-major address calculation, table lookup, candidate comparison, memory store, loop branch/jump, and final halt is charged by the Word-RAM semantics.

Main definitions #

Main results #

References #

Encoded optimal-BST instances #

An encoded optimal-BST instance on keys 1, …, numKeys with interval weights weights i j (0 ≤ i, j ≤ numKeys).

Clamped extension of I.weights to ℕ → ℕ → ℕ, preserving QuadrangleIneq and MonotoneOnIntervals.

Address upper bound covering input words, loop counters, and both (numKeys + 1)²-word working tables (cost and root).

Value upper bound (numKeys + 1) * maxWeight, dominating every input weight and intermediate DP cost sum on intervals inside [0, numKeys].

@[reducible, inline]

I.weightFn satisfies the quadrangle inequality and interval monotonicity required by Knuth's quadratic speedup.

Restrict any interval weight w : ℕ → ℕ → ℕ to keys 1, …, n.

Variable-cost counting loop helper #

Registers and shared Word-RAM DP engine #

Fixed standard-basis Word-RAM program for the O(n³) optimal-BST dynamic program.

Fixed standard-basis Word-RAM program for Knuth's O(n²) window optimal-BST algorithm.

Memory representation and framing lemmas #

Execution of setup, candidate evaluation, inner scan, and cell body #

Parameterized execution of bstCode #

Cubic algorithm (plainBSTProgram) and Knuth's quadratic algorithm (knuthBSTProgram) #

Word-RAM cubic running time of plainBST. For every I : OptimalBSTInstance (with no quadrangle or monotonicity hypothesis), plainBSTProgram computes (plainBST I.weightFn I.numKeys).cost in at most 17 (n + 1)³ instructions at every fitting width w.

Program.ComputesInTime theorem for knuthBSTProgram over instances satisfying OptimalBSTInstance.IsKnuthValid.

Word-RAM quadratic running time of Knuth's optimal-BST algorithm. Whenever I.weightFn satisfies QuadrangleIneq and MonotoneOnIntervals, knuthBSTProgram halts within 58 (n + 1)² instructions returning (knuthBST I.weightFn I.numKeys).cost at every fitting width w.

Uniform O(n²) Word-RAM computability of Knuth's optimal-BST algorithm over instances satisfying OptimalBSTInstance.IsKnuthValid.