Binary Search Trees in Lean Check a result Declarations Bibliography Lean source

Chapter 28Multiple trees

A request sequence often has more structure than its key frequencies show. If key 33 is always followed by key 1717, the second request is predictable from the first, however rare 1717 is overall. The natural measure of this predictability is the empirical conditional entropy HH of each request given the previous one, and the natural benchmark is m(1+H)m(1 + H) for mm requests: one unit per request, plus the information it carries given its predecessor. Chapter 18 defines this benchmark and proves, following Lemma 7 of Bose, Douïeb, Iacono and Langerman (2016), that binary search trees cannot reach it: no execution of a BST, even one that rotates and knows the whole sequence in advance, stays within a constant factor of m(1+H)m(1+H), plus a startup term depending only on nn, on every sequence. A lazy finger can be simulated by rotations at a constant factor (Chapter 16), so the optimal lazy-finger tree of Chapter 27, which adapts to where the previous search ended, cannot reach it either.

Theorem 9 of the same paper shows that the benchmark is reachable once the structure may hold several trees. Store, for every key pp, a small search tree holding the keys that most often follow pp, each with a pointer to its own small tree, and one shared balanced tree as a fallback. With cc successors per key, the structure has O(nc)O(nc) words, and each request costs about log⁡(1/Pr⁡[x∣p])\log(1/\Pr[x \mid p]) when it hits and O(log⁡n)O(\log n) when it misses. Misses are rare transitions, so the total is within a factor log⁡n/log⁡c\log n / \log c of m(1+H)m(1 + H). With c=nεc = n^\varepsilon, this is a constant factor in polynomial space, the consequence the paper draws in its Section 7.

Main result · Bose et al. 2016
Multiple trees serve every sequence within O(log⁡cn)O(\log_c n) of the conditional-entropy bound in O(nc)O(nc) words

Let n≥1n \ge 1 and 1≤c≤n1 \le c \le n. Let pp be a key and X=x1,…,xmX = x_1, \dots, x_m a sequence of keys, all in {0,…,n−1}\{0, \dots, n-1\}. Let FabF_{ab} be the number of indices jj with (xj−1,xj)=(a,b)(x_{j-1}, x_j) = (a, b), where x0=px_0 = p, let Ra=∑bFabR_a = \sum_b F_{ab}, and let

I(X)  =  ∑a,b : Fab>0Fablog⁡2RaFabI(X) \;=\; \sum_{a,b \,:\, F_{ab} > 0} F_{ab} \log_2 \frac{R_a}{F_{ab}}

be the empirical conditional information of XX, so that I=mHI = mH. The multiple-tree store built from FF with capacity cc uses at most 10nc10nc words, and serves XX, starting from pp, at total cost at most

6 (m+I(X)) 1+log⁡2n1+log⁡2c.6\,\bigl(m + I(X)\bigr)\, \frac{1 + \log_2 n}{1 + \log_2 c}.

Moreover, for every ε>0\varepsilon > 0 there is a capacity cc, depending only on nn and ε\varepsilon, such that for every pp and XX the store built from FF uses at most 20 n1+ε20\,n^{1+\varepsilon} words and serves XX at cost at most 6min⁡(ε,1) m (1+H)\frac{6}{\min(\varepsilon, 1)}\, m\,(1 + H).

The cost is that of the linked-search model of Chapter 11: one unit per visited tree node and per pointer link followed. The store is built from the transition counts of the very sequence it serves, and the time to build it is not part of the bound; the counts themselves are not stored. In Lean, the execution is LinkedSearch.Execution, the space Store.words, the counts LazyFinger.frequency, m+Im + I is ConditionalEntropy.cost and HH is ConditionalEntropy.entropy.

The Lean statements
theoremMultipleTrees.Results.sequencepropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Abstract.lean:46
/-- Theorem 9 of arXiv:1304.6897 in the explicit static linked-pointer model.The empirical matrix is used for preprocessing only. Cost includes one unitper visited node and each fallback/successor link traversal. Request normalizationand `1 + log` factors cover deterministic rows, empty sequences, and capacity one.The initial predecessor is explicit; preprocessing time is not part of the bound. -/theorem sequence (n capacity : Nat) (positive : 0 < n)    (nonempty : 0 < capacity) (fits : capacity ≤ n) (p : Fin n) (xs : List (Fin n)) :    ∃ q k,      LinkedSearch.Execution (build n capacity positive (LazyFinger.frequency p (xs.map Fin.val)))        p (xs.map Fin.val) q k ∧      (k : ℝ) ≤ 6 * ConditionalEntropy.cost n p (xs.map Fin.val) *        (1 + Real.logb 2 n) / (1 + Real.logb 2 capacity) ∧      (build n capacity positive (LazyFinger.frequency p (xs.map Fin.val))).words ≤ 10 * n * capacity := …  sequence_tradeoff n capacity positive nonempty fits p xs
theoremMultipleTrees.Results.polynomial_spacepropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Abstract.lean:61
/-- For each fixed positive exponent, Section 7's structure uses`O(n^(1+ε))` words and `O(m(1+H))` query work. The integer capacity is chosenbefore the sequence; preprocessing the actual trees still uses its empiricalfrequencies. The constants are explicit, including exponents greater than one.Initialization uses the specified predecessor and preprocessing time is excluded. -/theorem polynomial_space (n : Nat) (positive : 0 < n) (ε : ℝ) (exponent : 0 < ε) :    ∃ capacity : Nat, 0 < capacity ∧ capacity ≤ n ∧      ∀ (p : Fin n) (xs : List (Fin n)), ∃ q k,        LinkedSearch.Execution          (build n capacity positive (LazyFinger.frequency p (xs.map Fin.val)))          p (xs.map Fin.val) q k ∧        (k : ℝ) ≤ (6 / min ε 1) * (xs.length : ℝ) *          (1 + ConditionalEntropy.entropy n p (xs.map Fin.val)) ∧        ((build n capacity positive (LazyFinger.frequency p (xs.map Fin.val))).words : ℝ) ≤          20 * (n : ℝ) ^ (1 + ε) := …  MultipleTrees.polynomial_space n positive ε exponent
32 definitions in 6 chapters · reading list

The box is enough to confirm the result; its reading path consists of the linked-search model, the conditional-entropy definitions and the construction of this chapter. The rest of the chapter builds the structure and proves the bound. Section 28.1 explains the setting, Section 28.2 and Section 28.3 build the local trees, Section 28.4 assembles them, Section 28.5 bounds a single request, and Section 28.6 states the paper’s Theorem 9 and its Section 7 consequence in full.

The definitions are in Algorithms/MultipleTrees/Definitions/Abstract.lean, Selection/Definitions/Abstract.lean and Cache/Definitions/Abstract.lean; the theorems are in Algorithms/MultipleTrees/Abstract.lean, Selection/Abstract.lean and Cache/Abstract.lean.

28.1 Offline preprocessing, online queries

The structure is static, like the optimal trees of Chapter 27: it is built once, from the transition counts of the sequence, and then serves the sequence without changing. Three conventions follow, and the theorems state each of them explicitly.

  • The counts are used for preprocessing only. The store holds trees and pointers, nothing else: a query compares keys and follows pointers, and never consults a frequency. Store.words counts the store’s words, and the transition matrix is not among them.
  • Preprocessing time is excluded. The bounds concern the queries. No theorem bounds the time to compute the counts, sort them and build the trees, which is polynomial but not formalized.
  • The initial predecessor is explicit. The first request is served from the local tree of a given key pp, and the pair (p,x1)(p, x_1) is a transition like any other.

The cost model is the linked-search model of Chapter 11. A LinkedSearch.Store is a directory of local trees, one per key, and a shared fallback tree. Each node stores a key, two children and a pointer to a local tree. A cursor holds the local tree of the previous request. LinkedSearch.Request serves a key xx by an ordinary comparison search in the cursor’s local tree. If it finds xx, it follows xx’s stored pointer, at cost 11. If not, it follows the fallback pointer, at cost 11, searches the fallback tree, and follows the pointer found there, at cost 11. Each visited node costs 11. In both cases the cursor ends at xx’s local tree. LinkedSearch.Execution chains requests, adding their costs.

Lazy finger and multiple trees are both ways of exploiting where the previous request was. A lazy finger walks from the previous key inside one tree, so all predecessors share one shape. Multiple trees give each predecessor its own shape, at the price of nn trees, which is why they can reach the conditional-entropy bound and a single tree cannot.

28.2 Selecting the successors

For a fixed predecessor pp, the local tree holds the cc keys that most often follow pp, where cc is the capacity.

defMultipleTrees.rankingpropext · Quot.soundAlgorithms/MultipleTrees/Selection/Definitions/Abstract.lean:8
/-- Rank all keys by decreasing outgoing frequency. Ties retain a validnonincreasing order and do not affect the omission bound. -/def ranking (n : Nat) (frequency : Nat → Nat) : List Nat :=  (List.range n).mergeSort (fun a b => frequency b ≤ frequency a)
defMultipleTrees.selectedpropext · Quot.soundAlgorithms/MultipleTrees/Selection/Definitions/Abstract.lean:13
/-- The most frequent successors stored for one predecessor. This list is infrequency order; an ordered-key representation is built separately. -/def selected (n capacity : Nat) (frequency : Nat → Nat) : List Nat :=  (ranking n frequency).take capacity

ranking n frequency lists the keys 0,…,n−10, \dots, n-1 by nonincreasing frequency, using a merge sort; how ties are ordered does not matter for the bounds. selected n capacity frequency keeps the first capacity of them. Here frequency is one row of the transition matrix, the counts FpbF_{pb} for a fixed pp.

Theorem (selection). The selected list has min⁡(c,n)\min(c, n) entries, has no repetitions, and contains only keys less than nn. Every selected key is at least as frequent as every omitted key.

theoremMultipleTrees.Selection.Results.sizepropext · Quot.soundAlgorithms/MultipleTrees/Selection/Abstract.lean:9
theorem size (n capacity : Nat) (frequency : Nat → Nat) :    (selected n capacity frequency).length = min capacity n := selected_length n capacity frequency
theoremMultipleTrees.Selection.Results.distinctpropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Selection/Abstract.lean:12
theorem distinct (n capacity : Nat) (frequency : Nat → Nat) :    (selected n capacity frequency).Nodup := selected_distinct n capacity frequency
theoremMultipleTrees.Selection.Results.validpropext · Quot.soundAlgorithms/MultipleTrees/Selection/Abstract.lean:15
theorem valid (n capacity : Nat) (frequency : Nat → Nat) :    ∀ x ∈ selected n capacity frequency, x < n := selected_valid n capacity frequency
theoremMultipleTrees.Selection.Results.dominatespropext · Quot.soundAlgorithms/MultipleTrees/Selection/Abstract.lean:18
theorem dominates (n capacity : Nat) (frequency : Nat → Nat) (x y : Nat)    (stored : x ∈ selected n capacity frequency) (valid : y < n)    (omitted : y ∉ selected n capacity frequency) : frequency y ≤ frequency x :=  selected_dominates n capacity frequency x y stored valid omitted

The point of the selection is that an omitted successor is rare. If c≤nc \le n keys are kept and yy is not among them, then each of the cc kept keys is at least as frequent as yy, so c⋅Fpy≤Rpc \cdot F_{py} \le R_p, the row total.

Theorem (misses are rare). If c≤nc \le n and y<ny < n is not selected, then c⋅Fpy≤Rpc \cdot F_{py} \le R_p. If moreover c≥1c \ge 1 and Fpy>0F_{py} > 0, then log⁡2c≤log⁡2(Rp/Fpy)\log_2 c \le \log_2(R_p / F_{py}).

theoremMultipleTrees.Selection.Results.misspropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Selection/Abstract.lean:23
theorem miss (n capacity : Nat) (frequency : Nat → Nat) (y : Nat)    (fits : capacity ≤ n) (valid : y < n) (omitted : y ∉ selected n capacity frequency) :    capacity * frequency y ≤ ((List.range n).map frequency).sum :=  omitted_frequency n capacity frequency y fits valid omitted
theoremMultipleTrees.Selection.Results.miss_informationpropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Selection/Abstract.lean:28
theorem miss_information (n capacity : Nat) (frequency : Nat → Nat) (y : Nat)    (fits : capacity ≤ n) (nonempty : 0 < capacity) (valid : y < n)    (omitted : y ∉ selected n capacity frequency) (positive : 0 < frequency y) :    Real.logb 2 capacity ≤ Real.logb 2 ((((List.range n).map frequency).sum : ℝ) / frequency y) :=  omitted_information n capacity frequency y fits nonempty valid omitted positive

The second form is the one the tradeoff uses: a miss costs O(log⁡n)O(\log n), but it only happens on a transition whose information log⁡2(Rp/Fpy)\log_2(R_p/F_{py}) is already at least log⁡2c\log_2 c.

28.3 Local trees

defMultipleTrees.cacheKeyspropext · Quot.soundAlgorithms/MultipleTrees/Cache/Definitions/Abstract.lean:10
/-- The most frequent successors, rearranged into key order for comparisons. -/def cacheKeys (n capacity : Nat) (frequency : Nat → Nat) : List Nat :=  (selected n capacity frequency).mergeSort (· ≤ ·)
defMultipleTrees.cacheTreepropext · Quot.soundAlgorithms/MultipleTrees/Cache/Definitions/Abstract.lean:14
/-- A static cache stores actual universe keys. Its shape balances a mixture ofsuccessor frequencies and uniform weight, controlling both hits and misses. -/def cacheTree (n capacity : Nat) (frequency : Nat → Nat) : LabelledTree Nat :=  (WeightedSearch.balancedBuild ((cacheKeys n capacity frequency).map frequency)).labelWith    (cacheKeys n capacity frequency)

cacheKeys sorts the selected keys into increasing order, so that they can label a search tree. cacheTree builds a shape for them with WeightedSearch.balancedBuild of Chapter 34, weighted by their frequencies and mixed with a uniform baseline, and labels its nodes with the keys in inorder using Tree.labelWith. The baseline gives every stored key logarithmic depth, and the frequencies give frequent successors small depth.

Theorem (local trees). The local tree has min⁡(c,n)\min(c, n) nodes. Its inorder key list is the sorted selection, a permutation of the selected keys, and it is a valid search tree.

theoremMultipleTrees.Cache.Results.sizepropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Cache/Abstract.lean:9
theorem size (n capacity : Nat) (frequency : Nat → Nat) :    (cacheTree n capacity frequency).erase.size = min capacity n := cacheTree_size n capacity frequency
theoremMultipleTrees.Cache.Results.keyspropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Cache/Abstract.lean:12
theorem keys (n capacity : Nat) (frequency : Nat → Nat) :    (cacheTree n capacity frequency).inorder = cacheKeys n capacity frequency ∧      (cacheKeys n capacity frequency).Perm (selected n capacity frequency) :=  ⟨cacheTree_inorder n capacity frequency, cacheKeys_perm n capacity frequency⟩
theoremMultipleTrees.Cache.Results.orderedpropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Cache/Abstract.lean:17
theorem ordered (n capacity : Nat) (frequency : Nat → Nat) :    (cacheTree n capacity frequency).Ordered := cacheTree_ordered n capacity frequency

Theorem (search in a local tree). An ordinary comparison search for a key xx in the local tree finds it exactly when xx is selected. If c≥1c \ge 1, then whether it succeeds or not, it visits at most log⁡2(2min⁡(c,n))+1\log_2(2\min(c, n)) + 1 nodes. If xx is selected and Fpx>0F_{px} > 0, it visits at most log⁡2(3Rp/Fpx)+1\log_2(3R_p / F_{px}) + 1 nodes.

theoremMultipleTrees.Cache.Results.membershippropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Cache/Abstract.lean:20
theorem membership (n capacity : Nat) (frequency : Nat → Nat) (x : Nat) :    (LabelledTree.lookup (cacheTree n capacity frequency) x).1 = true ↔      x ∈ selected n capacity frequency := cacheTree_lookup n capacity frequency x
theoremMultipleTrees.Cache.Results.uniformpropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Cache/Abstract.lean:24
theorem uniform (n capacity : Nat) (frequency : Nat → Nat) (x : Nat)    (nonempty : 0 < min capacity n) :    ((LabelledTree.lookup (cacheTree n capacity frequency) x).2 : ℝ) ≤      Real.logb 2 (2 * ((min capacity n : Nat) : ℝ)) + 1 := cacheTree_uniform n capacity frequency x nonempty
theoremMultipleTrees.Cache.Results.weightedpropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Cache/Abstract.lean:29
theorem weighted (n capacity : Nat) (frequency : Nat → Nat) (x : Nat)    (stored : x ∈ selected n capacity frequency) (positive : 0 < frequency x) :    ((LabelledTree.lookup (cacheTree n capacity frequency) x).2 : ℝ) ≤      Real.logb 2 (3 * (((List.range n).map frequency).sum : ℝ) / frequency x) + 1 :=  cacheTree_weighted n capacity frequency x stored positive

The search is LabelledTree.lookup, which returns whether the key was found and the number of visited nodes; an unsuccessful search can visit nodes too. In the weighted bound, RpR_p is the total of the whole row, not only of the selected keys, which is weaker but is the quantity that appears in the conditional information.

28.4 The store

defMultipleTrees.rootAddressno axiomsAlgorithms/MultipleTrees/Definitions/Abstract.lean:9
/-- Preprocessing converts a universe key into its local-root address. Themodulus only totalizes out-of-universe inputs; stored and requested keys are valid. -/def rootAddress (n : Nat) (positive : 0 < n) (x : Nat) : Fin n :=  ⟨x % n, Nat.mod_lt x positive⟩
defMultipleTrees.fallbackTreepropext · Quot.soundAlgorithms/MultipleTrees/Definitions/Abstract.lean:14
/-- The shared fallback contains every universe key with a uniformly balancedshape. Frequencies are unnecessary for this tree. -/def fallbackTree (n : Nat) : LabelledTree Nat := cacheTree n n (fun _ => 0)
defMultipleTrees.buildpropext · Quot.soundAlgorithms/MultipleTrees/Definitions/Abstract.lean:18
/-- Preprocess a frequency matrix into local successor caches and one sharedfallback. Only trees and root pointers are stored; the frequency matrix is nota field of the resulting runtime store. -/def build (n capacity : Nat) (positive : 0 < n) (frequency : Nat → Nat → Nat) :    LinkedSearch.Store (Fin n) where  roots p := LinkedSearch.attach (rootAddress n positive) (cacheTree n capacity (frequency p))  fallback := LinkedSearch.attach (rootAddress n positive) (fallbackTree n)

build n capacity positive frequency is the whole structure, for a universe of n≥1n \ge 1 keys and a full transition matrix frequency. The local tree of pp is cacheTree for row pp. The fallback is a local tree with capacity nn and all frequencies zero: it holds every key, in a balanced shape. LinkedSearch.attach fills in the successor pointers during preprocessing: a node holding key yy points to the local tree of yy. Local trees are addressed by Fin n, and rootAddress converts a key to its address; the reduction modulo nn only makes the function total, since every stored key is already less than nn. The matrix is an argument of build, not a field of the store.

Theorem (space). The store has exactly nmin⁡(c,n)+nn \min(c, n) + n nodes and 4(nmin⁡(c,n)+n)+n+14(n\min(c, n) + n) + n + 1 words. For c≥1c \ge 1, that is at most 10nc10nc words.

theoremMultipleTrees.Results.nodespropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Abstract.lean:11
/-- The local trees plus the single shared fallback have this exact node count. -/theorem nodes (n capacity : Nat) (positive : 0 < n) (frequency : Nat → Nat → Nat) :    (build n capacity positive frequency).nodes = n * min capacity n + n :=  build_nodes n capacity positive frequency
theoremMultipleTrees.Results.wordspropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Abstract.lean:16
/-- Four fields per node and one cell per root; no frequency matrix is retained. -/theorem words (n capacity : Nat) (positive : 0 < n) (frequency : Nat → Nat → Nat) :    (build n capacity positive frequency).words = 4 * (n * min capacity n + n) + n + 1 :=  build_words n capacity positive frequency
theoremMultipleTrees.Results.spacepropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Abstract.lean:21
theorem space (n capacity : Nat) (positive : 0 < n) (frequency : Nat → Nat → Nat)    (nonempty : 0 < capacity) : (build n capacity positive frequency).words ≤ 10 * n * capacity :=  build_space n capacity positive frequency nonempty

Each node has four words: its key, its successor pointer and two children. The directory has one word per local tree, and one more word points to the fallback.

28.5 Serving one request

Theorem (query semantics). Starting from the local tree of any key pp, the executable query for any key xx is a legal request in the linked-search model, ending at xx’s local tree, at the cost it reports.

theoremMultipleTrees.Results.querypropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Abstract.lean:25
/-- The cursor advances by stored links, ending at the requested key's local root. -/theorem query (n capacity : Nat) (positive : 0 < n) (frequency : Nat → Nat → Nat) (p x : Fin n) :    LinkedSearch.Request (build n capacity positive frequency) p x x      (LinkedSearch.request (build n capacity positive frequency) p x).2 :=  request_execution n capacity positive frequency p x

LinkedSearch.request is the executable query and LinkedSearch.Request the relational semantics of Chapter 11; the theorem says they agree on this store, and that the cursor reaches xx, so the next request can start from there.

Theorem (worst case). For c≥1c \ge 1, every request, whatever its frequency, costs at most log⁡2(2min⁡(c,n))+log⁡2(2n)+4\log_2(2\min(c, n)) + \log_2(2n) + 4.

theoremMultipleTrees.Results.uniformpropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Abstract.lean:31
/-- Worst-case query cost includes a possible failed cache search and fallback. -/theorem uniform (n capacity : Nat) (positive : 0 < n) (frequency : Nat → Nat → Nat)    (nonempty : 0 < capacity) (p x : Fin n) :    ((LinkedSearch.request (build n capacity positive frequency) p x).2 : ℝ) ≤      Real.logb 2 (2 * ((min capacity n : Nat) : ℝ)) + Real.logb 2 (2 * (n : ℝ)) + 4 :=  request_uniform n capacity positive frequency nonempty p x

This is the cost of a failed local search, the fallback link, a full search of the balanced fallback and the final successor link. It is the bound for transitions that the preprocessing never saw.

Theorem (one transition). If 1≤c≤n1 \le c \le n and the transition p→xp \to x has Fpx>0F_{px} > 0, the request for xx from pp costs at most

6 (1+log⁡2RpFpx) 1+log⁡2n1+log⁡2c.6\,\Bigl(1 + \log_2 \frac{R_p}{F_{px}}\Bigr)\,\frac{1 + \log_2 n}{1 + \log_2 c}.
theoremMultipleTrees.Results.tradeoffpropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Abstract.lean:38
/-- A normalized per-transition information bound, including capacity one. -/theorem tradeoff (n capacity : Nat) (positive : 0 < n) (frequency : Nat → Nat → Nat)    (nonempty : 0 < capacity) (fits : capacity ≤ n) (p x : Fin n) (observed : 0 < frequency p x) :    ((LinkedSearch.request (build n capacity positive frequency) p x).2 : ℝ) ≤      6 * (1 + Real.logb 2 ((ConditionalEntropy.rowTotal n frequency p : ℝ) / frequency p x)) *        (1 + Real.logb 2 n) / (1 + Real.logb 2 capacity) :=  request_tradeoff n capacity positive frequency nonempty fits p x observed

Here RpR_p is ConditionalEntropy.rowTotal. The proof has two cases. On a hit, the cost is a weighted search in the local tree plus a link, O(1+log⁡2(Rp/Fpx))O(1 + \log_2(R_p/F_{px})), and the factor (1+log⁡2n)/(1+log⁡2c)(1 + \log_2 n)/(1 + \log_2 c) is at least 11. On a miss, the cost is O(1+log⁡2n)O(1 + \log_2 n), but by the selection theorem log⁡2c≤log⁡2(Rp/Fpx)\log_2 c \le \log_2(R_p / F_{px}), so (1+log⁡2(Rp/Fpx))/(1+log⁡2c)≥1(1 + \log_2(R_p/F_{px}))/(1 + \log_2 c) \ge 1. The constant 66 covers both cases. The normalization by 1+log⁡1 + \log instead of log⁡\log keeps the statement meaningful for c=1c = 1 and for deterministic transitions, where Rp=FpxR_p = F_{px}.

28.6 The tradeoff

Summing the one-transition bound over the sequence gives Theorem 9. The store is built from the sequence’s own transition matrix, so every transition actually performed has positive count. And the sum of 1+log⁡2(Rxj−1/Fxj−1xj)1 + \log_2(R_{x_{j-1}} / F_{x_{j-1} x_j}) over all requests is m+I(X)m + I(X), because the transition a→ba \to b occurs FabF_{ab} times.

Theorem 9 (space/query tradeoff). Let n≥1n \ge 1, 1≤c≤n1 \le c \le n, p<np < n, and let XX be a sequence of keys less than nn. The store built with capacity cc from the transition counts of XX, starting from pp, serves XX from pp at cost at most 6 (m+I(X)) (1+log⁡2n)/(1+log⁡2c)6\,(m + I(X))\,(1 + \log_2 n)/(1 + \log_2 c), and uses at most 10nc10nc words.

theoremMultipleTrees.Results.sequencepropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Abstract.lean:46
/-- Theorem 9 of arXiv:1304.6897 in the explicit static linked-pointer model.The empirical matrix is used for preprocessing only. Cost includes one unitper visited node and each fallback/successor link traversal. Request normalizationand `1 + log` factors cover deterministic rows, empty sequences, and capacity one.The initial predecessor is explicit; preprocessing time is not part of the bound. -/theorem sequence (n capacity : Nat) (positive : 0 < n)    (nonempty : 0 < capacity) (fits : capacity ≤ n) (p : Fin n) (xs : List (Fin n)) :    ∃ q k,      LinkedSearch.Execution (build n capacity positive (LazyFinger.frequency p (xs.map Fin.val)))        p (xs.map Fin.val) q k ∧      (k : ℝ) ≤ 6 * ConditionalEntropy.cost n p (xs.map Fin.val) *        (1 + Real.logb 2 n) / (1 + Real.logb 2 capacity) ∧      (build n capacity positive (LazyFinger.frequency p (xs.map Fin.val))).words ≤ 10 * n * capacity :=  sequence_tradeoff n capacity positive nonempty fits p xs

The sequence is a list of Fin n, so its validity is part of its type; xs.map Fin.val is the same list of natural numbers. The theorem asserts an execution of the store, with some final cursor q and cost k, not just a number: every request in it is a legal linked search.

Two capacities are worth spelling out. With c=nc = n, every observed successor is stored, the store has O(n2)O(n^2) words and the cost is at most 6(m+I)6(m + I), within a constant of the benchmark. With c=1c = 1, the store has O(n)O(n) words and the cost is O((m+I)log⁡n)O((m + I)\log n). In between, the factor is (1+log⁡2n)/(1+log⁡2c)≈log⁡cn(1 + \log_2 n)/(1 + \log_2 c) \approx \log_c n.

Theorem (polynomial space, Section 7 of the paper). Let n≥1n \ge 1 and ε>0\varepsilon > 0. There is a capacity cc with 1≤c≤n1 \le c \le n, depending only on nn and ε\varepsilon, such that for every start pp and every sequence XX, the store built with capacity cc from the transition counts of XX serves XX from pp at cost at most 6min⁡(ε,1) m (1+H)\frac{6}{\min(\varepsilon,1)}\, m\,(1 + H), and uses at most 20 n1+ε20\, n^{1+\varepsilon} words.

theoremMultipleTrees.Results.polynomial_spacepropext · Classical.choice · Quot.soundAlgorithms/MultipleTrees/Abstract.lean:61
/-- For each fixed positive exponent, Section 7's structure uses`O(n^(1+ε))` words and `O(m(1+H))` query work. The integer capacity is chosenbefore the sequence; preprocessing the actual trees still uses its empiricalfrequencies. The constants are explicit, including exponents greater than one.Initialization uses the specified predecessor and preprocessing time is excluded. -/theorem polynomial_space (n : Nat) (positive : 0 < n) (ε : ℝ) (exponent : 0 < ε) :    ∃ capacity : Nat, 0 < capacity ∧ capacity ≤ n ∧      ∀ (p : Fin n) (xs : List (Fin n)), ∃ q k,        LinkedSearch.Execution          (build n capacity positive (LazyFinger.frequency p (xs.map Fin.val)))          p (xs.map Fin.val) q k ∧        (k : ℝ) ≤ (6 / min ε 1) * (xs.length : ℝ) *          (1 + ConditionalEntropy.entropy n p (xs.map Fin.val)) ∧        ((build n capacity positive (LazyFinger.frequency p (xs.map Fin.val))).words : ℝ) ≤          20 * (n : ℝ) ^ (1 + ε) :=  MultipleTrees.polynomial_space n positive ε exponent

The proof takes the capacity ⌈nmin⁡(ε,1)⌉\lceil n^{\min(\varepsilon, 1)} \rceil, so log⁡2c≥min⁡(ε,1)log⁡2n\log_2 c \ge \min(\varepsilon, 1) \log_2 n and the ratio of logarithms is at most 1/min⁡(ε,1)1 / \min(\varepsilon, 1). The cap at 11 reflects that a capacity of nn already stores every successor: for ε>1\varepsilon > 1, the space bound 20 n1+ε20\,n^{1+\varepsilon} is valid but weaker than the O(n2)O(n^2) actually used. The cost is written with the average conditional entropy H=I/mH = I/m, which ConditionalEntropy.entropy totalizes to 00 for the empty sequence, so m(1+H)=m+Im(1 + H) = m + I.

Reading the Lean
  • The capacity is chosen before the sequence is known, but the trees are not: they are built from the transition counts of XX itself, as in the paper. Both theorems are offline statements about a structure tuned to the sequence it serves, like the optimal static trees of Chapter 27.
  • The cost counts node visits and pointer links in the linked-search model. Building the store is not counted, and the transition matrix is not part of the space.
  • The benchmark is normalized as m+I=m(1+H)m + I = m(1 + H), with one unit per request, so a deterministic sequence, with H=0H = 0, still costs Θ(m)\Theta(m).