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

Chapter 17Wilber’s interleave bound

The bounds of Chapter 14 are upper-bound benchmarks: explicit functions of the request sequence that a good algorithm should match. To show that an algorithm is competitive, one needs the other side as well, a lower bound on what every BST execution must pay. Dynamic optimality compares an algorithm with the offline optimum, and nobody knows how to compute that optimum in closed form. Wilber (1989) gave two lower bounds on it that are explicit functions of the sequence. Fifteen years later, at FOCS 2004, Tango trees were built around the first one (Demaine et al. 2007), the first O(log⁡log⁡n)O(\log \log n)-competitive BST.

This chapter defines that first bound, the interleave bound. Fix any BST PP on the nn keys, called the reference tree, and count how often the requests alternate between the left and right side of each of its nodes. Wilber’s theorem says that every BST execution pays at least a constant fraction of this count. The chapter states the bound against the three cost models of Part I: arborally satisfied point sets, rotation executions and pointer executions. It then explains the proof, which uses Wilber’s transition points.

Key result · Wilber 1989
Wilber’s interleave bound is a lower bound on every BST execution

Let PP be any binary search tree on nn keys, and let IBP(X)\mathrm{IB}_P(X) be the total number of left/right alternations of the sequence XX at the nodes of PP (defined in Section 17.1). Every execution that serves X=x1,…,xmX = x_1, \dots, x_m by rotations, starting from any initial tree on nn keys and using rr rotations in total, satisfies

IBP(X)  ≤  4r+2m.\mathrm{IB}_P(X) \;\le\; 4r + 2m.

So the offline optimum costs at least IBP(X)/4−m/2\mathrm{IB}_P(X)/4 - m/2 rotations, for every choice of PP.

In Lean, the execution is an Execution from Part I and IBP\mathrm{IB}_P is Wilber.interleave. The reference tree only needs the right size; it is unrelated to the trees the execution passes through.

The Lean statement
theoremWilber.Results.rotation_lower_boundpropext · Classical.choice · Quot.soundBounds/Interleave/Abstract.lean:15
/-- Lower bound against every offline execution in the simplified BST model. -/theorem rotation_lower_bound (reference : Tree) {s t : Tree} {xs : List Nat} {r : Nat}    (size : reference.size = s.size) (execution : RotationBST.Execution s xs t r) :    interleave reference xs ≤ 4 * r + 2 * xs.length := …  Wilber.interleave_le_rotations reference size execution
11 definitions in 4 chapters · reading list

The definitions follow the convention of Demaine, Harmon, Iacono and Pătraşcu, written DHIP below. They are in Bounds/Interleave/Definitions/Abstract.lean. The theorems are in Bounds/Interleave/Abstract.lean.

17.1 The interleave quantity

Fix a reference tree PP on the same nn keys as the BST being analysed. It is a thought experiment only: no algorithm needs to build it, and any shape will do. For a node yy of PP, its subtree covers an interval of keys, which yy splits into two regions:

  • the left region: yy itself and its left subtree;
  • the right region: its right subtree.

Restrict the request sequence XX to the keys of yy’s subtree, and label each remaining request LL or RR according to its region. The number of alternations at yy is the number of adjacent pairs of labels that differ. The interleave quantity IBP(X)\mathrm{IB}_P(X) is the sum of the alternations over all nodes yy of PP.

Take the perfect reference tree on the seven keys 0,…,60, \dots, 6 (root 33, children 11 and 55) and the sequence 0 4 2 6 1 5 30\ 4\ 2\ 6\ 1\ 5\ 3.

  • Root 33. It sees every request. Its labels are L R L R L R LL\,R\,L\,R\,L\,R\,L: the last request is 33 itself, which belongs to its left region. That gives 66 alternations.
  • Node 11. It sees 0,2,10, 2, 1, labelled L R LL\,R\,L: 22 alternations.
  • Node 55. It sees 4,6,54, 6, 5, also L R LL\,R\,L: 22 alternations.
  • Leaves. Each leaf sees only its own key, which is in its left region, so it never alternates.

So IBP(X)=10\mathrm{IB}_P(X) = 10. The figure computes the same quantity for any sequence. Step through the requests, or click nodes to append requests.

The sequence above is the bit-reversal permutation, Wilber’s standard hard example: list 0,…,2k−10, \dots, 2^k - 1 in the order of their reversed binary representations. On the perfect tree with 2k−12^k - 1 keys it makes nearly every node alternate on nearly every request it sees. The interleave quantity is 1010 for n=7n = 7, 3434 for n=15n = 15, 258258 for n=63n = 63 and 81948194 for n=1023n = 1023, about (log⁡2(n+1)−2) n(\log_2 (n+1) - 2)\, n. Requesting the same keys in increasing order gives only (n−1)/2(n-1)/2. With the lower bound below, this shows that every BST needs Ω(nlog⁡n)\Omega(n \log n) operations on the bit-reversal permutation, while a sequential scan is cheap (Section 24.3.4).

17.1.1 In Lean

The definition recurses on the reference tree. As everywhere in the library, keys are inorder ranks, and a subtree works in its own local ranks 0,1,…0, 1, \dots:

defWilber.regionsno axiomsBounds/Interleave/Definitions/Abstract.lean:17
/-- The reference-tree convention of Demaine–Harmon–Iacono–Pătraşcu:the node itself belongs to its left region. `true` denotes that region. -/def regions (leftSize size : Nat) (xs : List Nat) : List Bool :=  (xs.filter (· < size)).map (· ≤ leftSize)
defWilber.leftRequestsno axiomsBounds/Interleave/Definitions/Abstract.lean:22
def leftRequests (leftSize : Nat) (xs : List Nat) : List Nat :=  xs.filter (· < leftSize)
defWilber.rightRequestsno axiomsBounds/Interleave/Definitions/Abstract.lean:25
def rightRequests (leftSize : Nat) (xs : List Nat) : List Nat :=  (xs.filter (leftSize < ·)).map (· - leftSize - 1)
defWilber.alternationsno axiomsBounds/Interleave/Definitions/Abstract.lean:28
/-- Number of switches between consecutive nonempty region observations. -/def alternations : List Bool → Nat  | [] => 0  | [_] => 0  | a :: b :: xs => (if a = b then 0 else 1) + alternations (b :: xs)
defWilber.interleaveno axiomsBounds/Interleave/Definitions/Abstract.lean:34
/-- Wilber's interleave quantity for an arbitrary fixed unlabelled referenceBST. This definition alone does not assert the BST lower-bound theorem. -/def interleave : Tree → List Nat → Nat  | .nil, _ => 0  | .node l r, xs => alternations (regions l.size (l.size + 1 + r.size) xs) +      interleave l (leftRequests l.size xs) + interleave r (rightRequests l.size xs)

At a node node l r the local rank of the node itself is l.size. Its subtree contains the ranks below size = l.size + 1 + r.size.

  • regions leftSize size xs keeps the requests below size and maps each to a Boolean, true for the left region (x ≤ leftSize, the node included) and false for the right.
  • leftRequests passes the requests strictly below the node to the left subtree, unchanged.
  • rightRequests passes those strictly above it to the right subtree, translated by leftSize + 1 into the right subtree’s local ranks.
  • alternations counts adjacent unequal pairs. Lists of length at most one have none.
  • interleave t xs adds the alternations at the root to those of the two subtrees on their filtered sequences.
Reading the Lean
  • Only the first bound. The docstring of interleave says it plainly: the definition alone asserts nothing about BSTs. The lower bound is a separate theorem.
  • Which region holds the node. Placing yy in its left region (x ≤ leftSize) is DHIP’s convention. A request for yy is seen by yy and its ancestors, but by none of its descendants.
  • The filter in regions. · < size matters only at the root, where it discards out-of-range requests. Further down, leftRequests and rightRequests have already restricted the sequence to the subtree. The theorems below never rely on it, since they all quantify over executions that can only exist for valid sequences.
  • Arbitrary reference trees. interleave takes any Tree, balanced or not. A path gives a small bound; balanced trees give the useful ones.

The bound can also be computed one request at a time. Appending a request xx adds a label at every node on the search path to xx in PP, and costs one alternation wherever the new label differs from the previous one. A node’s first label costs nothing:

defWilber.switchno axiomsBounds/Interleave/Definitions/Abstract.lean:41
/-- A first observation initializes a preference, and does not count as a switch. -/def switch (past : List Bool) (next : Bool) : Nat :=  match past.getLast? with  | none => 0  | some previous => if previous = next then 0 else 1

switch past next is 11 exactly when past has a last element and it differs from next. In the proofs, alternations (bs ++ [b]) = alternations bs + switch bs b. Tango’s vocabulary in Section 26.1 is built on this. There, the last label at a node is its preferred child, and a switch is a change of preferred child. The interleave quantity is exactly the total number of such changes over the whole sequence (Results.interleave_switches).

17.2 The lower bound

Wilber’s theorem: for every reference tree PP, every BST execution serving XX costs at least a constant fraction of IBP(X)\mathrm{IB}_P(X). The library proves it first in the geometric model of Chapter 9, where the cost of a solution is its number of points, and transfers it to the two BST models through the equivalences of Chapter 12.

Theorem (interleave bound, geometric form). Let reference be any tree on n keys. Every arborally satisfied superset E of the request points of xs over n keys has at least half as many points as the interleave quantity: IB(xs)≤2 ∣E∣\mathrm{IB}(xs) \le 2\,|E|.

theoremWilber.Results.interleave_lower_boundpropext · Classical.choice · Quot.soundBounds/Interleave/Abstract.lean:9
/-- Wilber's interleave quantity lower-bounds every ASS solution. The referencetree is arbitrary; it need not be balanced. -/theorem interleave_lower_bound {n : Nat} {xs : List Nat} (reference : Tree)    (size : reference.size = n) (E : Geometric.Offline n xs) :    interleave reference xs ≤ 2 * E.points.card := Wilber.interleave_le_ass reference size E

The two BST versions follow by composing with the conversions from executions to point sets. A rotation execution with rr rotations yields a solution with at most 2r+m2r + m points (Results.rotations_to_ass), and a pointer execution of cost cc yields one with at most 6c6c points (Results.pointers_to_ass).

Theorem (interleave bound, BST forms). Let PP be any tree on nn keys and XX a sequence of mm requests. Every rotation execution serving XX from a tree on nn keys with rr rotations satisfies IBP(X)≤4r+2m\mathrm{IB}_P(X) \le 4r + 2m. Every pointer-machine execution serving XX from a tree on nn keys at cost cc satisfies IBP(X)≤12c\mathrm{IB}_P(X) \le 12c.

theoremWilber.Results.rotation_lower_boundpropext · Classical.choice · Quot.soundBounds/Interleave/Abstract.lean:15
/-- Lower bound against every offline execution in the simplified BST model. -/theorem rotation_lower_bound (reference : Tree) {s t : Tree} {xs : List Nat} {r : Nat}    (size : reference.size = s.size) (execution : RotationBST.Execution s xs t r) :    interleave reference xs ≤ 4 * r + 2 * xs.length :=  Wilber.interleave_le_rotations reference size execution
theoremWilber.Results.pointer_lower_boundpropext · Classical.choice · Quot.soundBounds/Interleave/Abstract.lean:21
/-- Lower bound against conventional pointer-and-rotation executions. -/theorem pointer_lower_bound (reference : Tree) {s t : Tree} {xs : List Nat} {cost : Nat}    (size : reference.size = s.size) (execution : Pointer.Execution s xs t cost) :    interleave reference xs ≤ 12 * cost := Wilber.interleave_le_pointers reference size execution

Both quantify over all executions Execution and Pointer.Execution from every initial tree s. They therefore bound the offline optimum, which is the least cost of such an execution.

Reading the Lean
  • Two unrelated trees. The reference tree only has to have the right size (reference.size = s.size). It is unrelated to the initial tree s of the execution, and to every tree the execution passes through.
  • No additive term. DHIP state the bound as OPT≥IB/2−n\mathrm{OPT} \ge \mathrm{IB}/2 - n. The versions here have no −n-n, because the potential used in the proof (below) is zero before the first request.
  • Constants. The factors 22, 44, 1212 come from the proof’s 22 per touched node and the conversion constants of Chapter 12. They are not claimed to be sharp.

The bound is used through division. If an algorithm costs at most c⋅(IBP(X)+m)c \cdot (\mathrm{IB}_P(X) + m) plus a startup term, then it is O(c)O(c)-competitive, because the optimum is at least max⁡(m,IBP(X)/2)\max(m, \mathrm{IB}_P(X)/2), up to the constants above. Tango trees achieve this with c=O(log⁡log⁡n)c = O(\log \log n) for a balanced reference tree (Results.competitive, Section 26.2).

17.3 Why it holds: transition points

The proof works in the touched-region model of Chapter 8. It is exactly equivalent to the geometric one (Results.ass_iff_region_execution). There, each request touches a connected region of the current tree that contains the root and the requested key, rearranges it while keeping the hanging subtrees in place, and pays one unit per touched node. The library proves IBP(X)≤2⋅cost\mathrm{IB}_P(X) \le 2 \cdot \mathrm{cost} for every such execution. The geometric statement then follows from the exact equivalence.

17.3.1 The transition point of a cut

Fix a reference node yy with key interval [low,high)[\mathit{low}, \mathit{high}), which it cuts after the key cut=y\mathit{cut} = y. Look at the keys of this interval in the actual tree at some moment. Exactly one of them is shallowest, and it is an ancestor of all the others, since a search for any key of the interval must pass through it. Call it the top of the interval. The top lies on one side of the cut. The transition point of yy is the top of the other side: the shallowest key in the region that does not contain the interval’s top.

The proof defines these in Bounds/Interleave/Transition/Defs.lean. They are proof vocabulary, which no theorem statement mentions, so the book describes them without quoting the Lean. topIn t lo low high searches the tree t, whose inorder interval starts at lo, for the shallowest rank in the half-open interval [low, high). The search goes right while the current node is below low and left while it is at or above high. It stops at the first node inside the interval, or returns none if the interval contains no key. transition t low cut high takes the top of the whole interval. If the top is at most cut, it returns the top of the right part [cut + 1, high), and otherwise the top of the left part [low, cut + 1). The left part includes cut, as in regions.

Three facts about the actual tree make transition points useful. They are proved in Bounds/Interleave/Transition/ and Bounds/Interleave/Wilber/Cut.lean.

  1. Touching an interval means touching its top. A touched region contains the root and is connected, so if it contains a key of an interval, it contains that interval’s top.
  2. An untouched transition point stays put. If a request’s region does not contain yy’s transition point, then after the rearrangement the transition point is the same key. The region only reorders touched nodes and keeps every hanging subtree, so the tops of both sides are unchanged.
  3. Without the transition point, a request lands on the top’s side. If a request inside yy’s interval does not touch the transition point, the requested key is on the same side as the interval’s top. Otherwise, by fact 1, the region would contain the top of the other side, which is the transition point.

The argument is a potential function. For each cut, let the potential be 11 when the last label observed at yy differs from the side of the current top of yy’s interval, and 00 otherwise, including before any observation. Consider a request inside the interval.

  • If it does not touch the transition point, then by fact 3 the new label is the top’s side, so the potential becomes 00. An alternation happens only if the previous label differed from that side, that is, only if the potential was 11. By fact 2 the top’s side does not change. So alternations plus potential do not increase.
  • If it touches the transition point, the alternation count grows by at most 11 and the potential is at most 11. The sum grows by at most 22.

So the interleave quantity plus the total potential grows by at most twice the number of reference nodes whose transition points are touched. The last ingredient is that different cuts have different transition points:

The proof collects them with transitionKeys reference lo actual, which lists the transition points in the tree actual of all cuts of the reference tree, in preorder. A node whose right region is empty has no transition point, since then transition returns none. The proof shows that this list has no duplicates. A touched region of kk nodes therefore contains at most kk transition points, and each request adds at most 2k2k to interleave plus potential. Summing over the execution, and using that the potential starts at 00 and is never negative, gives IBP(X)≤2⋅cost\mathrm{IB}_P(X) \le 2 \cdot \mathrm{cost}.

Where the potential is in the library

The potential and its accounting are internal. They are cutPotential, potential and amortized_step in Bounds/Interleave/Wilber/Cut.lean and Potential.lean, and interleave_le_regions in Bound.lean. None of them occurs in the statements above, which depend only on the definitions displayed in this chapter and the model definitions of Part I. The one-line proofs of those statements name the internal theorems (Wilber.interleave_le_ass and its siblings), which link to the source.

17.3.2 Absolute ranks

The argument is about intervals of the actual tree, which keeps changing. Its keys are global ranks, while interleave translates ranks into each subtree’s local coordinates. The proof therefore uses a restatement in absolute ranks:

defWilber.labelsno axiomsBounds/Interleave/Wilber/Abstract.lean:8
/-- Region observations in absolute (rather than subtree-local) ranks. -/def labels (low cut high : Nat) (xs : List Nat) : List Bool :=  (xs.filter fun x => low ≤ x ∧ x < high).map (· ≤ cut)

labels low cut high xs keeps the requests in [low, high) and labels them x ≤ cut. The proof’s internal absoluteInterleave t lo xs sums the alternations of these labels over the nodes of t, a node at global rank lo + l.size having the interval [lo, lo + l.size + 1 + r.size). Every node sees the whole sequence xs and filters it itself; nothing is translated. For valid sequences the two quantities agree (the internal lemma absoluteInterleave_eq), so the bound proved for absoluteInterleave is the bound for interleave. Tango’s preferred paths are defined from the same absolute labels (Tango.decomposePaths): the last label at a node is its preferred child.

Switch the figure above to preferred paths to preview them. Each node prefers the side of its most recent request, and following preferences from the root down gives the paths that Tango keeps as auxiliary trees.

What is not formalized
  • Only the first bound. Wilber’s second lower bound, sometimes called the funnel bound, is not formalized; it is always at least the first, up to constant factors (Lecomte and Weinstein 2020). Neither are the later geometric lower bounds, such as the independent-rectangle bound of Demaine, Harmon, Iacono, Kane and Pătraşcu (2009).
  • Dynamic finger for splay trees. The dynamic finger bound of Cole et al. (2000) and Cole (2000) is not formalized for splay trees. It is proved for GreedyASS (Section 25.3), as a corollary of the weighted dynamic finger bound.
  • No compute time. Like everything else in the library, the bound counts BST operations only, not the time needed to compute anything.