Documentation

CslibFrontier.DataStructure.BTree.Comparisons.Range

Comparison cost of B-tree closed-interval queries #

rangeQ instantiates rangeM in CSLib's query-complexity framework, at FreeM (LEQuery α) with the comparison query LEQuery.ask and the trivial visit := pure (). eval_rangeQ identifies its value under orderOracle with range, through the transfer lemma Cslib.IsMonadHom.map_rangeM.

The scan of a node compares each separator it reaches with both bounds, so one visited node can cost 2 * (2 * t - 1) comparisons: the per-node bound of 2 * t behind the point-query bounds fails, and the node-visit bound of time_rangeT cannot simply be multiplied by it. The count is bounded instead by charging two comparisons to every returned entry and the rest to the two boundary paths. countQueries_rangeQ_of_occupied proves at most 4 * t * height + 2 * k comparisons, where k is the length of the output, on any tree with sorted entries, at most 2 * t - 1 root keys and children occupied for some t > 0, through three auxiliary bounds by recursion on the tree:

countQueries_rangeQ_le restates the bound on well-formed trees as 2 * t * (2 * height + k), and countQueries_rangeQ_le_log composes it with IsWF.height_le_log. The bounds are stated at the order's oracle only: under another oracle the output is not the output under ≤, so a bound in its length is not meaningful.

Main definitions #

Main results #

References #

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

def Cslib.DataStructure.BTree.rangeQ {α β : Type} (lo hi : α) (tr : BTree α β) :
FreeM (Query.LEQuery α) (List (α × β))

Closed-interval query, issuing one query per key comparison.

theorem Cslib.DataStructure.BTree.rangeQ_def {α β : Type} {tr : BTree α β} (lo hi : α) :
rangeQ lo hi tr = rangeM Query.LEQuery.ask (pure ()) lo hi tr

rangeQ is rangeM with the comparison query and free visits.

@[simp]
theorem Cslib.DataStructure.BTree.eval_rangeQ {α β : Type} {tr : BTree α β} [LinearOrder α] (lo hi : α) :
FreeM.eval (fun {ι : Type} => orderOracle) (rangeQ lo hi tr) = range lo hi tr

Under the order's oracle, rangeQ evaluates to range.

@[simp]

The empty tree costs no comparison.

@[simp]
theorem Cslib.DataStructure.BTree.countQueries_rangeQ_node_nil {α β : Type} [LinearOrder α] (lo hi : α) (first : BTree α β) :
FreeM.countQueries (fun {ι : Type} => orderOracle) (rangeQ lo hi (first.node [])) = FreeM.countQueries (fun {ι : Type} => orderOracle) (rangeQ lo hi first)

A node without keys costs what its only child costs.

@[simp]
theorem Cslib.DataStructure.BTree.countQueries_rangeQ_node_cons {α β : Type} [LinearOrder α] (lo hi k : α) (v : β) (first c : BTree α β) (rest : List (α × β × BTree α β)) :
FreeM.countQueries (fun {ι : Type} => orderOracle) (rangeQ lo hi (first.node ((k, v, c) :: rest))) = (2 + if lo ≤ k then FreeM.countQueries (fun {ι : Type} => orderOracle) (rangeQ lo hi first) else 0) + if k ≤ hi then FreeM.countQueries (fun {ι : Type} => orderOracle) (rangeQ lo hi (c.node rest)) else 0

The first separator is compared with both bounds; the child before it is searched when the separator is at least lo, and the remaining triples are scanned, as the node without its first child and separator, when the separator is at most hi.

Auxiliary bounds #

Comparison bounds #

theorem Cslib.DataStructure.BTree.countQueries_rangeQ_of_occupied {α β : Type} {t : ℕ} [LinearOrder α] {lo hi : α} {tr : BTree α β} (ht : 0 < t) (hr : tr.rootKeys ≤ 2 * t - 1) (hc : ∀ c ∈ tr.children, Occupied t c) (hs : tr.toList.IsSortedAssoc) :
FreeM.countQueries (fun {ι : Type} => orderOracle) (rangeQ lo hi tr) ≤ 4 * t * tr.height + 2 * (range lo hi tr).length

A closed-interval query makes at most 4 * t * height + 2 * k comparisons, where k is the number of entries it returns, on any tree with sorted entries, at most 2 * t - 1 root keys and children occupied for some t > 0. Each returned entry costs two comparisons; at each level, the path towards lo costs at most 2 * (2 * t - 1) more and the path towards hi at most two.

theorem Cslib.DataStructure.BTree.countQueries_rangeQ_le {α β : Type} {t : ℕ} {tr : BTree α β} [LinearOrder α] (h : IsWF t tr) (lo hi : α) :
FreeM.countQueries (fun {ι : Type} => orderOracle) (rangeQ lo hi tr) ≤ 2 * t * (2 * tr.height + (range lo hi tr).length)

A closed-interval query on a well-formed tree makes at most 2 * t * (2 * height + k) comparisons, where k is the number of entries it returns: the node-visit bound 2 * height + k of time_rangeT times 2 * t. A single node can cost more than 2 * t comparisons, so this is a consequence of countQueries_rangeQ_of_occupied, not a per-node bound.

theorem Cslib.DataStructure.BTree.countQueries_rangeQ_le_log {α β : Type} {t : ℕ} {tr : BTree α β} [LinearOrder α] (h : IsWF t tr) (lo hi : α) :
FreeM.countQueries (fun {ι : Type} => orderOracle) (rangeQ lo hi tr) ≤ 2 * t * (2 * (Nat.log t ((tr.size + 1) / 2) + 1) + (range lo hi tr).length)

A closed-interval query on a well-formed tree makes logarithmically many comparisons beyond 2 * t per entry it returns.