Chapter 16Where lazy finger stands
Chapter 15 introduced the lazy finger: a static BST in which each search starts where the previous one ended, instead of at the root. This chapter places that model between its two natural neighbours.
- Below it is the classical root-finger static tree, the setting of static optimality (Chapter 14). The tree is fixed and every search starts at the root.
- Above it is the dynamic BST of Part I. It also searches from the root each time, but may rotate.
Theorem 6 of Bose, Douïeb, Iacono and Langerman (2016) says that, up to constant factors, lazy finger is never worse than root finger and never better than a dynamic BST, and that both gaps can be arbitrarily large. The chapter also states the consequences listed in §1.4 of their paper: the optimal lazy-finger tree has static optimality, the static finger property and the dynamic finger property, all at once.
Throughout, is a sequence of keys in (valid requests), and all three costs charge one unit per request plus one per pointer move or rotation. We write
- for the root-finger cost in a tree (
StaticTree.cost); - for the optimal lazy-finger cost over all trees on keys, with the finger initially at the root (
LazyFinger.optimalCost; the minimum is attained); - for the least cost of a
Pointer.Execution, a legal execution of the pointer machine of Chapter 7 that starts each search at the root.
This is Theorem 6 of the paper. Let be a sequence of valid requests on keys.
- Lazy finger beats root finger up to a factor 2. For every tree on keys, .
- A dynamic BST follows lazy finger up to a constant factor. From every initial tree on keys there is a legal pointer-machine execution serving whose cost satisfies $$
- The first gap is unbounded. For every startup allowance there is a family of sequences , of length on keys, such that for every constant , once is large enough, $$
- The second gap is unbounded. For every there is a family on keys such that for every , once is large enough, every initial tree on keys admits a legal execution serving at a cost with $$
Item 2 is an existence statement about executions, so it compares lazy finger with the offline optimum. It does not give an online algorithm, and the execution may depend on the whole sequence. The families in items 3 and 4 are fixed before is chosen, so the ratios tend to infinity along one family: and , even after any one-time startup allowance.
The Lean statements
/-- First inequality of the hierarchy, against every fixed root-finger tree. -/theorem optimal_root_finger (t : Tree) (xs : List Nat) : optimalCost t.size xs ≤ 2 * StaticTree.cost t xs := … optimalCost_le_twice_static t xs/-- The dynamic/lazy inequality of Theorem 6 of arXiv:1304.6897, stated as alegal offline execution witness from any prescribed initial tree. This doesnot assert an online choice of the optimal reference tree. -/theorem optimal (s : Tree) (xs : List Nat) (valid : ∀ x ∈ xs, x < s.size) : ∃ u k, Pointer.Execution s xs u k ∧ k ≤ 16 * s.size + 464 * optimalCost s.size xs := … dynamic_optimal s xs valid/-- One family, fixed before the factor is chosen, has an unbounded separationratio as its sequence length grows. -/theorem family (allowance : Nat → Nat) : ∃ sizes : Nat → Nat, ∃ requests : Nat → List Nat, (∀ m, 0 < sizes m ∧ (requests m).length = m ∧ (∀ x ∈ requests m, x < sizes m)) ∧ ∀ C, ∃ threshold, ∀ m, threshold ≤ m → ∀ root : Tree, root.size = sizes m → C * optimalCost (sizes m) (requests m) + allowance (sizes m) < StaticTree.cost root (requests m) := … optimal_root_lazy_family allowance/-- One family, fixed before the factor and initial tree are chosen, witnessesan unbounded lazy-finger/dynamic separation ratio as length grows. -/theorem family (allowance : Nat → Nat) : ∃ sizes : Nat → Nat, ∃ requests : Nat → List Nat, (∀ m, 0 < sizes m ∧ (requests m).length = m ∧ (∀ x ∈ requests m, x < sizes m)) ∧ ∀ C, ∃ threshold, ∀ m, threshold ≤ m → ∀ s : Tree, s.size = sizes m → ∃ t k, Pointer.Execution s (requests m) t k ∧ C * k + allowance (sizes m) < optimalCost (sizes m) (requests m) := … lazy_dynamic_family allowanceThis is the list of consequences in §1.4 of the paper. For every sequence of valid requests on keys there is a tree on keys whose lazy-finger cost, from the root, is the optimum , and this cost is at most
- , where counts the requests for (static optimality);
- , for every finger (static finger);
- with , for every key (dynamic finger).
The tree may depend on the sequence; it is chosen before and . The term pays once for starting the finger at the root rather than at .
The Lean statement
/-- The three consequences in §1.4 of arXiv:1304.6897 hold for one optimalstatic tree. The choice can depend on the sequence, but precedes quantificationover the fixed finger and predecessor. This is not an online tree-selection rule. -/theorem simultaneous (n : Nat) (xs : List Nat) (valid : ∀ x ∈ xs, x < n) : ∃ t : Tree, t.size = n ∧ cost t xs = optimalCost n xs ∧ (cost t xs : ℝ) ≤ 2 * Bounds.staticCost n xs ∧ (∀ f, (cost t xs : ℝ) ≤ 6 * Bounds.fingerCost f xs) ∧ ∀ p, p < n → (cost t xs : ℝ) ≤ n + 4 * Bounds.dynamicFingerCostFrom p xs := … optimal_three_bounds n xs validTo confirm the results, the two boxes suffice. The rest of the chapter explains them in the order of the comparison chain. Section 16.1 gives an overview of the chain. Section 16.2 proves item 2, by simulating the lazy finger with a pointer machine. Section 16.3 proves item 3 with sequential scans. Section 16.4 counts nearby keys in a fixed tree, and Section 16.5 uses the count to prove item 4 with alternating pairs. Section 16.6 derives the consequences. Item 1 is proved in Chapter 15. The definitions are in Bounds/LazyFinger/{Sequential,Separation,Neighborhood}/Definitions/Abstract.lean, and the theorems in the Abstract.lean files of Bounds/LazyFinger/Dynamic, Sequential, Separation, Neighborhood and Consequences.
16.1 The chain at a glance
All three models use the same keys and charge one unit per request. They differ in where a search starts and whether the tree may change.
| Model | Tree | A search starts at | Cost of a request | Lean |
|---|---|---|---|---|
| Root finger | fixed | the root | StaticTree.cost |
|
| Lazy finger | fixed | the previous request | LazyFinger.cost |
|
| Dynamic BST | rotates | the root | moves and rotations, plus one | Pointer.Execution |
Here is the number of edges on the path between and in , which is LazyFinger.distance. The first search of a lazy finger starts at the root.
The chain reads
where hides the factor and the additive of item 2. In words:
- Lazy finger is at least as good as root finger, up to a factor 2. Going from to through their lowest common ancestor is never longer than going from the root to and then from the root to . Charging each move to one of the two root-to-key paths gives the factor 2. This is
Results.root_fingerfor a fixed tree andResults.optimal_root_fingerfor the optimum, both in Chapter 15. - A dynamic BST is at least as good as lazy finger, up to a constant factor. A dynamic BST could simply keep the tree fixed and walk from one request to the next, but the pointer machine restarts every search at the root. So the simulation has to carry a finger along inside the tree. Section 16.2 shows how.
- Lazy finger can be much better than root finger. A sequential scan costs amortized per request with a lazy finger in every tree, while every root-finger tree pays about per request.
- A dynamic BST can be much better than lazy finger. A sequence that alternates for a long time, for each pair in turn, is cheap for a BST that brings the current pair to the top. A fixed tree, however, must place most pairs far apart.
16.2 A dynamic BST can follow a lazy finger
The goal of this section is item 2 of the headline. Let be a tree and a sequence. The lazy finger walks along the edges of from one request to the next. A pointer machine can perform the same walk, but it must return to the root before each search. The library closes this gap in three steps.
- The lazy-finger distance is exactly the length of a shortest pointer walk (Section 16.2.1).
- A lazy finger is therefore a one-finger execution in the multi-finger model of Chapter 10, at exactly the lazy-finger cost (Section 16.2.2).
- A multi-finger execution with a constant number of fingers can be simulated by a conventional BST at a constant factor. This is the finger hand of Section 35.2. The method is due to Demaine, Iacono, Langerman and Özkan (2013). Optimizing over reference trees then gives the theorem (Section 16.2.3).
16.2.1 Walking the edge distance
The pointer machine’s configurations (Pointer.Config) are a cursor on a node of a tree. Its instructions (Pointer.Step) move the cursor to a parent or child, or rotate. The navigation-only instructions are Pointer.Move, and a sequence of them is a Pointer.Walk; a walk never changes the tree.
Theorem (the distance is walkable). For valid keys of a tree , there are cursors at and at in joined by exactly pointer instructions, and in fact by a navigation-only walk of that length.
/-- Realize the lazy-finger distance by exactly that many pointer instructions.The endpoints are cursors on the fixed reference tree. -/theorem walk (t : Tree) (x y : Nat) (hx : x < t.size) (hy : y < t.size) : ∃ a b : Pointer.Config, a.tree = t ∧ b.tree = t ∧ a.key? = some x ∧ b.key? = some y ∧ Pointer.Trace a b (distance t x y) := pointer_walk t x y hx hy/-- The realizing path uses only navigation, with no intervening rotations. -/theorem readonly (t : Tree) (x y : Nat) (hx : x < t.size) (hy : y < t.size) : ∃ a b : Pointer.Config, a.tree = t ∧ b.tree = t ∧ a.key? = some x ∧ b.key? = some y ∧ Pointer.Walk a b (distance t x y) := readonly_walk t x y hx hyThe first statement uses Pointer.Trace, which allows any instruction, and the second uses a Pointer.Walk. Both fix the tree: a.tree = t and b.tree = t.
Theorem (the distance is shortest). Every navigation-only walk from a cursor at to a cursor at has at least steps. Conversely, from a cursor at some single move reaches a key with .
/-- The edge metric is a lower bound for every navigation-only walk. -/theorem shortest {a b : Pointer.Config} {k : Nat} (walk : Pointer.Walk a b k) (x y : Nat) (keyA : a.key? = some x) (keyB : b.key? = some y) : distance a.tree x y ≤ k := distance_le_walk walk x y keyA keyB/-- A shortest route can be followed one legal parent/child instruction at a time. -/theorem toward (a : Pointer.Config) (x y : Nat) (key : a.key? = some x) (valid : y < a.tree.size) (different : x ≠ y) : ∃ b z, Pointer.Move a b ∧ b.key? = some z ∧ distance b.tree z y + 1 = distance a.tree x y := toward_exists a x y key valid differentSo LazyFinger.distance, defined by recursion on the tree, is the graph metric of the tree as the pointer machine sees it, and it can be followed greedily, one legal move at a time.
16.2.2 One persistent finger
The multi-finger model of Chapter 10 keeps several cursors in one shared tree (Multifinger.State). A request is served by any finger reaching the requested key, and the fingers stay where they are between requests (Multifinger.Execution). With one finger this is exactly a lazy finger.
Theorem (lazy finger is a one-finger execution). Let be a tree, a valid key and a valid sequence. There is a one-finger execution from the state “tree , finger at ” that serves and costs exactly the lazy-finger cost of from . Its final state has the same tree and the finger at some key .
/-- Retaining one finger between requests gives exactly the lazy-finger cost. -/theorem persistent (t : Tree) (p : Nat) (xs : List Nat) (hp : p < t.size) (valid : ∀ x ∈ xs, x < t.size) : ∃ q : Nat, Multifinger.Execution (⟨t, fun _ : Fin 1 => p⟩ : Multifinger.State 1) xs ⟨t, fun _ => q⟩ (costFrom t p xs) := multifinger_execution t p xs hp validThe cost is LazyFinger.costFrom t p xs, one unit per request plus the walked distance, with no rounding. The tree never changes.
16.2.3 From one finger to a conventional BST
The pointer machine restarts each search at the root. To serve the next request it must therefore reach the finger’s position again, cheaply. The finger hand of Section 35.2 does this. It maintains, inside the one physical tree, a compact representation of the paths that join the root to all fingers, the hand. With it, the root reaches any finger, and a finger takes a step, at amortized constant cost for a fixed number of fingers. In the library, a multi-finger execution with fingers and cost becomes a conventional execution of cost at most . For one finger this is , the constant below. When all fingers start at the root, the hand starts in the original tree with zero potential, so no startup term appears.
Theorem (same initial tree). For every tree and valid sequence there is a legal pointer-machine execution from serving at cost at most , where is the lazy-finger cost in from the root.
/-- A conventional dynamic execution from the same initial tree, with constantfactor overhead. Each request starts at the root in the target machine. -/theorem same_tree (t : Tree) (xs : List Nat) (valid : ∀ x ∈ xs, x < t.size) : ∃ u k, Pointer.Execution t xs u k ∧ k ≤ 464 * cost t xs := dynamic_execution t xs validThe comparison is between two executions that start in the same tree. If the dynamic BST may choose its initial tree, it starts in an optimal lazy-finger tree:
/-- When the initial tree may be chosen, there is no startup term. The selectedinitial tree may depend on the whole sequence. -/theorem optimal_tree (n : Nat) (xs : List Nat) (valid : ∀ x ∈ xs, x < n) : ∃ t u k, t.size = n ∧ Pointer.Execution t xs u k ∧ k ≤ 464 * optimalCost n xs := dynamic_optimal_tree n xs validHere the initial tree t is part of the witness, and depends on the whole sequence. Usually the initial tree is prescribed instead. The pointer machine can transform any tree on keys into any other with at most instructions (Results.reshape, Chapter 7), and the library charges that reshaping to the first request:
/-- An arbitrary fixed initial tree, with explicit cost to reshape it to thereference tree. This also includes the empty sequence and empty universe. -/theorem reference (s t : Tree) (xs : List Nat) (same : s.size = t.size) (valid : ∀ x ∈ xs, x < t.size) : ∃ u k, Pointer.Execution s xs u k ∧ k ≤ 16 * s.size + 464 * cost t xs := dynamic_reference_tree s t xs same validThe statement covers the empty sequence and the empty universe, where the execution is empty.
Theorem (dynamic BSTs follow optimal lazy finger, Theorem 6). Let be any tree on keys and a valid sequence. There is a legal pointer-machine execution from that serves at cost .
/-- The dynamic/lazy inequality of Theorem 6 of arXiv:1304.6897, stated as alegal offline execution witness from any prescribed initial tree. This doesnot assert an online choice of the optimal reference tree. -/theorem optimal (s : Tree) (xs : List Nat) (valid : ∀ x ∈ xs, x < s.size) : ∃ u k, Pointer.Execution s xs u k ∧ k ≤ 16 * s.size + 464 * optimalCost s.size xs := dynamic_optimal s xs validThe initial tree is quantified before the optimal lazy-finger tree is chosen inside the proof. The execution reshapes into that tree and then simulates the finger. It therefore knows the whole sequence: the theorem bounds the offline optimum, and is not an online algorithm. When there are at least as many requests as keys, the unit request charges pay for the reshaping, since :
/-- For `m ≥ n`, normalized request charges absorb initialization. -/theorem long_sequence (s : Tree) (xs : List Nat) (valid : ∀ x ∈ xs, x < s.size) (long : s.size ≤ xs.length) : ∃ u k, Pointer.Execution s xs u k ∧ k ≤ 480 * optimalCost s.size xs := dynamic_optimal_long s xs valid long16.3 Sequential scans separate lazy from root finger
The first separation uses the simplest possible sequence: scan the keys in order, and repeat.
/-- Full ordered scans of the key universe, repeated `rounds` times. -/def scans (n : Nat) : Nat → List Nat | 0 => [] | rounds + 1 => List.range n ++ scans n rounds/-- Ordered scans stopped at an exact request count, including a partial last scan. -/def scanPrefix (n length : Nat) : List Nat := scans n (length / n) ++ List.range (length % n)scans n rounds is rounds copies of . scanPrefix n length stops the repetition at exactly length requests: it takes full scans and then the first keys of another. For the division gives , so the prefix is empty, and the validity statement below requires .
theorem prefix_length (n m : Nat) : (scanPrefix n m).length = m := scanPrefix_length n mtheorem prefix_valid (n m : Nat) (positive : 0 < n) : ∀ x ∈ scanPrefix n m, x < n := scanPrefix_valid n m positiveLazy finger scans in linear time. In any fixed tree, an inorder traversal crosses each edge twice, and the jump from the last key back to the first crosses each edge at most once more. With the unit per request, a scan costs at most .
Theorem (scans with a lazy finger). For every tree on keys, rounds scans cost at most with a lazy finger starting at the root, and also from any valid starting key. A scan prefix of length costs at most .
/-- Every fixed tree executes repeated ordered scans in linear lazy-finger cost. -/theorem lazy_scan (t : Tree) (rounds : Nat) : cost t (scans t.size rounds) ≤ 4 * rounds * t.size := scans_cost t rounds/-- The same bound from any valid initial key. -/theorem lazy_scan_from (t : Tree) (p rounds : Nat) (hp : p < t.size) : costFrom t p (scans t.size rounds) ≤ 4 * rounds * t.size := scans_costFrom t p rounds hptheorem lazy_prefix (t : Tree) (m : Nat) (positive : 0 < t.size) (long : t.size ≤ m) : cost t (scanPrefix t.size m) ≤ 8 * (m / t.size) * t.size := scanPrefix_cost t m positive longThe bounds hold for every tree shape, so they bound the optimum too.
Root finger pays the entropy. Each key is requested equally often in scans, so the entropy bound of Chapter 14 is , and every fixed tree pays at least a third of it (Results.entropy_lower_bound).
Theorem (scans with a root finger). For every tree on keys, .
/-- Every root-finger tree pays a logarithmic charge per scan request. -/theorem root_scan (t : Tree) (rounds : Nat) : (rounds * t.size : Nat) * (1 + Real.logb 2 (t.size : ℝ)) ≤ 3 * (StaticTree.cost t (scans t.size rounds) : ℝ) := scans_root_lower t roundsThe gap. The ratio between the two bounds is about , which is larger than any constant once .
Theorem (a strict gap for every factor). Let , and . For any two trees on keys, a lazy finger in the first and a root finger in the second, times the lazy-finger cost of rounds scans is less than the root-finger cost.
/-- A concrete family with any prescribed strict gap, independent of the lazyand root-finger tree shapes, for any positive number of scans. -/theorem strict_gap (C rounds : Nat) (hr : 0 < rounds) (lazy root : Tree) (hl : lazy.size = 2 ^ (12 * C)) (ht : root.size = 2 ^ (12 * C)) : C * cost lazy (scans (2 ^ (12 * C)) rounds) < StaticTree.cost root (scans (2 ^ (12 * C)) rounds) := scans_separate C rounds hr lazy root hl htThe two trees are independent, so neither model gets to see the other’s choice. The next statements turn this into statements about the optimum , with increasingly strong quantifiers.
Theorem (unbounded ratio). For every there is a universe and a valid sequence of length (one scan) such that for every tree on keys. Moreover, for every startup allowance , there is such a sequence of length at least with .
/-- No uniform constant bounds best root-finger cost by best lazy-finger cost. -/theorem unbounded_ratio (C : Nat) : ∃ n xs, 0 < n ∧ xs.length = n ∧ (∀ x ∈ xs, x < n) ∧ ∀ t : Tree, t.size = n → C * optimalCost n xs < StaticTree.cost t xs := root_lazy_unbounded C/-- No universe-dependent startup allowance repairs the missing constant-factorcomparison: a sufficiently long repeated scan defeats it. -/theorem gap_beyond_startup (C : Nat) (allowance : Nat → Nat) : ∃ n xs, 0 < n ∧ n ≤ xs.length ∧ (∀ x ∈ xs, x < n) ∧ ∀ t : Tree, t.size = n → C * optimalCost n xs + allowance n < StaticTree.cost t xs := root_lazy_unbounded_with_startup C allowanceA single sequence for each could be an artefact of a short length. The next two theorems fix a universe for each and then let the length be any large enough : the witness is scanPrefix n m with , and the threshold is about .
Theorem (every long length). For every and every allowance there are and a threshold such that every length has a valid sequence of length with for all trees on keys, and hence with .
theorem all_lengths_shapes (C : Nat) (allowance : Nat → Nat) : ∃ n threshold, 0 < n ∧ n ≤ threshold ∧ ∀ m, threshold ≤ m → ∃ xs, xs.length = m ∧ (∀ x ∈ xs, x < n) ∧ ∀ lazy root : Tree, lazy.size = n → root.size = n → C * cost lazy xs + allowance n < StaticTree.cost root xs := root_lazy_all_lengths C allowancetheorem all_lengths (C : Nat) (allowance : Nat → Nat) : ∃ n threshold, 0 < n ∧ n ≤ threshold ∧ ∀ m, threshold ≤ m → ∃ xs, xs.length = m ∧ (∀ x ∈ xs, x < n) ∧ ∀ root : Tree, root.size = n → C * optimalCost n xs + allowance n < StaticTree.cost root xs := optimal_root_lazy_all_lengths C allowanceTheorem (one family, Theorem 6). For every allowance there is a single family of sequences , one of each length , on universes , such that for every , eventually, for every tree on keys.
/-- One family, fixed before the factor is chosen, has an unbounded separationratio as its sequence length grows. -/theorem family (allowance : Nat → Nat) : ∃ sizes : Nat → Nat, ∃ requests : Nat → List Nat, (∀ m, 0 < sizes m ∧ (requests m).length = m ∧ (∀ x ∈ requests m, x < sizes m)) ∧ ∀ C, ∃ threshold, ∀ m, threshold ≤ m → ∀ root : Tree, root.size = sizes m → C * optimalCost (sizes m) (requests m) + allowance (sizes m) < StaticTree.cost root (requests m) := optimal_root_lazy_family allowanceOn a fixed universe the ratio is bounded: every root-finger tree has depth less than , so . A family with an unbounded ratio therefore needs . The library builds it by diagonalization, from the eventual witnesses of all_lengths. At length it takes the largest factor whose threshold is at most , and uses that factor’s universe and witness. Short lengths use a one-key fallback. Here grows extremely slowly, roughly like for the factor that can afford, but the theorem only asserts existence.
16.4 Neighbourhoods in a fixed tree
The second separation needs a lower bound on lazy-finger distances in every tree. The bound comes from counting: every node of a binary tree has at most three neighbours, so few keys are close to any given key.
/-- Existing ranks at lazy-finger distance at most `radius` from `x`. -/def ball (t : Tree) (x radius : Nat) : Finset Nat := (Finset.range t.size).filter fun y => distance t x y ≤ radiusball t x r is the set of valid ranks with .
Theorem (neighbourhoods are small). For every valid , the ball of radius around has at most keys.
/-- Uniform exponential bound on every fixed tree's lazy-finger neighborhood. -/theorem cardinality (t : Tree) (x r : Nat) (valid : x < t.size) : (ball t x r).card ≤ 7 ^ r := ball_card t x r validThe true bound for a graph of maximum degree three is . The base is cruder, but any fixed base gives the logarithmic lower bounds below, and it changes only constants.
All keys outside the ball are at distance at least . Summing gives the following bounds.
Theorem (distance sums). For valid ,
If , then , so the average distance from is at least . Summing over as well, .
/-- Counting the near ball gives a distance-sum lower bound without asymptotics. -/theorem distance_sum (t : Tree) (x r : Nat) (valid : x < t.size) : (r + 1) * t.size ≤ (r + 1) * 7 ^ r + ∑ y ∈ Finset.range t.size, distance t x y := distance_sum_lower t x r valid/-- Every starting key has average distance at least `(r+1)/2` when the universehas at least `2 * 7^r` keys. -/theorem average (t : Tree) (x r : Nat) (valid : x < t.size) (large : 2 * 7 ^ r ≤ t.size) : (r + 1) * t.size ≤ 2 * ∑ y ∈ Finset.range t.size, distance t x y := average_distance_lower t x r valid large/-- The all-pairs version used for the lazy-finger/dynamic separation. -/theorem pairs (t : Tree) (r : Nat) (large : 2 * 7 ^ r ≤ t.size) : (r + 1) * t.size * t.size ≤ 2 * ∑ x ∈ Finset.range t.size, ∑ y ∈ Finset.range t.size, distance t x y := pair_distance_lower t r largeTaking the largest with , the average distance between two keys of any tree on keys is at least about . Every statement is in exact natural-number arithmetic, without asymptotics.
16.5 Alternating pairs separate dynamic from lazy finger
The second separation uses a sequence with very little to remember at any moment, but too many different moments for one fixed tree.
/-- Repeat an alternating pair. A long block has a two-key working set. -/def pairBlock (x y : Nat) : Nat → List Nat | 0 => [] | rounds + 1 => x :: y :: pairBlock x y rounds/-- Concatenate alternating blocks for an explicit list of ordered pairs. -/def pairBlocks (pairs : List (Nat × Nat)) (rounds : Nat) : List Nat := pairs.flatMap fun pair => pairBlock pair.1 pair.2 rounds/-- All ordered pairs, each occurring in one long alternating block. -/def pairSequence (n rounds : Nat) : List Nat := pairBlocks ((List.range n).flatMap fun x => (List.range n).map fun y => (x, y)) rounds/-- All-pairs blocks followed by a repeated key, giving any prescribed length.The padding key is valid when the universe is nonempty. -/def pairSequenceAtLength (n m : Nat) : List Nat := pairSequence n (m / (2 * n * n)) ++ List.replicate (m % (2 * n * n)) 0pairBlock x y roundsis , withroundscopies of the pair.pairBlocksconcatenates such blocks for a list of pairs.pairSequence n roundsdoes this for all ordered pairs with , in lexicographic order. Pairs with are included and are harmless.pairSequenceAtLength n mtakes rounds and pads with copies of key to reach length exactly . The padding key is valid when .
/-- The witnesses enumerate all ordered pairs in long alternating blocks. -/theorem length (n rounds : Nat) : (pairSequence n rounds).length = 2 * rounds * n * n := pairSequence_length n roundstheorem valid (n rounds : Nat) : ∀ x ∈ pairSequence n rounds, x < n := pairSequence_valid n roundstheorem prescribed_length (n m : Nat) : (pairSequenceAtLength n m).length = m := pairSequenceAtLength_length n mtheorem prescribed_valid (n m : Nat) (positive : 0 < n) : ∀ x ∈ pairSequenceAtLength n m, x < n := pairSequenceAtLength_valid n m positiveLazy finger pays the average distance. In a fixed tree , each repetition in the block of contains the transition , which costs . So the sequence costs at least , whatever the starting finger and the transitions between blocks. The neighbourhood bound gives:
Theorem (lazy-finger lower bound). For every tree on keys, . The padded sequence of length satisfies the same bound with in place of .
/-- Uniform lower bound for every fixed lazy-finger reference tree. -/theorem lazy_lower (t : Tree) (rounds r : Nat) (large : 2 * 7 ^ r ≤ t.size) : rounds * (r + 1) * t.size * t.size ≤ 2 * cost t (pairSequence t.size rounds) := pairSequence_lower t rounds r large/-- Padding preserves the all-pairs lower bound in every static tree. -/theorem prescribed_lower (t : Tree) (m r : Nat) (large : 2 * 7 ^ r ≤ t.size) : (m / (2 * t.size * t.size)) * (r + 1) * t.size * t.size ≤ 2 * cost t (pairSequenceAtLength t.size m) := pairSequenceAtLength_lower t m r largeSince the sequence has requests, this is about per request, in every tree.
A dynamic BST pays a constant. Before the block of , a dynamic BST reshapes its tree so that and both lie at depth at most one. A reshape costs at most instructions. Then every request of the block costs a constant.
Theorem (dynamic upper bound). From every initial tree on keys there is a legal pointer-machine execution of pairSequence n rounds at cost at most . If , the cost is at most , which is per request.
/-- A legal dynamic execution of the same sequence, paying explicitly toreshape between blocks and retaining every key. -/theorem dynamic_upper (s : Tree) (rounds : Nat) : ∃ t k, t.size = s.size ∧ Pointer.Execution s (pairSequence s.size rounds) t k ∧ k ≤ s.size * s.size * (16 * s.size + 6 * rounds) := pairSequence_dynamic s rounds/-- Long blocks make the dynamic execution linear in the number of requests. -/theorem dynamic_linear (s : Tree) (rounds : Nat) (long : 16 * s.size ≤ rounds) : ∃ t k, t.size = s.size ∧ Pointer.Execution s (pairSequence s.size rounds) t k ∧ k ≤ 7 * rounds * s.size * s.size := pairSequence_dynamic_long s rounds longEvery reshape is a real sequence of pointer instructions, charged in full; nothing switches trees for free. The final tree t keeps all keys. For the padded sequence, the repeated key is served after one more reshape, and is charged as well:
/-- Both the repeated-key padding and its physical preparation are charged. -/theorem prescribed_dynamic (s : Tree) (m : Nat) (positive : 0 < s.size) (long : 16 * s.size ≤ m / (2 * s.size * s.size)) : ∃ t k, t.size = s.size ∧ Pointer.Execution s (pairSequenceAtLength s.size m) t k ∧ k ≤ 14 * (m / (2 * s.size * s.size)) * s.size * s.size := pairSequenceAtLength_dynamic s m positive longThe gap. Taking large enough that exceeds a multiple of , and long blocks, gives the separation. The quantifiers are those of the previous section. The optimal lazy-finger tree is chosen after the sequence is known, and the dynamic BST may start from any tree.
Theorem (strict separation). For every there is a universe and a valid sequence of length at least such that from every initial tree on keys there is a legal execution of cost with . The same holds with for any startup allowance .
/-- Strict lazy-finger/dynamic separation (Theorem 6 of arXiv:1304.6897).The quantifiers allow the static optimum to choose its tree after seeing thesequence, and allow any prescribed dynamic initial tree. -/theorem strict (C : Nat) : ∃ n xs, 0 < n ∧ n ≤ xs.length ∧ (∀ x ∈ xs, x < n) ∧ ∀ s : Tree, s.size = n → ∃ t k, Pointer.Execution s xs t k ∧ C * k < optimalCost n xs := lazy_dynamic_unbounded C/-- The gap survives every universe-dependent one-time initialization allowance. -/theorem with_startup (C : Nat) (allowance : Nat → Nat) : ∃ n xs, 0 < n ∧ n ≤ xs.length ∧ (∀ x ∈ xs, x < n) ∧ ∀ s : Tree, s.size = n → ∃ t k, Pointer.Execution s xs t k ∧ C * k + allowance n < optimalCost n xs := lazy_dynamic_unbounded_with_startup C allowanceTheorem (every long length). For every and there are and a threshold such that every length has such a sequence of length exactly .
/-- The strict separation holds at every sufficiently large prescribed length,uniformly over dynamic initial trees and beyond any universe-dependent startup. -/theorem all_lengths (C : Nat) (allowance : Nat → Nat) : ∃ n threshold, 0 < n ∧ n ≤ threshold ∧ ∀ m, threshold ≤ m → ∃ xs, xs.length = m ∧ (∀ x ∈ xs, x < n) ∧ ∀ s : Tree, s.size = n → ∃ t k, Pointer.Execution s xs t k ∧ C * k + allowance n < optimalCost n xs := lazy_dynamic_all_lengths C allowanceTheorem (one family, Theorem 6). For every allowance there is a single family of sequences, one of each length , on universes , such that for every , eventually, every initial tree on keys admits a legal execution of cost with .
/-- One family, fixed before the factor and initial tree are chosen, witnessesan unbounded lazy-finger/dynamic separation ratio as length grows. -/theorem family (allowance : Nat → Nat) : ∃ sizes : Nat → Nat, ∃ requests : Nat → List Nat, (∀ m, 0 < sizes m ∧ (requests m).length = m ∧ (∀ x ∈ requests m, x < sizes m)) ∧ ∀ C, ∃ threshold, ∀ m, threshold ≤ m → ∀ s : Tree, s.size = sizes m → ∃ t k, Pointer.Execution s (requests m) t k ∧ C * k + allowance (sizes m) < optimalCost (sizes m) (requests m) := lazy_dynamic_family allowanceThe family is built by the same diagonalization as in Section 16.3, from the witnesses pairSequenceAtLength n m of all_lengths.
Inside a block only two keys are in use, so almost every request has working-set number at most two. A dynamic BST exploits this. A static tree cannot, because over the whole sequence every pair is used and most pairs are far apart. Once the blocks are long (), the working-set bound of the sequence is while is . So, informally, the optimal lazy-finger tree does not have the working-set property, although it has the three properties of the next section. The library does not state this corollary.
16.6 Consequences for the optimal tree
The characterization of Chapter 15 compares with the weighted dynamic-finger benchmark WeightedFinger.costFrom: one positive weight per key, and a charge of per request, where is the total weight of the keys between and inclusive. Particular weightings give the classical bounds.
Unit weights give dynamic finger. With every weight equal to , the interval mass is and the minimum weight is , so the charge is . That is the dynamic-finger benchmark Bounds.dynamicFingerCostFrom, with the predecessor of the first request given explicitly.
/-- The unit-weight benchmark is exactly the normalized dynamic-finger sum. -/theorem unit_weights (p : Nat) (xs : List Nat) : WeightedFinger.costFrom (fun _ => 1) p xs = Bounds.dynamicFingerCostFrom p xs := unit_weight_cost p xsTheorem (dynamic finger). Let and let be valid. The optimal lazy-finger cost with the finger starting at is at most times the dynamic-finger cost from . With the finger starting at the root, , and if then .
/-- Dynamic finger with the same specified predecessor in both benchmarks. -/theorem dynamic_finger_from (n p : Nat) (xs : List Nat) (hp : p < n) (valid : ∀ x ∈ xs, x < n) : (optimalCostFrom n p xs : ℝ) ≤ 4 * Bounds.dynamicFingerCostFrom p xs := optimal_dynamic_finger_from n p xs hp valid/-- The selected static tree starts at its root; the dynamic-finger benchmarkstarts at `p`. The difference costs at most `n` once. -/theorem dynamic_finger (n p : Nat) (xs : List Nat) (hp : p < n) (valid : ∀ x ∈ xs, x < n) : (optimalCost n xs : ℝ) ≤ n + 4 * Bounds.dynamicFingerCostFrom p xs := optimal_dynamic_finger n p xs hp validtheorem dynamic_finger_long (n p : Nat) (xs : List Nat) (hp : p < n) (valid : ∀ x ∈ xs, x < n) (long : n ≤ xs.length) : (optimalCost n xs : ℝ) ≤ 5 * Bounds.dynamicFingerCostFrom p xs := optimal_dynamic_finger_long n p xs hp valid longThe first bound uses the optimum with a specified start, LazyFinger.optimalCostFrom. The additive in the second pays once for moving the finger from the root to (Results.initialization). The factor in the third absorbs it, since the dynamic-finger cost is at least .
Theorem (static optimality). .
/-- The best lazy-finger tree satisfies the static-optimality entropy bound. -/theorem static_optimality (n : Nat) (xs : List Nat) (valid : ∀ x ∈ xs, x < n) : (optimalCost n xs : ℝ) ≤ 2 * Bounds.staticCost n xs := optimal_static_entropy n xs validThe proof takes a root-finger tree whose cost is at most the entropy bound Bounds.staticCost (Results.entropy_upper_bound), and applies the factor 2 of the hierarchy.
Theorem (static finger). For every , .
/-- Uniform static-finger bound, including fingers outside the key universe. -/theorem static_finger (n f : Nat) (xs : List Nat) (valid : ∀ x ∈ xs, x < n) : (optimalCost n xs : ℝ) ≤ 6 * Bounds.fingerCost f xs := optimal_static_finger n f xs validThe finger is any natural number, even one outside the key universe. The bound follows from static optimality, because the entropy bound is at most three times the static-finger bound Bounds.fingerCost (Chapter 14). Neither of these two bounds needs an additive term.
Theorem (all three at once, §1.4). For every valid there is a tree on keys that attains and satisfies the three bounds, the static-finger bound for every and the dynamic-finger bound for every .
/-- The three consequences in §1.4 of arXiv:1304.6897 hold for one optimalstatic tree. The choice can depend on the sequence, but precedes quantificationover the fixed finger and predecessor. This is not an online tree-selection rule. -/theorem simultaneous (n : Nat) (xs : List Nat) (valid : ∀ x ∈ xs, x < n) : ∃ t : Tree, t.size = n ∧ cost t xs = optimalCost n xs ∧ (cost t xs : ℝ) ≤ 2 * Bounds.staticCost n xs ∧ (∀ f, (cost t xs : ℝ) ≤ 6 * Bounds.fingerCost f xs) ∧ ∀ p, p < n → (cost t xs : ℝ) ≤ n + 4 * Bounds.dynamicFingerCostFrom p xs := optimal_three_bounds n xs validsimultaneous adds nothing beyond the three separate bounds and the attainment of the optimum (Results.optimal_attained). Each bound is about the optimal value , so any optimal tree satisfies all three. What the theorem makes explicit is the quantifier order: one tree, chosen from the sequence, serves every and every . It is not an online rule for choosing the tree.