Documentation

CslibFrontier.DataStructure.BTree.Comparisons.BinarySearch

Comparison costs of B-tree queries with binary in-node search #

This file instantiates lookupBinM and rankBinM in CSLib's query-complexity framework, at FreeM (LEQuery α) with the comparison query LEQuery.ask and the trivial visit := pure (), as Comparisons.Query does for the linear scans. Every key comparison is a query.

Bisecting n keys takes at most ⌈log₂ (n + 1)⌉ comparisons, on any list and under any oracle (countQueries_slotBinM_le). A node of a well-formed tree of minimum degree t holds at most 2 * t - 1 keys, so the search costs at most ⌈log₂ (2 * t)⌉ comparisons per node. Rank needs nothing more; lookup spends one further comparison to test the key found for equality. le_mul_height_of_node_le sums the per-node cost along the descent. The bounds hold for every oracle, since the shape of the tree does not depend on the answers. Under orderOracle the programs evaluate to the pure lookupBin and rankBin, which are lookup and rank on trees with sorted entries (lookupBin_eq_lookup, rankBin_eq_rank).

The linear scans of Comparisons.Query are bounded by 2 * t * height comparisons, so bisection replaces the factor 2 * t by ⌈log₂ (2 * t)⌉ + 1.

Composed with the height bound IsWF.height_le_log, the counts become bounds in the number n of entries: at most (⌈log₂ (2 * t)⌉ + 1) * (log_t ((n + 1) / 2) + 1) comparisons for lookup and ⌈log₂ (2 * t)⌉ * (log_t ((n + 1) / 2) + 1) for rank, with the dependence on t explicit. For fixed t and n ≥ 1 both are within a constant factor of ⌈log₂ (n + 1)⌉. They are stated for every well-formed tree, and as UpperBound statements whose size function is size. They are also stated for the tree bulk-loaded from any list of n entries, sorted or not: like the answers of the oracle, the order of the keys does not change the shape of the tree, and the counts need only the shape (countQueries_lookupBinQ_le_of_occupied, countQueries_rankBinQ_le_of_occupied).

The statements are original to this development; no lower bound is proved here.

Main definitions #

Main results #

def Cslib.DataStructure.BTree.lookupBinQ {α β : Type} (k : α) (tr : BTree α β) :

Lookup by binary in-node search, issuing one query per key comparison.

lookupBinQ is the FreeM (LEQuery α) instantiation of lookupBinM.

def Cslib.DataStructure.BTree.rankBinQ {α β : Type} (k : α) (tr : BTree α β) :

Rank by binary in-node search, issuing one query per key comparison.

theorem Cslib.DataStructure.BTree.rankBinQ_def {α β : Type} {tr : BTree α β} (k : α) :

rankBinQ is the FreeM (LEQuery α) instantiation of rankBinM.

@[simp]
theorem Cslib.DataStructure.BTree.eval_lookupBinQ {α β : Type} {tr : BTree α β} [LinearOrder α] (k : α) :
FreeM.eval (fun {ι : Type} => orderOracle) (lookupBinQ k tr) = lookupBin k tr

Under the order's oracle, lookupBinQ evaluates to the pure lookupBin.

@[simp]
theorem Cslib.DataStructure.BTree.eval_rankBinQ {α β : Type} {tr : BTree α β} [LinearOrder α] (k : α) :
FreeM.eval (fun {ι : Type} => orderOracle) (rankBinQ k tr) = rankBin k tr

Under the order's oracle, rankBinQ evaluates to the pure rankBin.

Comparison counts #

theorem Cslib.DataStructure.BTree.countQueries_slotBinM_le {α β : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (k : α) (rest : List (Triple α β)) :
FreeM.countQueries (fun {ι : Type} => oracle) (slotBinM Query.LEQuery.ask k rest) ≤ Nat.clog 2 (rest.length + 1)

The within-node binary search makes at most ⌈log₂ (n + 1)⌉ comparisons on n keys, sorted or not, under every oracle.

theorem Cslib.DataStructure.BTree.countQueries_lookupBinQ_le_of_occupied {α β : Type} {t : ℕ} {tr : BTree α β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (ht : 0 < t) (hk : tr.rootKeys ≤ 2 * t - 1) (hc : ∀ c ∈ tr.children, Occupied t c) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (lookupBinQ k tr) ≤ (Nat.clog 2 (2 * t) + 1) * tr.height

Lookup by binary in-node search makes at most ⌈log₂ (2 * t)⌉ + 1 comparisons per level, under every oracle, in a tree whose root holds at most 2 * t - 1 keys and whose children are occupied. The keys need not be sorted.

theorem Cslib.DataStructure.BTree.countQueries_rankBinQ_le_of_occupied {α β : Type} {t : ℕ} {tr : BTree α β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (ht : 0 < t) (hk : tr.rootKeys ≤ 2 * t - 1) (hc : ∀ c ∈ tr.children, Occupied t c) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (rankBinQ k tr) ≤ Nat.clog 2 (2 * t) * tr.height

Rank by binary in-node search makes at most ⌈log₂ (2 * t)⌉ comparisons per level, under every oracle, in a tree whose root holds at most 2 * t - 1 keys and whose children are occupied. The keys need not be sorted.

theorem Cslib.DataStructure.BTree.countQueries_lookupBinQ_ofSortedList_le {α β : Type} {t : ℕ} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (ht : 2 ≤ t) (l : List (α × β)) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (lookupBinQ k (ofSortedList t l)) ≤ (Nat.clog 2 (2 * t) + 1) * (Nat.log t ((l.length + 1) / 2) + 1)

Lookup by binary in-node search in the tree bulk-loaded from a list of n entries makes at most (⌈log₂ (2 * t)⌉ + 1) * (log_t ((n + 1) / 2) + 1) comparisons, under every oracle. The list need not be sorted: the shape of the tree does not depend on the keys.

theorem Cslib.DataStructure.BTree.countQueries_rankBinQ_ofSortedList_le {α β : Type} {t : ℕ} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (ht : 2 ≤ t) (l : List (α × β)) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (rankBinQ k (ofSortedList t l)) ≤ Nat.clog 2 (2 * t) * (Nat.log t ((l.length + 1) / 2) + 1)

Rank by binary in-node search in the tree bulk-loaded from a list of n entries makes at most ⌈log₂ (2 * t)⌉ * (log_t ((n + 1) / 2) + 1) comparisons, under every oracle. The list need not be sorted.

theorem Cslib.DataStructure.BTree.countQueries_lookupBinQ_le {α β : Type} {t : ℕ} {tr : BTree α β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) [LinearOrder α] (h : IsWF t tr) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (lookupBinQ k tr) ≤ (Nat.clog 2 (2 * t) + 1) * tr.height

Lookup by binary in-node search in a well-formed tree makes at most ⌈log₂ (2 * t)⌉ + 1 comparisons per level, under every oracle.

theorem Cslib.DataStructure.BTree.countQueries_rankBinQ_le {α β : Type} {t : ℕ} {tr : BTree α β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) [LinearOrder α] (h : IsWF t tr) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (rankBinQ k tr) ≤ Nat.clog 2 (2 * t) * tr.height

Rank by binary in-node search in a well-formed tree makes at most ⌈log₂ (2 * t)⌉ comparisons per level, under every oracle.

Bounds in the number of entries #

theorem Cslib.DataStructure.BTree.countQueries_lookupBinQ_le_log {α β : Type} {t : ℕ} {tr : BTree α β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) [LinearOrder α] (h : IsWF t tr) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (lookupBinQ k tr) ≤ (Nat.clog 2 (2 * t) + 1) * (Nat.log t ((tr.size + 1) / 2) + 1)

Lookup by binary in-node search in a well-formed tree of n entries makes at most (⌈log₂ (2 * t)⌉ + 1) * (log_t ((n + 1) / 2) + 1) comparisons, under every oracle.

theorem Cslib.DataStructure.BTree.countQueries_rankBinQ_le_log {α β : Type} {t : ℕ} {tr : BTree α β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) [LinearOrder α] (h : IsWF t tr) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (rankBinQ k tr) ≤ Nat.clog 2 (2 * t) * (Nat.log t ((tr.size + 1) / 2) + 1)

Rank by binary in-node search in a well-formed tree of n entries makes at most ⌈log₂ (2 * t)⌉ * (log_t ((n + 1) / 2) + 1) comparisons, under every oracle.

Upper bounds over all oracles #

theorem Cslib.DataStructure.BTree.lookupBinQ_upperBound {α β : Type} [LinearOrder α] {t : ℕ} :
Query.UpperBound (fun (p : WFInput α β t) => lookupBinQ (↑p).2 (↑p).1) (fun (p : WFInput α β t) => (↑p).1.height) fun (h : ℕ) => (Nat.clog 2 (2 * t) + 1) * h

Lookup by binary in-node search makes at most ⌈log₂ (2 * t)⌉ + 1 comparisons at each of at most height nodes.

theorem Cslib.DataStructure.BTree.rankBinQ_upperBound {α β : Type} [LinearOrder α] {t : ℕ} :
Query.UpperBound (fun (p : WFInput α β t) => rankBinQ (↑p).2 (↑p).1) (fun (p : WFInput α β t) => (↑p).1.height) fun (h : ℕ) => Nat.clog 2 (2 * t) * h

Rank by binary in-node search makes at most ⌈log₂ (2 * t)⌉ comparisons at each of at most height nodes.

theorem Cslib.DataStructure.BTree.lookupBinQ_upperBound_size {α β : Type} [LinearOrder α] {t : ℕ} :
Query.UpperBound (fun (p : WFInput α β t) => lookupBinQ (↑p).2 (↑p).1) (fun (p : WFInput α β t) => (↑p).1.size) fun (n : ℕ) => (Nat.clog 2 (2 * t) + 1) * (Nat.log t ((n + 1) / 2) + 1)

Lookup by binary in-node search in a well-formed tree of at most n entries makes at most (⌈log₂ (2 * t)⌉ + 1) * (log_t ((n + 1) / 2) + 1) comparisons.

theorem Cslib.DataStructure.BTree.rankBinQ_upperBound_size {α β : Type} [LinearOrder α] {t : ℕ} :
Query.UpperBound (fun (p : WFInput α β t) => rankBinQ (↑p).2 (↑p).1) (fun (p : WFInput α β t) => (↑p).1.size) fun (n : ℕ) => Nat.clog 2 (2 * t) * (Nat.log t ((n + 1) / 2) + 1)

Rank by binary in-node search in a well-formed tree of at most n entries makes at most ⌈log₂ (2 * t)⌉ * (log_t ((n + 1) / 2) + 1) comparisons.