Uniform Probing in Open-Addressed Hash Tables #
This file formalizes the classical uniform-probing model of open-addressed hashing from
Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, third edition (MIT Press,
2009), Section 11.4 ("Open addressing", pp. 269–277), realizing the bounded-capacity
insert-only dictionary specification Cslib.DataStructure.Dictionary.spec over m table slots
(2 * n_max ≤ m).
In the textbook uniform-probing ("uniform hashing") model, each key u : Fin U is assigned an
independent uniform random permutation π u : Equiv.Perm (Fin m)
(PMF.uniformOfFintype (Fin U → Equiv.Perm (Fin m))), which may equivalently be sampled lazily on
demand via Cslib.RandomOracle.uniform_interpret_eq_lazy
(CslibFrontier/Languages/Probabilistic/RandomOracle.lean).
Source statements and notation mapping #
In [CLRS09], third edition, Section 11.4:
- An open-address table has
mslots (Fin m) and holdsnstored elements (O.cardor(Dictionary.dom s).card, bounded byn_maxwith2 * n_max ≤ m), giving load factorα = n / m < 1(α ≤ 1 / 2when2 * n ≤ m). - Under uniform hashing (p. 271), each key's probe sequence
h(k, 0), h(k, 1), …, h(k, m - 1)is equally likely to be any of them!permutations of{0, 1, …, m - 1}(Equiv.Perm (Fin m)). Every search or insertion counts all inspected slots along the probe sequence up to and including the stopping slot (unsuccessfulProbes O σ = missedProbes O σ + 1andprobeCount T u σ = unsuccessfulProbes (otherOccupiedSlots T u) σ). - Theorem 11.6 (pp. 274–275) states that in an open-address hash table with load factor
α = n / m < 1, the expected number of probes in an unsuccessful search is at most1 / (1 - α) = m / (m - n)under uniform hashing; its proof establishes the exact occupancy tail productPr{X ≥ i} = ∏_{j = 0}^{i - 2} ((n - j) / (m - j)) ≤ α ^ (i - 1). - Corollary 11.7 (p. 275) states that inserting an element into an open-address hash table with
load factor
α < 1requires at most1 / (1 - α) = m / (m - n)probes on average. - Theorem 11.8 (p. 276) states that when each key in the table is equally likely to be searched
for, the expected number of probes in a successful search is at most
(1 / α) ln(1 / (1 - α)). Its proof first establishes for a fixed stored keyuthat searching forufollows the exact same probe sequence as whenuwas inserted (probeCount_runTable_eq_firstInsert), so ifuwas first inserted after a prefixops₁reaching states₁withi = (Dictionary.dom s₁).cardkeys, the expected number of probes foruis at most1 / (1 - i / m) = m / (m - i)(expect_probeCount_of_mem_dom_le, and≤ 2when2 * n_max ≤ mviaexpect_probeCount_of_mem_dom_le_two). The uniform-key-averaged logarithmic endpoint(1 / α) ln(1 / (1 - α))is not formalized here.
Main definitions #
Cslib.DataStructure.Dictionary.UniformProbing.allOccupied,Cslib.DataStructure.Dictionary.UniformProbing.missedProbesFrom,Cslib.DataStructure.Dictionary.UniformProbing.missedProbes,Cslib.DataStructure.Dictionary.UniformProbing.unsuccessfulProbes,Cslib.DataStructure.Dictionary.UniformProbing.probeSlotIndex: slot-probe predicates, probe counts, and first-available slot index along a probe permutationσ : Equiv.Perm (Fin m).Cslib.DataStructure.Dictionary.UniformProbing.Table,Cslib.DataStructure.Dictionary.UniformProbing.emptyTable,Cslib.DataStructure.Dictionary.UniformProbing.occupiedSlots,Cslib.DataStructure.Dictionary.UniformProbing.otherOccupiedSlots,Cslib.DataStructure.Dictionary.UniformProbing.probeCount: pure open-addressed table ofmslots and per-key probe count.Cslib.DataStructure.Dictionary.UniformProbing.tableQuery,Cslib.DataStructure.Dictionary.UniformProbing.tableInsert,Cslib.DataStructure.Dictionary.UniformProbing.tableStep,Cslib.DataStructure.Dictionary.UniformProbing.runTable,Cslib.DataStructure.Dictionary.UniformProbing.TableRep: open-addressedquery/insertoperations and representation invariant linkingTable U mtoDictionary.State U.
Main results #
Cslib.DataStructure.Dictionary.UniformProbing.toOuterMeasure_allOccupied_eq_prod,Cslib.DataStructure.Dictionary.UniformProbing.toOuterMeasure_allOccupied_le_pow: on a table withn = O.card ≤ moccupied slots, the probability overσ ← PMF.uniformOfFintype (Equiv.Perm (Fin m))that the firstk ≤ mprobes all hitO(k < unsuccessfulProbes O σ) is∏ i ∈ Finset.range k, ((n - i : ℝ≥0∞) / (m - i : ℝ≥0∞)) ≤ ((n : ℝ≥0∞) / (m : ℝ≥0∞)) ^ k([CLRS09], third edition, proof of Theorem 11.6, pp. 274–275).Cslib.DataStructure.Dictionary.UniformProbing.expect_unsuccessfulProbes_le,Cslib.DataStructure.Dictionary.UniformProbing.expect_unsuccessfulProbes_le_two,Cslib.DataStructure.Dictionary.UniformProbing.expect_probeCount_of_eq_none_le,Cslib.DataStructure.Dictionary.UniformProbing.expect_probeCount_of_eq_none_le_two: Theorem 11.6 (pp. 274–275) and Corollary 11.7 (p. 275) of [CLRS09], third edition — an unsuccessful lookup or fresh-key insertion into a table holdingn < mkeys makes at mostm / (m - n)probes in expectation (≤ 2when2 * n ≤ m).Cslib.DataStructure.Dictionary.UniformProbing.TableRep.probeCount_tableInsert_of_isSome,Cslib.DataStructure.Dictionary.UniformProbing.probeCount_runTable_eq_firstInsert,Cslib.DataStructure.Dictionary.UniformProbing.expect_probeCount_of_mem_dom_le,Cslib.DataStructure.Dictionary.UniformProbing.expect_probeCount_of_mem_dom_le_two: pathwise insertion-cost identity and per-key intermediate bound from the proof of Theorem 11.8 (p. 276) of [CLRS09], third edition — a successful lookup of a fixed present keyu ∈ dom smakes the exact same number of probes as the operation that first inserteduafter prefixops₁(reaching states₁), which is at mostm / (m - (Dictionary.dom s₁).card) ≤ 2in expectation when2 * n_max ≤ m(the uniform-key-averaged(1 / α) ln(1 / (1 - α))endpoint of Theorem 11.8 is not formalized here).Cslib.DataStructure.Dictionary.UniformProbing.uniform_interpret_eq_lazy_perm: eager sampling of the independent permutation familyFin U → Equiv.Perm (Fin m)agrees in distribution with lazy on-demand oracle evaluation (RandomOracle.uniform_interpret_eq_lazy).Cslib.DataStructure.Dictionary.UniformProbing.missedProbes_le_card,Cslib.DataStructure.Dictionary.UniformProbing.probeCount_le_card_otherOccupied_add_one,Cslib.DataStructure.Dictionary.UniformProbing.probeCount_le_dom_card_add_one,Cslib.DataStructure.Dictionary.UniformProbing.probeCount_le_add_one,Cslib.DataStructure.Dictionary.UniformProbing.expect_probeCount_le_add_one: deterministic and expected pigeonhole probe-count bounds (probeCount T u (π u) ≤ n_max + 1) for any full-cycle probe permutation familyπ.
Representation space note #
Storing an explicit table of independent permutations in (Equiv.Perm (Fin m)) ^ U requires
U * log₂ (m!) bits (Ω(U) machine words when U = poly(n_max) ≫ n_max). To achieve an
O(n_max)-word self-contained Word-RAM dictionary,
CslibFrontier/DataStructure/Dictionary/WordRAM/UniformProbing.lean pairs a Double Tabulation
primary hash h₁(u) ∈ Fin (2 ^ M) with a Double Tabulation stride index
h₂(u) ∈ Fin (2 ^ (M - 1)), probing with the odd stride 2 * h₂(u) + 1
([CLRS09], third edition, Section 11.4, "Double hashing", pp. 272–274), reusing the
open-addressed table invariants proved here.
References #
Section 1: Permutation probe counts and stopping indices #
Event that the first k slots σ ⟨0, _⟩, ..., σ ⟨k - 1, _⟩ of permutation σ all belong to
the occupied slot set O.
Instances For
Count how many consecutive slots σ ⟨idx, _⟩, σ ⟨idx + 1, _⟩, ... belong to O, for up to
rem steps starting at idx.
Number of consecutive probes σ 0, σ 1, ... that hit O before the first slot outside O.
Total number of slot probes 1 + missedProbes O σ made by an unsuccessful search (or fresh-key
insertion) against occupied slot set O along permutation σ.
For any k ≤ m, at least k probes miss (k ≤ missedProbes O σ) if and only if the first k
slots of σ all belong to O.
For any k ≤ m, an unsuccessful search inspects more than k slots
(k < unsuccessfulProbes O σ) if and only if the first k slots of σ all belong to O.
When O.card < m, not all m slots of σ can belong to O.
When O.card < m, missedProbes O σ < m for every permutation σ.
Exact characterization of missedProbes O σ: for any i₀ : Fin m, missedProbes O σ = i₀.val
if and only if all earlier probes i < i₀ lie in O and σ i₀ ∉ O.
Tail-sum identity on Fin m: when O.card < m, the total number of probes
unsuccessfulProbes O σ equals the number of k ∈ Finset.range m for which allOccupied O k σ
holds.
Set of permutations σ : Equiv.Perm (Fin m) whose first k probes all belong to O.
Exact step recurrence for occPermSet: for any k < m,
(m - k) * (occPermSet O (k + 1)).card = (O.card - k) * (occPermSet O k).card.
Exact textbook product formula ([CLRS09], Proof of Theorem 11.6):
for any occupied slot set O : Finset (Fin m) and k ≤ m, the probability over a uniform random
permutation σ ← PMF.uniformOfFintype (Equiv.Perm (Fin m)) that the first k probes all hit O
(k < unsuccessfulProbes O σ) is ∏ i ∈ Finset.range k, ((O.card - i : ℝ≥0∞) / (m - i : ℝ≥0∞)).
Upper bound (n / m) ^ k on the k-probe tail probability ([CLRS09], Proof of Theorem
11.6): for any occupied slot set O : Finset (Fin m) and k ≤ m, the probability over a uniform
random permutation σ that the first k probes all hit O is at most
((O.card : ℝ≥0∞) / (m : ℝ≥0∞)) ^ k.
Finite geometric sum identity in ℝ≥0∞ without subtraction: for n < m,
∑ k ∈ Finset.range j, (n / m) ^ k + (m / (m - n)) * (n / m) ^ j = m / (m - n).
For n < m, any partial geometric sum ∑ k ∈ Finset.range j, (n / m) ^ k is at most
m / (m - n).
Theorem 11.6 of [CLRS09] (Expected probes in an unsuccessful search):
for any occupied slot set O : Finset (Fin m) with O.card < m, the expected number of probes
unsuccessfulProbes O σ over a uniform random permutation
σ ← PMF.uniformOfFintype (Equiv.Perm (Fin m)) is at most m / (m - O.card).
Half-load corollary of Theorem 11.6 ([CLRS09], Section 11.4):
when 0 < m and 2 * O.card ≤ m (load factor α = O.card / m ≤ 1 / 2), the expected number of
probes in an unsuccessful search is at most 2.
Pure open-addressed hash table of m slots over key universe Fin U, where each slot holds
none (empty) or some (u, v) (occupied by key u with value v).
The initially empty table of m slots.
Total number of slot probes 1 + missedProbes (otherOccupiedSlots T u) σ made when probing
table T for key u along permutation σ.
Open-addressed table representation invariant linking concrete slot array T : Table U m to
abstract dictionary state s : Dictionary.State U under permutation family
π : Fin U → Equiv.Perm (Fin m).
- slot_spec (j : Fin m) (u : Fin U) (v : ℕ) : T j = some (u, v) → s u = some v ∧ ∃ (h : missedProbes (otherOccupiedSlots T u) (π u) < m), j = (π u) ⟨missedProbes (otherOccupiedSlots T u) (π u), h⟩
Every occupied slot
jholds(u, v)withs u = some vat keyu's stopping probe index. - key_spec (u : Fin U) (v : ℕ) : s u = some v → ∃ (h : missedProbes (otherOccupiedSlots T u) (π u) < m), T ((π u) ⟨missedProbes (otherOccupiedSlots T u) (π u), h⟩) = some (u, v)
Every present key
uwiths u = some vis stored at its stopping probe index.
The empty table represents Dictionary.empty U under any permutation family π.
Under TableRep π s T, each key u occupies at most one slot in T.
Under TableRep π s T, the number of occupied slots in T equals (Dictionary.dom s).card.
Under TableRep π s T, (otherOccupiedSlots T u).card = ((Dictionary.dom s).erase u).card.
Under TableRep π s T, (otherOccupiedSlots T u).card ≤ (Dictionary.dom s).card.
When s u = none, no slot in T holds key u, so otherOccupiedSlots T u = occupiedSlots T.
When TableRep π s T and (Dictionary.dom s).card < m, the stopping index for any key u
satisfies missedProbes (otherOccupiedSlots T u) (π u) < m.
Target slot π u ⟨missedProbes (otherOccupiedSlots T u) (π u), _⟩ where key u is found or
inserted when (otherOccupiedSlots T u).card < m.
Under TableRep π s T and hO : (otherOccupiedSlots T u).card < m, T (targetSlot π T u hO)
equals (s u).map (fun v => (u, v)).
Pure open-addressed lookup tableQuery hm π T u when 0 < m: inspects slot
π u ⟨min (missedProbes (otherOccupiedSlots T u) (π u)) (m - 1), _⟩ and returns some v if it
contains (u, v), else none.
Pure open-addressed insertion tableInsert hm π T u v: writes some (u, v) at slot
π u ⟨min (missedProbes (otherOccupiedSlots T u) (π u)) (m - 1), _⟩.
Under TableRep π s T and (otherOccupiedSlots T u).card < m, tableQuery hm π T u = s u.
Inserting (u, v) leaves otherOccupiedSlots for u itself unchanged.
Inserting (u, v) preserves the stopping index missedProbes (otherOccupiedSlots · w) (π w)
for every key w already present in s ((s w).isSome).
Preservation of TableRep under tableInsert: if TableRep π s T and
(otherOccupiedSlots T u).card < m, then tableInsert hm π T u v satisfies
TableRep π (Function.update s u (some v)).
Inserting (u, v) leaves probeCount · u (π u) unchanged.
Probe invariance for present keys ([CLRS09], proof of Theorem 11.8, p. 276):
once a key w is present in s ((s w).isSome), inserting or updating any key (u, v) leaves
the number of probes probeCount · w (π w) made when searching for w unchanged.
Table produced by running ops starting from emptyTable U m.
Under an admissible insertion (Dictionary.spec U V n_max).valid s (.insert u v) with
n_max ≤ m, TableRep π s T and (Dictionary.dom s).card ≤ n_max,
(otherOccupiedSlots T u).card < m holds for the inserted key u.
Any valid operation history preserves TableRep.
Running any valid operation history from emptyTable U m produces a table satisfying
TableRep.
If key w is already present in s, then running any valid continuation ops from (s, T)
preserves probeCount · w (π w).
Prefix + suffix decomposition for ValidHistory and runTableFrom.
Pathwise insertion-cost identity from the proof of Theorem 11.8 of [CLRS09], third edition,
p. 276 (probeCount_runTable_eq_firstInsert):
if ops = ops₁ ++ (.insert u v₀ :: ops₂) is a valid operation history where ops₁ is the prefix
before u is inserted (((Dictionary.spec U V n_max).run (Dictionary.empty U) ops₁).1 u = none),
then the number of probes made when searching for u after the entire history ops equals the
number of probes made when u was first inserted after ops₁.
If key u is never inserted during ops (∀ v, .insert u v ∉ ops), then runTableFrom is
independent of π u: updating π at u leaves the resulting table unchanged.
Theorem 11.6 (pp. 274–275) & Corollary 11.7 (p. 275) of [CLRS09], third edition, over
PMF.uniformOfFintype:
for any valid operation history ops reaching state s with (Dictionary.dom s).card < m and any
absent key u (s u = none), the expected number of probes made by an unsuccessful lookup
query u or fresh-key insertion insert u v on runTable hm π ops over
π ← PMF.uniformOfFintype (Fin U → Equiv.Perm (Fin m)) is at most
m / (m - (Dictionary.dom s).card) (≤ 2 when 2 * (Dictionary.dom s).card ≤ m).
Half-load corollary of Theorem 11.6 & Corollary 11.7 ([CLRS09], third edition, Section 11.4,
pp. 274–276):
when 2 * n_max ≤ m, any unsuccessful lookup or fresh-key insertion after a valid history ops
makes at most 2 probes in expectation over
π ← PMF.uniformOfFintype (Fin U → Equiv.Perm (Fin m)).
Per-key successful-search bound from the proof of Theorem 11.8 of [CLRS09], third edition,
p. 276:
if ops = ops₁ ++ (.insert u v₀ :: ops₂) is a valid operation history where ops₁ is the prefix
before u is first inserted (s₁ u = none), then the expected number of probes made when
searching for the fixed key u after ops equals the expected number of probes when u was first
inserted after ops₁, and is at most m / (m - (Dictionary.dom s₁).card) (this formalizes the
per-key intermediate bound 1 / (1 - i / m) = m / (m - i) for i = (Dictionary.dom s₁).card from
the proof of Theorem 11.8; the uniform-key-averaged (1 / α) ln(1 / (1 - α)) endpoint of Theorem
11.8 is not formalized here).
Half-load per-key successful-search bound from the proof of Theorem 11.8 ([CLRS09], third
edition, Section 11.4, p. 276):
when 2 * n_max ≤ m, any successful lookup of a fixed present key u (first inserted after prefix
ops₁) makes at most 2 probes in expectation over
π ← PMF.uniformOfFintype (Fin U → Equiv.Perm (Fin m)).
Section 4: Lazy Random-Oracle Equivalence (RandomOracle.uniform_interpret_eq_lazy) #
Any adaptive oracle program querying per-key permutations u ↦ π u : Equiv.Perm (Fin m) has the
exact same output distribution whether the permutation table
π ← PMF.uniformOfFintype (Fin U → Equiv.Perm (Fin m)) is sampled eagerly upfront or lazily on
first query via Cslib.RandomOracle.answer.
Section 5: Deterministic Pigeonhole Probe-Count Bounds #
For any occupied slot set O : Finset (Fin m) and permutation σ : Equiv.Perm (Fin m), the
number of consecutive missed probes missedProbes O σ is at most O.card by injectivity of σ.
Deterministic Pigeonhole Probe Bound: for any table T : Table U m, key u : Fin U, and
probe permutation σ : Equiv.Perm (Fin m), probeCount T u σ ≤ (otherOccupiedSlots T u).card + 1.
Under TableRep π s T, probeCount T u (π u) ≤ (Dictionary.dom s).card + 1 holds
deterministically for every key u : Fin U.
Under TableRep π s T and (Dictionary.dom s).card ≤ n_max,
probeCount T u (π u) ≤ n_max + 1 holds deterministically for every key u : Fin U.
Deterministic Expected Probe-Count Bound (≤ n_max + 1) for Any Full-Cycle Permutation
Family: for any nonempty finite seed space Θ and any permutation family
π : Θ → Fin U → Equiv.Perm (Fin m) representing s in T θ with
(Dictionary.dom s).card ≤ n_max, the expected probe count of u under PMF.uniformOfFintype Θ
is at most n_max + 1.