Chapter 5Online algorithms and cost
An Execution describes every legal way to serve a sequence: it is an offline object that knows the whole sequence in advance. A BST algorithm must decide what to do with each request when that request arrives. This chapter defines online algorithms in the rotation model, their two cost measures, and dynamic optimality. Chapter 7 shows that any such algorithm can be run online on a conventional pointer machine, with constant overhead.
The chapter has no main theorem of its own. It fixes the yardstick for Part IV. The definition of an online algorithm, Algorithm, is what “there is a BST algorithm that…” means in later statements. Its cost, Algorithm.cost or Algorithm.totalCost, is what their bounds measure. And dynamic optimality is the property those bounds approximate. The one theorem shown here, that online runs are offline executions, says the comparison is fair.
5.1 Online algorithms
/-- An online rule sees only past requests, the current tree and current request.For each existing rank, it returns a tree and a certified rotation count. -/structure Algorithm where step : List Nat → Tree → Nat → Tree × Nat correct : ∀ past t x, x < t.size → Access t (step past t x).1 x (step past t x).2An Algorithm is a function together with a certificate.
step past t xreceives the requests served so far (past, oldest first), the current treetand the current requestx. It returns the next tree and a rotation count. It receives nothing about future requests, and this is the only sense in which the algorithm is online.correctcertifies that wheneverxis an existing rank, the returned pair is a legal root access:xends at the root, and the returned count is the exact length of some rotation sequence fromtto the new tree.
- The cost cannot be under-reported.
Accessrequires a rotation sequence of exactly the returned length, so the count is at least the rotation distance between the two trees. An algorithm could report more than it needs to (rotating back and forth), but never less. - No persistent state except the tree, but full history. There is no separate memory field. Any auxiliary state an algorithm might keep, such as counters, preferred children or timestamps, is a function of the request history, which
stepreceives in full. Recomputing it costs nothing, since computation is uncharged: only rotations count. The interface is therefore more permissive than a pointer machine with words per node, which is the right direction for lower bounds and for definitions of optimality. Chapter 13 defines the stricter model, with bounded state and charged computation. Splaying (Section 24.1) ignorespastentirely. - Invalid requests are outside the contract. For
x ≥ t.size,stepmay return anything andcorrectsays nothing. Every theorem about runs carries the hypothesis∀ x ∈ xs, x < t.size.
Running an algorithm threads the tree through the sequence and adds the counts:
def Algorithm.runFrom (a : Algorithm) (past : List Nat) (t : Tree) : List Nat → Tree × Nat | [] => (t, 0) | x :: xs => let (u, k) := a.step past t x let (v, m) := a.runFrom (past ++ [x]) u xs (v, k + m)def Algorithm.cost (a : Algorithm) (t : Tree) (xs : List Nat) : Nat := (a.runFrom [] t xs).2runFrom past t xs serves xs starting from history past. Each request is appended to the history after it is served (past ++ [x]), so the step for the -th request sees exactly the requests before it. cost starts with an empty history and keeps only the total number of rotations.
The simplest instance is moveToRootAlgorithm: it ignores the history and rotates the requested key up one level at a time, the witness used in Chapter 3. It opens Part IV in Chapter 23. Splaying (Section 24.1) is the main example.
A run of an algorithm on valid requests is one of the executions of Chapter 3. Online algorithms are therefore a special case of offline executions, and their cost is at least the offline optimum.
Theorem (online runs are executions). Let be an online algorithm, a tree, and a sequence of valid requests for . Running on from , after any history, is an execution of from whose cost is exactly the number of rotations reports.
/-- The online interface really produces legal executions on valid inputs. -/theorem Algorithm.execution (a : Algorithm) (past : List Nat) (t : Tree) (xs : List Nat) (valid : ∀ x ∈ xs, x < t.size) : Execution t xs (a.runFrom past t xs).1 (a.runFrom past t xs).2 := AccessBridge.run_execution a past t xs validModels/Rotation/Existence/Abstract.lean and its importsThe proof is an induction on xs that applies correct at every step. Sizes are preserved, so validity with respect to the initial tree remains validity at every later step.
5.2 Two cost measures
Algorithm.cost counts rotations only. The conventional cost of a BST algorithm charges each request at least one unit, for the comparison at the node that holds the key:
/-- Conventional cost: one unit per request, plus rotations. -/def Algorithm.totalCost (a : Algorithm) (t : Tree) (xs : List Nat) : Nat := xs.length + a.cost t xsWe write for a.cost t xs, for xs.length and for a.totalCost t xs. The two measures differ in an essential way. A request for the current root costs rotations, so a sequence of such requests has and . A bound of the form therefore cannot be obtained from a bound on when can be smaller than . The bounds in Chapter 14 charge every request at least one unit, so for them the two measures are interchangeable (workingSet_totalCost_iff).
5.3 Dynamic optimality
Sleator and Tarjan (1985) conjectured that splay trees are dynamically optimal: on every sequence, their cost is within a constant factor of the best offline BST algorithm, up to an additive term that depends only on . The library states both versions of this property, one for each cost measure.
/-- Compare against every legal offline execution from the same initial tree. -/def DynamicallyOptimal (a : Algorithm) : Prop := ∃ C D : Nat, ∀ t xs u k, Execution t xs u k → a.cost t xs ≤ C * k + D * t.size/-- Conventional competitiveness; constants are uniform in size and sequence. -/def TotallyDynamicallyOptimal (a : Algorithm) : Prop := ∃ C D : Nat, ∀ t xs u k, Execution t xs u k → a.totalCost t xs ≤ C * (xs.length + k) + D * t.sizeRead the quantifiers in order. There exist constants ; then for every initial tree t, every sequence xs and every offline execution of xs from t with any cost k, the algorithm’s cost on xs from t is at most , where = t.size.
- Comparison against every execution, hence against the optimum. Quantifying over all executions
Execution t xs u kis equivalent to comparing with the least suchk, so the definition never needs to compute the offline optimum. The final treeuof the offline strategy is unconstrained. - Same initial tree. The offline strategy starts from the same
tas the algorithm. The additive term is linear in , as in the usual statement of the conjecture. ByGeometry.reset, allowing the offline strategy to choose its own initial tree would change its cost by at most , so this choice does not matter. - Uniform constants. and are fixed before the tree and the sequence are chosen. In particular, they cannot depend on .
- Invalid sequences are vacuous. If some request is out of range there is no
Executionat all (execution_exists_iff), so the condition holds trivially. DynamicallyOptimalcompares rotation counts , whileTotallyDynamicallyOptimalcompares total costs . The total-cost version is the conventional notion of dynamic optimality, and it is the one that transfers to the pointer machine (Section 7.6).
The rotation-only notion implies the conventional one.
Theorem. Every dynamically optimal algorithm is totally dynamically optimal.
/-- Rotation-only competitiveness implies conventional total-cost competitiveness.The converse is deliberately not asserted: requests can have zero rotations. -/theorem DynamicallyOptimal.total (a : Algorithm) (h : DynamicallyOptimal a) : TotallyDynamicallyOptimal a := BoundsBridge.total_dynamic a hBounds/Normalization/Abstract.lean and its importsThe proof takes the constants : add to both sides. The converse is deliberately not claimed. It holds only if no totally dynamically optimal algorithm exists, which is the open question itself. Suppose a is totally dynamically optimal, and let a' behave like a but, on every request, first rotate a child of the root up and back down. Then a' costs at most , so it is still totally dynamically optimal. But on requests for the root of a tree with at least two nodes, a' performs at least rotations, while the offline execution that does nothing performs . No bound holds for all , so a' is not DynamicallyOptimal.
Neither DynamicallyOptimal nor TotallyDynamicallyOptimal is proved for any algorithm in the library. The dynamic optimality conjecture is open. The theorems that mention it, such as the pointer-machine transfer of Section 7.6, are conditional: if an algorithm satisfies the property, then some consequence follows. Chapter 17 proves Wilber’s lower bound on the offline optimum, and Section 26.2 uses it to show that Tango trees are -competitive, the best ratio known.