Chapter 9The geometric view
Run a BST algorithm on a request sequence and record, for each request, the set of nodes it touches. Plot each touched key at time as the point , with keys on the horizontal axis and time going upward. Demaine, Harmon, Iacono, Kane and Pătraşcu (2009) observed that the point sets arising this way have a purely geometric description. Whenever two points do not share a row or a column, the rectangle they span contains a third point. Conversely, every point set with this property, which they call arborally satisfied, is the touch set of some BST execution. The cost of an execution is its number of points.
This turns BST optimization into a covering problem in the plane. The offline optimum for a request sequence is, up to a constant factor, the size of the smallest arborally satisfied superset of the request points . Lower bounds become statements about rectangles, and Wilber’s bound of Chapter 17 is proved here against every arborally satisfied set. Algorithms become rules that complete one row at a time, and the most natural such rule, GreedyASS (Section 25.1), is conjectured to be dynamically optimal.
This chapter states the model: points and arborally satisfied sets, offline solutions, and online algorithms that add one row per request. Its theorems are small facts about these definitions. The result that makes the geometric view worth having, that point counts and BST costs agree up to constant factors, is the key result of Chapter 12, and every definition it needs is in this chapter or in Part I. The definitions are in Models/Geometric/Definitions/Abstract.lean, Online/Abstract.lean, Offline/Abstract.lean and Static/Abstract.lean, and the theorems in Models/Geometric/Abstract.lean. The correspondence with the tree models is Chapter 12.
9.1 Points and rectangles
/-- A key and a time, both numbered from zero. -/abbrev Point := Nat × Nat/-- The closed axis-aligned rectangle with opposite corners `a` and `b`. -/def Rectangle (a b p : Point) : Prop := min a.1 b.1 ≤ p.1 ∧ p.1 ≤ max a.1 b.1 ∧ min a.2 b.2 ≤ p.2 ∧ p.2 ≤ max a.2 b.2A point is a pair (key, time) of natural numbers. Keys are inorder ranks, as in every other model, and times number the requests from . Rectangle a b p says that p lies in the closed axis-parallel rectangle with opposite corners a and b; the corners need not be the lower left and upper right ones.
/-- Arboral satisfaction: every nondegenerate rectangle spanned by two pointscontains a third point, possibly on its boundary. -/def ASS (P : Finset Point) : Prop := ∀ a ∈ P, ∀ b ∈ P, a.1 ≠ b.1 → a.2 ≠ b.2 → ∃ p ∈ P, p ≠ a ∧ p ≠ b ∧ Rectangle a b pA finite point set P is arborally satisfied when every pair of points a, b of P with different keys and different times spans a rectangle containing a third point of P. The third point may lie on the boundary, including at one of the two other corners.
- Pairs on a common row or column impose nothing:
a.1 ≠ b.1 → a.2 ≠ b.2 → …. Their “rectangle” is a segment. p ≠ a ∧ p ≠ bexcludes the two corners themselves, and nothing else. A point at the corner satisfies the rectangle.- Pairs are ordered, so each rectangle is checked twice. This is harmless.
Since P is a finite set, every quantifier in ASS ranges over a finite set, and the library makes this explicit with decidability instances. They let small cases be checked by decide, and they make the brute-force GreedyASS of Section 25.1 executable.
instance (a b p : Point) : Decidable (Rectangle a b p) := inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _))instance (P : Finset Point) : Decidable (ASS P) := inferInstanceAs (Decidable (∀ a ∈ P, ∀ b ∈ P, _ → _ → ∃ p ∈ P, _ ∧ _ ∧ _))instance (n time request : Nat) (P : Finset Point) (keys : Finset Nat) : Decidable (Completion n time request P keys) := inferInstanceAs (Decidable (_ ∧ _ ∧ _))9.1.1 An example
Serve the requests on four keys with the fixed tree whose root is , with left child , right child , and the left child of . Each search touches its root-to-key path: at time , then , then , then . These are points, which is the fixed-tree cost StaticTree.cost of the sequence: one unit per request plus its depth.
The request points alone, , are not arborally satisfied: the rectangle of and is empty. The touched points are. In the figure, the “Requests only” and “GreedyASS” buttons load the two sets (GreedyASS happens to produce exactly the touches of this tree). Click two points to inspect their rectangle, or click cells to add and remove points; the panel finds an empty rectangle whenever there is one.
9.2 Why tree executions are satisfied
Here is the classical argument for a fixed tree, where each request touches its search path. Take two touched points and with and , and let be the lowest common ancestor of and , so . If , then is a proper ancestor of , so it was touched at time , and is a third point in the rectangle. If , then is an ancestor of , so was touched and lies in the rectangle. For trees that change over time the same argument works, because the lowest common ancestor of two keys can only change at a time when it is touched.
The library states the fixed-tree case for any family of touched sets that are rooted and connected in each row:
/-- In each row, the touched keys form a rooted connected region of one fixedBST. The root has absolute rank `lo + left.size`; subtrees stay unlabelled. -/def RootedRows : Tree → Nat → Finset Point → Prop | .nil, _, P => P = ∅ | .node l r, lo, P => (∀ p ∈ P, (lo + l.size, p.2) ∈ P) ∧ RootedRows l lo (P.filter fun p => p.1 < lo + l.size) ∧ RootedRows r (lo + l.size + 1) (P.filter fun p => lo + l.size < p.1)RootedRows t lo P reads the tree top-down. At a node whose key is = lo + l.size, every point of P (all of which have keys in this subtree) forces into P: if anything below or at this node is touched at some time, the node itself is touched at that time. The points with keys below must satisfy the same condition in the left subtree, and those above in the right subtree. The empty tree admits no points.
Keys outside the tree’s range are ruled out, not ignored. A point with key below lo is passed to the left subtree at every level, and eventually reaches a nil, where P = ∅ fails. The same holds for keys at or beyond lo + t.size.
Theorem (fixed trees give satisfied sets). For every tree t, offset lo and finite point set P, if RootedRows t lo P holds then P is arborally satisfied.
/-- The geometric view of rooted touches in a fixed BST is ASS. -/theorem fixed_tree_view (t : Tree) (lo : Nat) (P : Finset Point) (rooted : RootedRows t lo P) : ASS P := rootedRows_ass t lo P rootedModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Models.Geometric.StaticThe proof is the induction above: a pair on the same side of the root is handled by the subtree, and a pair separated by the root, or containing it, finds a root point on one of its two rows. The dynamic version of the argument is part of the equivalence theorems of Chapter 12.
9.3 Offline solutions
Requests are the points :
/-- The access-sequence point set, starting at the supplied time. -/def requestsAt : Nat → List Nat → Finset Point | _, [] => ∅ | time, x :: xs => insert (x, time) (requestsAt (time + 1) xs)requestsAt time xs places the -th request on row time + j. With time = 0, request is the point . Repeated keys are allowed; they lie in different rows.
/-- An offline geometric solution includes all requests and is arborallysatisfied. Its cost is `points.card`; the initial BST convention is separate. -/structure Solution (requests : Finset Point) where points : Finset Point contains : requests ⊆ points satisfied : ASS pointsA solution for a set of requests is an arborally satisfied superset of them. Its cost is its number of points.
/-- A bounded offline geometric execution of the given access sequence.The entire set may depend on future requests. -/structure Offline (n : Nat) (xs : List Nat) extends Solution (requestsAt 0 xs) where bounded : History n xs.length pointsAn offline solution for keys and the sequence xs is a solution for requestsAt 0 xs whose points all have keys below and times below = xs.length. The set is chosen with the whole sequence in view, hence “offline”. The bound rules out extra columns, which would be keys that do not exist, and extra rows, which would be work done after the last request.
OfflineextendsSolution, soE.points,E.containsandE.satisfiedare its fields, together withE.bounded.- No initial tree is mentioned. A geometric solution corresponds to a BST execution from an initial tree of its choosing. Starting from a prescribed tree costs an extra , and the theorems of Chapter 12 state this term explicitly.
Two facts about offline solutions. Every solution contains the request points, so its cost is at least , just as the total cost of a BST execution is at least the number of requests.
Theorem (requests are paid for). Every offline solution for a sequence of requests has at least points.
/-- Every solution contains the request points, so it has at least `m` points. -/theorem request_lower_bound {n : Nat} {xs : List Nat} (E : Offline n xs) : xs.length ≤ E.points.card := offline_lower_bound EModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Models.Geometric.StaticTheorem (an optimum exists). For every sequence xs of keys below , there is an offline solution whose number of points is at most that of every offline solution for keys and xs.
/-- A minimum offline geometric solution exists for every valid sequence. -/theorem optimal_exists {n : Nat} (xs : List Nat) (valid : ∀ x ∈ xs, x < n) : ∃ E : Offline n xs, ∀ F : Offline n xs, E.points.card ≤ F.points.card := offline_minimum xs validModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Models.Geometric.StaticExistence of some solution is the only content, since costs are natural numbers. The full grid is a solution: every nondegenerate rectangle with corners on the grid contains its two other corners. The library instead runs the GreedyASS sweep of Section 25.1 and takes the least cost with Nat.find. The minimum cost is the geometric counterpart of the offline BST optimum.
9.4 Row by row
A BST algorithm serves requests in order, so its points arrive one row at a time. Rows are built from sets of keys:
/-- Put a finite set of keys on one time row. -/def row (time : Nat) (keys : Finset Nat) : Finset Point := keys.image fun x => (x, time)/-- A history over `n` keys, strictly before the next time `time`. -/def History (n time : Nat) (P : Finset Point) : Prop := ∀ p ∈ P, p.1 < n ∧ p.2 < timerow time keys puts the keys on one row. History n time P says that P uses only the keys and only rows strictly before time: it describes what has happened before the current request.
/-- A legal online extension: include the request, use only valid keys, andsatisfy all rectangles after adding points on this row. -/def Completion (n time request : Nat) (P : Finset Point) (keys : Finset Nat) : Prop := request ∈ keys ∧ keys ⊆ Finset.range n ∧ ASS (P ∪ row time keys)A legal row for the request at time time contains the request, uses only existing keys, and keeps the whole set arborally satisfied once it is added. The condition is checked on the union P ∪ row time keys, so a new row must satisfy the rectangles it forms with every old point, not only among its own points. Old rows cannot change.
Among legal rows, the smallest one is a natural choice:
/-- A minimum-cardinality legal extension of the current row. This specifiesthe greedy geometric algorithm independently of its implementation. -/def GreedyRow (n time request : Nat) (P : Finset Point) (keys : Finset Nat) : Prop := Completion n time request P keys ∧ ∀ other, Completion n time request P other → keys.card ≤ other.cardGreedyRow specifies the greedy rule by a property: a legal row of minimum cardinality. It does not say that such a row is unique, or how to find it. Section 25.1 proves that it is unique and is computed by a simple sweep.
9.4.1 Online algorithms
/-- An online geometric algorithm sees only the old point set and the currentrequest. It must complete the current row without changing past rows. -/structure OnlineAlgorithm (n : Nat) where step : Nat → Nat → Finset Point → Finset Nat correct : ∀ time request P, History n time P → ASS P → request < n → Completion n time request P (step time request P)An online geometric algorithm is a function from the current time, the current request and the points so far to the keys of the new row, with a certificate. Whenever the history is bounded and arborally satisfied and the request is valid, the row is legal. The step receives no future requests, and this is the only sense of “online”.
- The certificate must hold for every satisfied history, not only for those the algorithm itself produces. This is stronger than necessary, and GreedyASS meets it.
- The step sees the past points but not the past request sequence. It cannot tell which point of an old row was the request. For GreedyASS this does not matter.
- Computation is uncharged, as in Section 5.1. Only points count.
/-- Run from a supplied history, numbering new rows consecutively. -/def fill (step : Nat → Nat → Finset Point → Finset Nat) : Nat → Finset Point → List Nat → Finset Point | _, P, [] => P | time, P, x :: xs => fill step (time + 1) (P ∪ row time (step time x P)) xs/-- Charge exactly one unit for every point added, including each request. -/def rowCost (step : Nat → Nat → Finset Point → Finset Nat) : Nat → Finset Point → List Nat → Nat | _, _, [] => 0 | time, P, x :: xs => (step time x P).card + rowCost step (time + 1) (P ∪ row time (step time x P)) xsfill step time P xs runs a step function from history P, numbering new rows consecutively from time. rowCost charges one unit for each point added, the request included, so it is the geometric analogue of the total cost .
An online run is an offline solution, and its cost is exactly its number of points.
Theorem (online runs are solutions). For every online geometric algorithm A on keys and every sequence xs of keys below , the points that A produces on xs, starting from the empty history, form an offline solution with exactly rowCost points.
/-- Online row cost is exactly the cardinality of the resulting ASS solution. -/theorem online_solution {n : Nat} (A : OnlineAlgorithm n) (xs : List Nat) (valid : ∀ x ∈ xs, x < n) : ∃ E : Offline n xs, E.points = fill A.step 0 ∅ xs ∧ E.points.card = rowCost A.step 0 ∅ xs := offline_of_online A xs validModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Models.Geometric.StaticThe cardinality claim needs a short argument: each row is added at a fresh time, so no two rows share a point.
Conversely, every offline solution can be read row by row as a sequence of legal moves:
/-- The rows strictly preceding `time`. -/def pastPoints (P : Finset Point) (time : Nat) : Finset Point := P.filter fun p => p.2 < time/-- The keys touched at a given time. -/def keysAt (P : Finset Point) (time : Nat) : Finset Nat := (P.filter fun p => p.2 = time).image Prod.fstpastPoints P time is the part of P strictly before time, and keysAt P time the keys on row time.
Theorem (rows of a solution are legal). If a finite point set P is arborally satisfied, has keys below and contains the request point at time time, then the row of P at time is a legal completion of the rows of P before it.
/-- Conversely, an offline ASS solution can be revealed in legal rows.The choice of those rows may depend on the future. -/theorem offline_row {n time request : Nat} {P : Finset Point} (bounded : ∀ p ∈ P, p.1 < n) (satisfied : ASS P) (req : (request, time) ∈ P) : Completion n time request (pastPoints P time) (keysAt P time) := offline_completion bounded satisfied reqModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Models.Geometric.StaticThe reason is that arboral satisfaction is inherited by every prefix of the rows. A rectangle spanned by two points at times at most has all its points at times at most , so the third point it contains is in the prefix too. The rows of an offline solution are legal, but each may have been chosen with knowledge of later requests, and an online algorithm cannot reproduce them in general.
Reading an offline solution row by row also accounts for all of its points. Here scheduleCost P time adds the row sizes of the first time rows.
Theorem (rows account for every point). For every offline solution of a sequence of requests, the sizes of its first rows add up to its number of points.
/-- The number of points in the first `time` rows, computed row by row. -/def scheduleCost (P : Finset Point) : Nat → Nat | 0 => 0 | time + 1 => scheduleCost P time + (keysAt P time).cardtheorem offline_cost {n : Nat} {xs : List Nat} (E : Offline n xs) : scheduleCost E.points xs.length = E.points.card := bounded_schedule_cost E.boundedModels.Rotation, Models.Geometric.Definitions, Models.Geometric.Online, Models.Geometric.Offline, Models.Geometric.StaticFor an offline solution all rows are below , so the first rows hold every point.
Online and offline geometric algorithms now mirror online algorithms and offline executions in the BST models. Chapter 12 makes the correspondence precise: offline, point counts and BST costs agree up to constant factors (exactly, for the region model), and online, every geometric algorithm can be simulated by an online BST algorithm. The simulation is the subject of Section 25.2, and GreedyASS, the geometric algorithm it is usually applied to, is Section 25.1.