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:
- Cubic optimal BST (
plainBSTProgram): fills the interval cost and root tables by increasing interval lengthd + 1 = 1, …, n, scanning every candidate rootk ∈ [i + 1, j]. Computes(plainBST I.weightFn I.numKeys).cost = optCost I.weightFn 0 I.numKeysfor every encoded weight tableI : OptimalBSTInstancein at most17 (n + 1)³standard-basis instructions. - Knuth's window optimal BST (
knuthBSTProgram): restricts each cell(i, j]withi + 1 < jto the candidate-root window[root(i, j - 1), root(i + 1, j)]read from the previous diagonal in memory. WheneverI.weightFnsatisfiesQuadrangleIneqandMonotoneOnIntervals(OptimalBSTInstance.IsKnuthValid), the windows along each diagonal telescope (sum_knuth_window_le) andknuthBSTProgramcomputes(knuthBST I.weightFn I.numKeys).costin at most58 (n + 1)²instructions.
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 #
Cslib.Algorithms.WordRAM.OptimalBST.OptimalBSTInstance: ann-key instance carrying the(n + 1) × (n + 1)interval weight matrixweights, its clamped extensionOptimalBSTInstance.weightFn, its flat word layoutOptimalBSTInstance.encode, its word-width conditionOptimalBSTInstance.FitsWidth, and the quadrangle/monotonicity predicateOptimalBSTInstance.IsKnuthValid.Cslib.Algorithms.WordRAM.OptimalBST.OptimalBSTInstance.ofWeight: restricts any interval weightw : ℕ → ℕ → ℕto keys1, …, nwhile preservingQuadrangleIneqandMonotoneOnIntervals.Cslib.Algorithms.WordRAM.OptimalBST.plainBSTProgram,knuthBSTProgram: fixedProgram .standard 22programs for the cubic and Knuth window algorithms.
Main results #
Cslib.Algorithms.WordRAM.OptimalBST.computesInTime_plainBST,uniformComputableInTime_plainBST:plainBSTProgramcomputes(plainBST I.weightFn I.numKeys).costin at most17 (n + 1)³instructions for everyOptimalBSTInstanceat fitting widths.Cslib.Algorithms.WordRAM.OptimalBST.executes_knuthBST,computesInTime_knuthBST,uniformComputableInTime_knuthBST: underQuadrangleIneqandMonotoneOnIntervals,knuthBSTProgramcomputes(knuthBST I.weightFn I.numKeys).costin at most58 (n + 1)²instructions at fitting widths.Cslib.Algorithms.WordRAM.OptimalBST.OptimalBSTInstance.isKnuthValid_knuthWeight: instances built fromknuthWeight p qsatisfyIsKnuthValid.
References #
Encoded optimal-BST instances #
Clamped extension of I.weights to ℕ → ℕ → ℕ, preserving QuadrangleIneq and
MonotoneOnIntervals.
Row-major (numKeys + 1)²-word table of I.weights.
Flat Word-RAM input [numKeys] ++ weightTable.
Largest interval weight in I.weightTable.
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].
Word-width condition for plainBSTProgram and knuthBSTProgram.
FitsWidth already bounds the length and every word of I.encode.
Every OptimalBSTInstance admits a fitting word width.
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.
Uniform O(n³) Word-RAM computability of plainBST cost, bundling width admissibility.
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.