Documentation

CslibFrontier.DataStructure.BTree.Comparisons.LowerBound

Comparison lower bound for B-tree rank search #

Information-theoretic ⌈log₂(n + 1)⌉ comparison lower bound for rank queries on B-trees over an infinite key type, in CSLib's FreeM (LEQuery α) query-complexity framework ([Knu98], Section 6.2.1).

A comparison-based rank program can achieve logarithmic query complexity only when the input tree's keys are strictly increasing under the oracle's linear order: a search that descends into a single child per level misses out-of-order keys in sibling subtrees, and ranking an arbitrary unsorted list of n keys requires n comparisons. Accordingly, IsRank requires that under any oracle implementing a linear order r for which (tree.toList.map Prod.fst) is strictly r-increasing, the program returns the number of entries strictly r-below the query key k. Both B-tree rank implementations—rankQ (linear in-node scan) and rankBinQ (binary in-node search)—satisfy IsRank (rankQ_isRank, rankBinQ_isRank).

Given any tree tree of size n with duplicate-free keys xs = tree.toList.map Prod.fst and a fresh key k ∉ xs (obtained from [Infinite α]), inserting k at each of the n + 1 gap positions 0, …, n of xs defines n + 1 linear orders on α. Every one of these n + 1 linear orders keeps xs strictly increasing while giving k a distinct rank in {0, …, n}. Applying FreeM.exists_countQueries_ge_clog to the binary-response query type LEQuery α shows that at least one of those n + 1 oracles forces ⌈log₂(n + 1)⌉ comparisons (rank_lowerBound_infinite). Bulk-loading n distinct keys via ofSortedList lifts this per-tree bound to Cslib.Query.LowerBound both over arbitrary BTree α β × α inputs (IsRank.lowerBound_infinite, requiring no ambient order on α) and over well-formed trees WFInput α β t of minimum degree t ≥ 2 (IsRank.lowerBound_wfInput_infinite, matching rankBinQ_upperBound_size so LowerBound.le_upperBound applies directly). The trees come from exists_isWF_toList_map_snd_eq, which bulk-loads any prescribed list of values under distinct increasing keys; the form over arbitrary inputs follows from the well-formed one under any linear order on α.

This module covers comparison-based rank in FreeM (LEQuery α) (not machine-instruction lower bounds). The companion comparison lower bound for lookup, in CslibFrontier.DataStructure.BTree.Comparisons.LookupLowerBound, is proved for trees with distinct values (its Cslib.Query.LowerBound forms assume an infinite value type); sets, whose values are not distinct, are not covered and remain open under #3674. It needs oracles with ties, because under linear orders alone equality is outside the oracle and a lookup program can be correct without asking.

Main definitions #

Main results #

References #

structure Cslib.DataStructure.BTree.IsRank {α β : Type} (rankProg : α → BTree α β → FreeM (Query.LEQuery α) ℕ) :

IsRank rankProg asserts that against every oracle implementing a linear order r under which the keys of tree.toList are strictly increasing, rankProg k tree evaluates to the number of entries of tree.toList whose key is strictly r-below k.

rankQ (linear in-node search) is a correct comparison-based rank program.

rankBinQ (binary in-node search) is a correct comparison-based rank program.

theorem Cslib.DataStructure.BTree.rank_lowerBound_infinite {α β : Type} [Infinite α] {rankProg : α → BTree α β → FreeM (Query.LEQuery α) ℕ} (h_corr : IsRank rankProg) (tree : BTree α β) (h_nodup : (List.map Prod.fst tree.toList).Nodup) :
∃ (k : α) (oracle : {ι : Type} → Query.LEQuery α ι → ι), Nat.clog 2 (tree.size + 1) ≤ FreeM.countQueries (fun {ι : Type} => oracle) (rankProg k tree)

Per-tree comparison lower bound for rank. On any B-tree tree with distinct keys over an infinite key type α, any IsRank program makes at least ⌈log₂(tree.size + 1)⌉ comparisons for some query key k and comparison oracle.

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

Comparison lower bound for B-tree rank over well-formed inputs (WFInput). When 2 ≤ t, any IsRank program restricted to well-formed B-trees of minimum degree t (WFInput α β t, matching rankBinQ_upperBound_size) still has worst-case comparison query complexity at least ⌈log₂(n + 1)⌉ at every input size n.

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

Comparison lower bound for B-tree rank (Cslib.Query.LowerBound). Any IsRank program on an infinite key type α and nonempty value type β has worst-case comparison query complexity at least ⌈log₂(n + 1)⌉ on trees of size at most n.