Documentation

CslibFrontier.DataStructure.BTree.Comparisons.Query

Comparison costs of B-tree point queries #

The point queries of Cslib.DataStructure.BTree are defined once over a monad, with a comparator and a node-visit action. This file instantiates them in CSLib's query-complexity framework, at FreeM (LEQuery α) with the comparison query LEQuery.ask and the trivial visit := pure (), so that every key comparison is a query and node visits are free: lookupQ, containsQ, minQ, maxQ, predQ, succQ and rankQ. Counting is structural: a program parametric in the oracle cannot compare keys without asking.

Applied to FreeM.isMonadHom_pure_eval, the transfer lemmas Cslib.IsMonadHom.map_lookupM and its siblings give eval_lookupQ and its siblings: under orderOracle, the oracle of the order on the keys, each query evaluates to the pure query.

A query scans the keys of a node, one comparison per key passed, and descends into one child; lookup spends one more comparison to test equality. le_mul_height_of_scan turns such a per-node bound of rest.length + 1 into 2 * t * height on a tree whose root holds at most 2 * t - 1 keys and whose children are occupied, which is every well-formed tree. The resulting bounds, countQueries_lookupQ_le and its siblings, hold for every oracle, since the shape of the tree does not depend on the answers; lookupQ_upperBound and its siblings restate them as Cslib.Query.UpperBound over well-formed inputs; lookupQ_upperBound_size and rankQ_upperBound_size bound the cost in the number of entries instead, a bound monotone in that number by monotone_mul_log_size. The minimum and maximum make no comparisons at all. countQueries_slotM_le bounds the within-node linear search by one comparison per key. The bound is the comparison analysis of B-TREE-SEARCH in [CLRS09], Section 18.2.

Main definitions #

Main results #

References #

Ported from cslib-community/EpsilonEridani PR #39 at commit 9425389c8.

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

Lookup, issuing one query per key comparison.

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

lookupQ is lookupM with the comparison query and free visits.

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

Membership test, issuing one query per key comparison.

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

containsQ is containsM with the comparison query and free visits.

def Cslib.DataStructure.BTree.minQ {α β : Type} (tr : BTree α β) :
FreeM (Query.LEQuery α) (Option (α × β))

Least entry in the comparison model, where it asks nothing.

theorem Cslib.DataStructure.BTree.minQ_def {α β : Type} {tr : BTree α β} :
tr.minQ = minM (pure ()) tr

minQ is minM with free visits.

def Cslib.DataStructure.BTree.maxQ {α β : Type} (tr : BTree α β) :
FreeM (Query.LEQuery α) (Option (α × β))

Greatest entry in the comparison model, where it asks nothing.

theorem Cslib.DataStructure.BTree.maxQ_def {α β : Type} {tr : BTree α β} :
tr.maxQ = maxM (pure ()) tr

maxQ is maxM with free visits.

def Cslib.DataStructure.BTree.predQ {α β : Type} (k : α) (tr : BTree α β) :
FreeM (Query.LEQuery α) (Option (α × β))

Strict predecessor, issuing one query per key comparison.

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

predQ is predM with the comparison query and free visits.

def Cslib.DataStructure.BTree.succQ {α β : Type} (k : α) (tr : BTree α β) :
FreeM (Query.LEQuery α) (Option (α × β))

Strict successor, issuing one query per key comparison.

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

succQ is succM with the comparison query and free visits.

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

Rank, issuing one query per key comparison.

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

rankQ is rankM with the comparison query and free visits.

Transfer: eval at the order's oracle is the pure operation #

@[simp]
theorem Cslib.DataStructure.BTree.eval_minQ {α β : Type} {tr : BTree α β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) :
FreeM.eval (fun {ι : Type} => oracle) tr.minQ = tr.min?

Under any oracle, minQ evaluates to min?.

@[simp]
theorem Cslib.DataStructure.BTree.eval_maxQ {α β : Type} {tr : BTree α β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) :
FreeM.eval (fun {ι : Type} => oracle) tr.maxQ = tr.max?

Under any oracle, maxQ evaluates to max?.

@[reducible, inline]

The oracle of the order on the keys.

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

Under the order's oracle, lookupQ evaluates to lookup.

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

Under the order's oracle, containsQ evaluates to contains.

@[simp]
theorem Cslib.DataStructure.BTree.eval_predQ {α β : Type} {tr : BTree α β} [LinearOrder α] (k : α) :
FreeM.eval (fun {ι : Type} => orderOracle) (predQ k tr) = pred? k tr

Under the order's oracle, predQ evaluates to pred?.

@[simp]
theorem Cslib.DataStructure.BTree.eval_succQ {α β : Type} {tr : BTree α β} [LinearOrder α] (k : α) :
FreeM.eval (fun {ι : Type} => orderOracle) (succQ k tr) = succ? k tr

Under the order's oracle, succQ evaluates to succ?.

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

Under the order's oracle, rankQ evaluates to rank.

Comparison counts #

theorem Cslib.DataStructure.BTree.countQueries_slotM_le {α β : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (k : α) (rest : List (α × β × BTree α β)) :
FreeM.countQueries (fun {ι : Type} => oracle) (slotM Query.LEQuery.ask k rest) ≤ rest.length

The within-node linear search makes at most one comparison per key.

@[simp]
theorem Cslib.DataStructure.BTree.countQueries_minQ {α β : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (tr : BTree α β) :
FreeM.countQueries (fun {ι : Type} => oracle) tr.minQ = 0

The minimum search makes no comparison.

@[simp]
theorem Cslib.DataStructure.BTree.countQueries_maxQ {α β : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (tr : BTree α β) :
FreeM.countQueries (fun {ι : Type} => oracle) tr.maxQ = 0

The maximum search makes no comparison.

@[simp]
theorem Cslib.DataStructure.BTree.countQueries_lookupQ_leaf {α β : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (lookupQ k leaf) = 0

Lookup in the empty tree makes no comparison.

@[simp]
theorem Cslib.DataStructure.BTree.countQueries_predQ_leaf {α β : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (predQ k leaf) = 0

Predecessor search in the empty tree makes no comparison.

@[simp]
theorem Cslib.DataStructure.BTree.countQueries_succQ_leaf {α β : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (succQ k leaf) = 0

Successor search in the empty tree makes no comparison.

@[simp]
theorem Cslib.DataStructure.BTree.countQueries_rankQ_leaf {α β : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (rankQ k leaf) = 0

Rank in the empty tree makes no comparison.

theorem Cslib.DataStructure.BTree.countQueries_lookupQ_node_le {α β : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (k : α) (first : BTree α β) (rest : List (α × β × BTree α β)) {B : ℕ} (hB : ∀ c ∈ (first.node rest).children, FreeM.countQueries (fun {ι : Type} => oracle) (lookupQ k c) ≤ B) :
FreeM.countQueries (fun {ι : Type} => oracle) (lookupQ k (first.node rest)) ≤ rest.length + 1 + B

Lookup at a node makes at most one comparison per key and one more beyond what it makes in the child it descends into.

theorem Cslib.DataStructure.BTree.countQueries_predQ_node_le {α β : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (k : α) (first : BTree α β) (rest : List (α × β × BTree α β)) {B : ℕ} (hB : ∀ c ∈ (first.node rest).children, FreeM.countQueries (fun {ι : Type} => oracle) (predQ k c) ≤ B) :
FreeM.countQueries (fun {ι : Type} => oracle) (predQ k (first.node rest)) ≤ rest.length + 1 + B

Predecessor search at a node makes at most one more comparison than the node has keys, beyond what it makes in the child it descends into.

theorem Cslib.DataStructure.BTree.countQueries_succQ_node_le {α β : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (k : α) (first : BTree α β) (rest : List (α × β × BTree α β)) {B : ℕ} (hB : ∀ c ∈ (first.node rest).children, FreeM.countQueries (fun {ι : Type} => oracle) (succQ k c) ≤ B) :
FreeM.countQueries (fun {ι : Type} => oracle) (succQ k (first.node rest)) ≤ rest.length + 1 + B

Successor search at a node makes at most one more comparison than the node has keys, beyond what it makes in the child it descends into.

theorem Cslib.DataStructure.BTree.countQueries_rankQ_node_le {α β : Type} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (k : α) (first : BTree α β) (rest : List (α × β × BTree α β)) {B : ℕ} (hB : ∀ c ∈ (first.node rest).children, FreeM.countQueries (fun {ι : Type} => oracle) (rankQ k c) ≤ B) :
FreeM.countQueries (fun {ι : Type} => oracle) (rankQ k (first.node rest)) ≤ rest.length + 1 + B

Rank at a node makes at most one more comparison than the node has keys, beyond what it makes in the child it descends into.

@[simp]
theorem Cslib.DataStructure.BTree.countQueries_containsQ {α β : Type} {tr : BTree α β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) (k : α) :
FreeM.countQueries (fun {ι : Type} => oracle) (containsQ k tr) = FreeM.countQueries (fun {ι : Type} => oracle) (lookupQ k tr)

The membership test makes as many comparisons as lookup.

Comparison bounds #

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

Lookup in a well-formed tree makes at most 2 * t comparisons per level, under every oracle.

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

The membership test in a well-formed tree makes at most 2 * t comparisons per level, under every oracle.

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

Predecessor search in a well-formed tree makes at most 2 * t comparisons per level, under every oracle.

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

Successor search in a well-formed tree makes at most 2 * t comparisons per level, under every oracle.

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

Rank in a well-formed tree makes at most 2 * t comparisons per level, under every oracle.

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

Lookup in a well-formed tree makes logarithmically many comparisons.

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

The membership test in a well-formed tree makes logarithmically many comparisons.

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

Predecessor search in a well-formed tree makes logarithmically many comparisons.

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

Successor search in a well-formed tree makes logarithmically many comparisons.

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

Rank in a well-formed tree makes logarithmically many comparisons.

Upper bounds over all oracles #

@[reducible, inline]

A well-formed tree of minimum degree t and a query key: the inputs of the bounded point queries.

theorem Cslib.DataStructure.BTree.monotone_mul_log_size (c t : ℕ) :
Monotone fun (n : ℕ) => c * (Nat.log t ((n + 1) / 2) + 1)

A bound c * (log_t ((n + 1) / 2) + 1) in the number n of entries is monotone in n, as UpperBound.of_pointwise requires.

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

Lookup makes at most 2 * t comparisons at each of at most height nodes.

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

The membership test makes at most 2 * t comparisons at each of at most height nodes.

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

Predecessor search makes at most 2 * t comparisons at each of at most height nodes.

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

Successor search makes at most 2 * t comparisons at each of at most height nodes.

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

Rank makes at most 2 * t comparisons at each of at most height nodes.

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

Lookup in a well-formed tree of at most n entries makes at most 2 * t * (log_t ((n + 1) / 2) + 1) comparisons.

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

Rank in a well-formed tree of at most n entries makes at most 2 * t * (log_t ((n + 1) / 2) + 1) comparisons.