Comparison lower bound for B-tree lookup #
Information-theoretic ⌈log₂(n + 1)⌉ comparison lower bound for lookup on B-trees with distinct
values over an infinite key type, in CSLib's FreeM (LEQuery α) query-complexity framework; sets
(β = Unit), whose values are not distinct, are not covered (#3674). The argument is an
adaptation of the rank lower bound of
CslibFrontier.DataStructure.BTree.Comparisons.LowerBound: information-theoretic counting over
n + 1 oracles that force n + 1 distinct outcomes. The formulation through oracles with ties
is original to this library.
IsLookup prog requires that, under the comparison oracle LEQuery.keyOracle key of any key
function key : α → W into a linear order under which the keys of tree.toList are strictly
increasing, prog k tree returns the value stored under the key that key identifies with
k, or none. Both B-tree lookup implementations, lookupQ (linear in-node scan) and
lookupBinQ (binary in-node search), satisfy IsLookup (lookupQ_isLookup,
lookupBinQ_isLookup).
The statement deviates in two ways from the plain claim that lookup needs ⌈log₂(n + 1)⌉
comparisons, stated in the shape of the rank bound.
- Oracles with ties instead of linear orders. Under a linear order two keys are equivalent
only when they are equal, so a correctness predicate quantified over linear-order oracles
leaves equality outside the oracle: for
α = ℕthe programfun k tree => pure (tree.toList.lookup k)is correct under every linear-order oracle and asks nothing, and no lower bound holds. A lower bound needs equality to be the oracle's business, so the oracle must be able to answer bothk ≤ xandx ≤ kfork ≠ x.IsLookuptherefore quantifies over the key oraclesLEQuery.keyOracle key, which have ties: every total preorder onαis the comparison of such a key function (its quotient by ties). - Distinct values. The
n + 1distinct answers that force the bound are thenstored values and absence, so the per-tree boundlookup_lowerBound_infiniteassumes that the values of the tree are distinct, and theCslib.Query.LowerBoundforms assume[Infinite β]. Sets (β = Unit) are therefore not covered; the bound for sets is open under#3674, since it needs a counting argument over distinct comparison transcripts rather than distinct results.
Given a tree of size n with distinct keys xs and distinct values, and a fresh key k ∉ xs,
the n + 1 key functions that send xs[j] to j and k to i ∈ {0, …, n} keep xs strictly
increasing. The lookup of k returns the value at index i when i < n and none when i = n.
Applying
FreeM.exists_countQueries_ge_clog to the binary-response query type LEQuery α shows that one
of these oracles forces ⌈log₂(n + 1)⌉ comparisons (lookup_lowerBound_infinite). Bulk-loading
n distinct keys with n distinct values via exists_isWF_toList_map_snd_eq lifts this to
Cslib.Query.LowerBound over arbitrary inputs BTree α β × α (IsLookup.lowerBound_infinite)
and over well-formed trees WFInput α β t of minimum degree t ≥ 2
(IsLookup.lowerBound_wfInput_infinite).
This module covers comparisons in FreeM (LEQuery α), not machine-instruction lower bounds.
Main definitions #
Cslib.DataStructure.BTree.IsLookup: correctness predicate for comparison-based B-tree lookup programs against the comparison oracles of key functions into linear orders.
Main results #
Cslib.DataStructure.BTree.lookupQ_isLookup,Cslib.DataStructure.BTree.lookupBinQ_isLookup: linear and binary in-node B-tree lookup satisfyIsLookup.Cslib.DataStructure.BTree.lookup_lowerBound_infinite: on any tree of sizenwith distinct keys and distinct values over an infinite key type, anyIsLookupprogram makes at least⌈log₂(n + 1)⌉comparisons on some query key and comparison oracle.Cslib.DataStructure.BTree.IsLookup.lowerBound_infinite: for[Infinite α] [Infinite β],Cslib.Query.LowerBoundof⌈log₂(n + 1)⌉overBTree α β × αwith sizep.1.size.Cslib.DataStructure.BTree.IsLookup.lowerBound_wfInput_infinite: for[Infinite α] [Infinite β],Cslib.Query.LowerBoundof⌈log₂(n + 1)⌉over well-formed inputsWFInput α β t(2 ≤ t) with sizep.val.1.size.
IsLookup prog asserts that against the comparison oracle LEQuery.keyOracle key of every key
function key : α → W into a linear order under which the keys of tree.toList are strictly
increasing, prog k tree evaluates to the lookup of key k in the entries of tree with keys
mapped by key: the value of the entry whose key key identifies with k, or none.
The key oracles have ties: every total preorder on α is the comparison of such a key function
(its quotient by ties). Quantifying over linear orders instead, as IsRank does, would leave
equality outside the oracle: for α = ℕ the program fun k tree => pure (tree.toList.lookup k)
would be correct and ask nothing. With ties the oracle can answer both k ≤ x and x ≤ k for
k ≠ x, so whether the query key is present is decided by comparisons, and the lower bound
lookup_lowerBound_infinite holds. That bound also needs distinct values, since its n + 1
distinct answers are the n stored values and absence; sets (β = Unit) are not covered, and the
bound for them, which needs a counting argument over distinct comparison transcripts rather than
distinct results, is open under #3674.
- correct {W : Type} [LinearOrder W] (key : α → W) (k : α) (tree : BTree α β) : List.Pairwise (fun (x1 x2 : W) => x1 < x2) (List.map (fun (x : α × β) => key x.1) tree.toList) → FreeM.eval (fun {ι : Type} => Query.LEQuery.keyOracle key) (prog k tree) = List.lookup (key k) (List.map (fun (x : α × β) => (key x.1, x.2)) tree.toList)
Under the oracle of a key function that strictly orders
tree.toList's keys,prog k treeis the lookup ofkey kamong the entries with mapped keys.
lookupQ (linear in-node search) is a correct comparison-based lookup program.
lookupBinQ (binary in-node search) is a correct comparison-based lookup program.
Per-tree comparison lower bound for lookup. On any B-tree tree with distinct keys and
distinct values over an infinite key type α, any IsLookup program makes at least
⌈log₂(tree.size + 1)⌉ comparisons for some query key k and comparison oracle.
Comparison lower bound for B-tree lookup over well-formed inputs (WFInput). When
2 ≤ t, any IsLookup program restricted to well-formed B-trees of minimum degree t
(WFInput α β t) on an infinite key type α and infinite value type β still has worst-case
comparison query complexity at least ⌈log₂(n + 1)⌉ at every input size n.
Comparison lower bound for B-tree lookup (Cslib.Query.LowerBound). Any IsLookup program
on an infinite key type α and infinite value type β has worst-case comparison query complexity
at least ⌈log₂(n + 1)⌉ on trees of size at most n.