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

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 xx arrives at time tt, add the point (x,t)(x, t), and for every old point whose rectangle with (x,t)(x, t) contains no other old point, add its projection onto row tt. 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.

Main result · Demaine et al. 2009
GreedyASS adds the unique smallest legal row

Let PP be an arborally satisfied set of points on keys {0,…,n−1}\{0,\dots,n-1\}, all at times before tt, and let r<nr < n be the request at time tt. Call a set of keys KK a legal row if r∈Kr \in K, every key of KK is less than nn, and PP together with the points K×{t}K \times \{t\} is still arborally satisfied. Then every legal row contains the sweep’s row, which is rr together with the keys of the old points visible from (r,t)(r, t). 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
theoremGeometric.GreedyASS.Results.greedy_minimumpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Abstract.lean:35
/-- 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 valid
theoremGeometric.GreedyASS.Results.greedy_requiredpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Abstract.lean:30
/-- 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 legal
9 definitions in 2 chapters · reading list

The definitions are in Algorithms/GreedyASS/Algorithm/Abstract.lean and the theorems in Algorithms/GreedyASS/Abstract.lean.

25.1.1 The sweep

defGeometric.Visiblepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Algorithm/Abstract.lean:13
/-- 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) p

An 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.

Reading the Lean
  • 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.
instanceGeometric.instDecidableVisiblepropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Algorithm/Abstract.lean:17
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:

defGeometric.greedySweeppropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Algorithm/Abstract.lean:21
/-- 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 0,4,2,6,1,5,3,70, 4, 2, 6, 1, 5, 3, 7 GreedyASS adds 2121 points. On the sequential sequence 0,1,…,70, 1, \dots, 7, typed into the figure, each row is the request and its predecessor, 2m−1=152m - 1 = 15 points in total: a linear cost for a sequential scan, as for splay trees (Section 24.3.4).

The sweep always produces a legal row.

Theorem (the sweep is legal). Let PP be an arborally satisfied set of points with keys less than nn and times less than tt, and let r<nr < n. Then the sweep’s row at time tt contains rr, uses only keys less than nn, and PP together with the new row is arborally satisfied.

theoremGeometric.GreedyASS.Results.greedy_steppropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Abstract.lean:24
/-- 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 valid

The 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:

defGeometric.greedyASSpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Abstract.lean:14
/-- 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 valid
Reading the Lean

greedyASS 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 PP be a set of points with keys less than nn and times less than tt, and let KK be any legal row for request rr at time tt. Then KK contains every key of the sweep’s row.

theoremGeometric.GreedyASS.Results.greedy_requiredpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Abstract.lean:30
/-- 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 legal

The argument is short. Suppose q = (x,t′)(x, t') is visible from the request (r,t)(r, t) but the legal row omits xx. The row contains rr, so among the new points between xx and rr, take the one closest to xx, say (y,t)(y, t). The rectangle of q and (y,t)(y, t) 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 yy. Since y≠xy \ne x and t′<tt' < t, 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 PP be an arborally satisfied set of points with keys less than nn and times less than tt, and let r<nr < n. A set of keys is a legal row of minimum size for request rr at time tt if and only if it is the sweep’s row.

theoremGeometric.GreedyASS.Results.greedy_minimumpropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Abstract.lean:35
/-- 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 valid
Minimal rows, not minimal totals

greedy_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:

defGeometric.smallerpropext · Quot.soundAlgorithms/GreedyASS/Algorithm/Abstract.lean:26
/-- 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
defGeometric.subsetspropext · Quot.soundAlgorithms/GreedyASS/Algorithm/Abstract.lean:30
/-- Enumerate all subsets of the key universe. -/def subsets : Nat → List (Finset Nat)  | 0 => [∅]  | n + 1 => subsets n ++ (subsets n).map (insert n)
defGeometric.greedypropext · Classical.choice · Quot.soundAlgorithms/GreedyASS/Algorithm/Abstract.lean:35
/-- 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 2n2^n subsets of {0,…,n−1}\{0, \dots, n-1\}, 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 nn (Results.online_bst). Applied to Geometric.greedyASS, it gives an online BST algorithm whose total cost is O(GreedyASS points+n)O(\text{GreedyASS points} + n). 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.

Known results

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 O(n)O(n). 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.