Documentation

CslibFrontier.DataStructure.BTree.Comparisons.LookupLowerBound

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.

  1. 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 program fun 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 both k ≤ x and x ≤ k for k ≠ x. IsLookup therefore quantifies over the key oracles LEQuery.keyOracle key, which have ties: every total preorder on α is the comparison of such a key function (its quotient by ties).
  2. Distinct values. The n + 1 distinct answers that force the bound are the n stored values and absence, so the per-tree bound lookup_lowerBound_infinite assumes that the values of the tree are distinct, and the Cslib.Query.LowerBound forms 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 #

Main results #

structure Cslib.DataStructure.BTree.IsLookup {α β : Type} (prog : α → BTree α β → FreeM (Query.LEQuery α) (Option β)) :

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.

lookupQ (linear in-node search) is a correct comparison-based lookup program.

lookupBinQ (binary in-node search) is a correct comparison-based lookup program.

theorem Cslib.DataStructure.BTree.lookup_lowerBound_infinite {α β : Type} [Infinite α] {prog : α → BTree α β → FreeM (Query.LEQuery α) (Option β)} (h : IsLookup prog) (tree : BTree α β) (hkeys : (List.map Prod.fst tree.toList).Nodup) (hvals : (List.map Prod.snd tree.toList).Nodup) :
∃ (k : α) (oracle : {ι : Type} → Query.LEQuery α ι → ι), Nat.clog 2 (tree.size + 1) ≤ FreeM.countQueries (fun {ι : Type} => oracle) (prog k tree)

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.

theorem Cslib.DataStructure.BTree.IsLookup.lowerBound_wfInput_infinite {α β : Type} [LinearOrder α] [Infinite α] [Infinite β] {t : ℕ} (ht : 2 ≤ t) {prog : α → BTree α β → FreeM (Query.LEQuery α) (Option β)} (h : IsLookup prog) :
Query.LowerBound (fun (p : WFInput α β t) => prog (↑p).2 (↑p).1) (fun (p : WFInput α β t) => (↑p).1.size) fun (n : ℕ) => Nat.clog 2 (n + 1)

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.

theorem Cslib.DataStructure.BTree.IsLookup.lowerBound_infinite {α β : Type} [Infinite α] [Infinite β] {prog : α → BTree α β → FreeM (Query.LEQuery α) (Option β)} (h : IsLookup prog) :
Query.LowerBound (fun (p : BTree α β × α) => prog p.2 p.1) (fun (p : BTree α β × α) => p.1.size) fun (n : ℕ) => Nat.clog 2 (n + 1)

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.