Chapter 25 · GreedyASS · Section 25.1The greedy sweep
Joan Lucas (1988) proposed an offline BST algorithm now called Greedy Future, and Ian Munro (2000) rediscovered it. To serve a request, touch its search path, then rearrange the path so that the keys on it that will be requested soonest are nearest the top. The rule is simple and locally sensible, it performs very well in experiments, and it is conjectured to be within a constant factor of the offline optimum.
Demaine, Harmon, Iacono, Kane and Pătraşcu (2009) found that in the geometric view of Chapter 9, Greedy Future no longer needs the future. Its touched points are produced by a sweep that looks only at the past. When request arrives at time , add the point , and for every old point whose rectangle with contains no other old point, add its projection onto row . Every rectangle that would otherwise be empty receives the cheapest possible repair. Combined with their simulation theorem, which turns any online geometric algorithm into an online BST algorithm at a constant-factor cost (Section 25.2), this makes Greedy an online BST algorithm. Greedy and splay trees are the two main candidates for a dynamically optimal BST algorithm.
This section defines the sweep and proves two things about it. First, it is a legal online geometric algorithm: every row it adds keeps the point set arborally satisfied. Second, it is greedy in the strongest sense: each row it adds is contained in every legal row, so it is the unique smallest one. That second fact is the section’s main result.
Let be an arborally satisfied set of points on keys , all at times before , and let be the request at time . Call a set of keys a legal row if , every key of is less than , and together with the points is still arborally satisfied. Then every legal row contains the sweep’s row, which is together with the keys of the old points visible from . The sweep’s row is itself legal, so it is the unique legal row of minimum size.
In Lean, greedy_required is the containment, and greedy_minimum says that a set of keys is a minimum-size legal row (Geometric.GreedyRow) exactly when it equals Geometric.greedySweep.
The Lean statements
/-- The minimum-cardinality completion is unique and equals the sweep. -/theorem greedy_minimum {n time request : Nat} {P : Finset Point} {keys : Finset Nat} (history : History n time P) (satisfied : ASS P) (valid : request < n) : GreedyRow n time request P keys ↔ keys = greedySweep time request P := … by constructor · exact greedy_unique history satisfied valid · rintro rfl exact sweep_greedy history satisfied validModels.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.Algorithm/-- Every legal completion contains GreedyASS's points. -/theorem greedy_required {n time request : Nat} {P : Finset Point} {keys : Finset Nat} (history : History n time P) (legal : Completion n time request P keys) : greedySweep time request P ⊆ keys := … sweep_subset history legalModels.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.AlgorithmThe definitions are in Algorithms/GreedyASS/Algorithm/Abstract.lean and the theorems in Algorithms/GreedyASS/Abstract.lean.
25.1.1 The sweep
/-- An old point whose rectangle with the new request has no third old point. -/def Visible (P : Finset Point) (time request : Nat) (q : Point) : Prop := q.1 ≠ request ∧ ∀ p ∈ P, p ≠ q → ¬ Rectangle q (request, time) pAn old point q is visible from the request (request, time) when it is in a different column and no other old point lies in the closed rectangle spanned by q and the request. Only the old set P is consulted, so all visible points are found at once, before the new row is built.
- Old points in the request’s own column are never visible. Their projection would be the request, which is added anyway.
- “No other point” includes the boundary: an old point on the edge of the rectangle, or at its far corner, hides
q. - Only the latest point in a column can be visible, because any earlier point in that column has the later one inside its rectangle with the request.
instance (P : Finset Point) (time request : Nat) (q : Point) : Decidable (Visible P time request q) := inferInstanceAs (Decidable (_ ∧ ∀ p ∈ P, _ → ¬ _))Visibility is decidable because P is finite, so the sweep is an executable filter:
/-- The usual GreedyASS sweep: project every old point making an emptyrectangle with the request onto the new row, and include the request itself. -/def greedySweep (time request : Nat) (P : Finset Point) : Finset Nat := insert request ((P.filter (Visible P time request)).image Prod.fst)greedySweep time request P is the set of keys of the new row: the request, together with the keys of all visible old points. Its arguments are exactly those of an online step in the sense of Geometric.OnlineAlgorithm: the time, the request and the past points.
The figure runs the sweep. At each step the shaded rectangles are the empty ones, between the new request and each visible old point, and the dashed lines are the projections that fill them.
On the bit-reversal sequence GreedyASS adds points. On the sequential sequence , typed into the figure, each row is the request and its predecessor, points in total: a linear cost for a sequential scan, as for splay trees (Section 24.3.4).
25.1.2 A legal online algorithm
The sweep always produces a legal row.
Theorem (the sweep is legal). Let be an arborally satisfied set of points with keys less than and times less than , and let . Then the sweep’s row at time contains , uses only keys less than , and together with the new row is arborally satisfied.
/-- The standard empty-rectangle sweep completes the current row. -/theorem greedy_step {n time request : Nat} {P : Finset Point} (history : History n time P) (satisfied : ASS P) (valid : request < n) : Completion n time request P (greedySweep time request P) := sweep_correct history satisfied validModels.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.AlgorithmThe proof checks every new rectangle, between an old point and a new point, by cases on where the request lies relative to it. The visible points are exactly what is needed to fill the rectangles that the request alone would leave empty.
So the sweep is an online geometric algorithm, with this theorem as its certificate:
/-- GreedyASS as an online geometric algorithm: each row is the sweep, whichsees only the request and the points so far, never the future. -/def greedyASS (n : Nat) : OnlineAlgorithm n where step := greedySweep correct := fun _ _ _ history satisfied valid => sweep_correct history satisfied validgreedyASS packages the sweep with this certificate as an online geometric algorithm. Its step sees only the time, the request and the past points, so it is online by construction, exactly as GreedyASS should be. The certificate holds for every satisfied history, not only for histories produced by GreedyASS itself.
25.1.3 Each row is the smallest possible
Every legal row must contain the sweep’s row.
Theorem (the sweep’s points are forced). Let be a set of points with keys less than and times less than , and let be any legal row for request at time . Then contains every key of the sweep’s row.
/-- Every legal completion contains GreedyASS's points. -/theorem greedy_required {n time request : Nat} {P : Finset Point} {keys : Finset Nat} (history : History n time P) (legal : Completion n time request P keys) : greedySweep time request P ⊆ keys := sweep_subset history legalModels.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.AlgorithmThe argument is short. Suppose q = is visible from the request but the legal row omits . The row contains , so among the new points between and , take the one closest to , say . The rectangle of q and lies inside the rectangle of q and the request, so it contains no other old point by visibility, and no other new point by the choice of . Since and , it is nondegenerate, so the completed set is not arborally satisfied, a contradiction. The theorem needs only the History hypothesis, which makes old points strictly earlier than the new row.
Together with greedy_step, the sweep’s row is a legal row contained in every legal row. It is therefore the unique legal row of minimum size, which is the specification Geometric.GreedyRow of Chapter 9.
Theorem (the sweep is the minimum row). Let be an arborally satisfied set of points with keys less than and times less than , and let . A set of keys is a legal row of minimum size for request at time if and only if it is the sweep’s row.
/-- The minimum-cardinality completion is unique and equals the sweep. -/theorem greedy_minimum {n time request : Nat} {P : Finset Point} {keys : Finset Nat} (history : History n time P) (satisfied : ASS P) (valid : request < n) : GreedyRow n time request P keys ↔ keys = greedySweep time request P := by constructor · exact greedy_unique history satisfied valid · rintro rfl exact sweep_greedy history satisfied validModels.Geometric.Definitions, Models.Geometric.Online, Algorithms.GreedyASS.Algorithmgreedy_minimum is a statement about one row, given the past. It does not say that the whole GreedyASS solution has minimum size among offline solutions (Results.optimal_exists); adding more points early can make later rows cheaper. The library does not claim that GreedyASS is optimal, or within a constant factor of optimal. That is the conjecture.
25.1.4 A brute-force version of the specification
GreedyRow can also be computed directly from its definition, by trying all subsets of keys:
/-- Keep the smaller set, retaining the first in a tie. -/def smaller (a b : Finset Nat) : Finset Nat := if b.card < a.card then b else a/-- Enumerate all subsets of the key universe. -/def subsets : Nat → List (Finset Nat) | 0 => [∅] | n + 1 => subsets n ++ (subsets n).map (insert n)/-- An executable exhaustive implementation of the minimum-row rule.Only geometric point cost is studied here; enumeration time is exponential. -/def greedy (n time request : Nat) (P : Finset Point) : Finset Nat := ((subsets n).filter fun keys => decide (Completion n time request P keys)).foldl smaller (Finset.range n)subsets n lists all subsets of , greedy keeps those that are legal completions (decidable thanks to the instances of Chapter 9), and the fold returns a smallest one, starting from the full key set. This is exponential and only meant for small cases. The library’s tests evaluate it by decide, for instance
example : greedy 4 2 2 {(1, 0), (1, 1), (3, 1)} = {1, 2, 3} := by decide
The sweep is the efficient implementation, and greedy_minimum says that the two agree whenever the history is satisfied. The figure’s JavaScript port of the sweep is tested against the Lean sweep, and also against a port of greedy on all small cases.
25.1.5 From points to a search tree
GreedyASS produces points, not rotations. Section 25.2 turns any online geometric algorithm into one online BST algorithm, fixed before the request sequence, whose cost is at most a constant times the number of points plus a term linear in (Results.online_bst). Applied to Geometric.greedyASS, it gives an online BST algorithm whose total cost is . Any future bound on the number of GreedyASS points, such as the conjectured comparison with the offline optimum, would therefore transfer to binary search trees.
Fox (2011) proved that GreedyASS satisfies an access lemma analogous to that of splay trees, which gives it the logarithmic amortized bound. Many further adaptive properties have since been established for it. None of these are formalized in the library.
One family of adaptive bounds for GreedyASS is formalized, however. Section 25.3 to Section 25.6 prove the theorems of Iacono and Langerman (2016): on every sequence, the number of points GreedyASS adds is at most a constant times the cost of the best static tree searched with a lazy finger, plus . Equivalently, it has the weighted dynamic finger property, and in particular the dynamic finger property. Through the simulation, the same bounds hold for an online BST algorithm.