Documentation

CslibFrontier.DataStructure.VanEmdeBoas.Basic

Recursive van Emde Boas sets #

The universe at depth d is [0, 2 ^ (2 ^ d)). Each internal node stores its minimum outside its clusters, caches its maximum, and records nonempty clusters in a recursive summary. This is the recursive variant presented in Demaine's lecture notes, not the stratified-tree implementation of van Emde Boas, Kaas and Zijlstra. The lecture notes credit Michael Bender for a 2001 personal communication.

The functions here are Lean algorithms. Immutable function updates, pattern matching and recursive calls have no certified machine cost. The separate word-RAM realization must charge instructions, initialization and memory before any machine running-time claim is made.

Main definitions #

References #

The extrema use one shared presence bit. A present pair stores minimum and maximum.

Cached extrema; none denotes an empty node.

Cached minimum, without a search through clusters.

Cached maximum, without a search through clusters.

Change only the local extrema. A caller must justify the children and summary invariant.

The fully allocated empty tree. Allocation cost is not measured in this Lean definition.

Set membership. On valid trees, arguments outside the universe are rejected.

Insert a bounded key. Repeated insertion of the local minimum does no recursive work. For an empty cluster, its first element is installed directly and only the summary recurses.

The least stored key strictly greater than the query, or none if it does not exist. The cached maximum chooses between one cluster recursion and one summary recursion.

Mathematical interpretation. The minimum is absent from the cluster interpretation.

The local extrema correctly describe a bounded set of keys.

Recursive invariant: bounded extrema, valid children and summary, exact summary occupancy, and strict exclusion of the local minimum from every cluster.

@[simp]

The empty tree at depth d has no cached bounds.

@[simp]

The empty tree at depth d has no cached minimum.

@[simp]

The empty tree at depth d has no cached maximum.

@[simp]

The empty tree at depth d contains no elements.

The representation invariant has a concrete instance at every depth.

@[simp]

Membership lookup on the empty tree at depth d always returns false.

@[simp]

Successor lookup on the empty tree at depth d always returns none.

Every valid tree has correctly bounded extrema.

Stored keys lie in the advertised bounded universe.

An empty local header has no represented elements.

A node that represents some key has an occupied local header.

theorem Cslib.DataStructure.VanEmdeBoas.Tree.Valid.extrema_of_bounds {d : ℕ} {t : Tree d} (h : t.Valid) {m M : ℕ} (hb : t.bounds = some (m, M)) :
m ≤ M ∧ M < universeSize d ∧ t.Contains m ∧ t.Contains M ∧ ∀ (x : ℕ), t.Contains x → m ≤ x ∧ x ≤ M

The extrema of an occupied valid node: its cached bounds (m, M) are ordered, lie in the universe, are represented, and bound every represented key.

theorem Cslib.DataStructure.VanEmdeBoas.Tree.Valid.contains_minimum {d : ℕ} {t : Tree d} (h : t.Valid) {m M : ℕ} (hb : t.bounds = some (m, M)) :

An occupied valid node has a represented cached minimum.

theorem Cslib.DataStructure.VanEmdeBoas.Tree.Valid.contains_maximum {d : ℕ} {t : Tree d} (h : t.Valid) {m M : ℕ} (hb : t.bounds = some (m, M)) :

An occupied valid node has a represented cached maximum.

theorem Cslib.DataStructure.VanEmdeBoas.Tree.cluster_coordinates {u i y : ℕ} (hu : 0 < u) (hy : y < u) :
(i * u + y) / u = i ∧ (i * u + y) % u = y

Division and remainder recover a cluster's coordinates for bounded low parts.

theorem Cslib.DataStructure.VanEmdeBoas.Tree.index_lt_index {u i j a b : ℕ} (ha : a < u) (hij : i < j) :
i * u + a < j * u + b

Every key of an earlier cluster precedes every key of a later one: with low parts below u, the cluster index is the high digit of i * u + a.

theorem Cslib.DataStructure.VanEmdeBoas.Tree.index_le_index {u i j a b : ℕ} (ha : a < u) (hij : i ≤ j) (hab : i = j → a ≤ b) :
i * u + a ≤ j * u + b

Packed keys are ordered when the first cluster index is at most the second and the low parts are ordered if the cluster indices coincide.

theorem Cslib.DataStructure.VanEmdeBoas.Tree.cluster_lt_split {d : ℕ} {i j : Fin (universeSize d)} {a b : ℕ} (hb : b < universeSize d) (h : ↑i * universeSize d + a < ↑j * universeSize d + b) :
↑i < ↑j ∨ i = j ∧ a < b

Conversely, comparing two keys compares their clusters first, then their low parts: if i * u + a < j * u + b with b < u, then cluster i precedes cluster j, or they coincide and a < b. Predecessor and successor queries use this to locate the answer's cluster.

Boolean lookup agrees with the mathematical interpretation of every valid tree.