Open-Addressed Linear Probing on the Randomized Word RAM #
This file implements a self-contained open-addressed linear-probing hash table realizing
Cslib.DataStructure.Dictionary.spec U V n_max on the randomized Word RAM
(Cslib.DataStructure.WordRAM.Randomized.StateTransitionImpl), adapting
Pagh, Pagh and Ružić, Linear Probing with
Constant Independence, SIAM J. Comput. 39(3):1107–1120, 2009, Section 2.3 (Lemma 2.1,
pp. 1110–1111) and Section 4 (Lemma 4.1, Theorem 4.3, and Theorem 4.5,
pp. 1114–1118); Cormen, Leiserson, Rivest and Stein, Introduction to
Algorithms, Section 11.4.
Design #
Power-of-$2$ open-addressed layout ($O(n_{\max})$ words, zero tombstones): For capacity bound $n_{\max}$, we set $M = \mathrm{tableLog}(n_{\max}) = \lceil \log_2 \max(8 n_{\max}, 8) \rceil$ and table size $m = 2^M$ (
tableSize n_max), so that $8 n_{\max} \le 2^M < 16 \max(n_{\max}, 1)$ (load factor $\alpha \le 1/8$). Memory atbaseconsists of:[base, base + KWise.space n_max 5): embedded $5$-wise independent trinomial-field hash state (Cslib.DataStructure.HashFamily.WordRAM.KWise);[usedBase, usedBase + 2^M): occupancy flagsused[0 .. 2^M - 1](0= empty,1= occupied, so zero-initialized memory is immediately a valid empty table);[keyBase, keyBase + 2^M): stored keyskey[0 .. 2^M - 1];[valBase, valBase + 2^M): stored valuesval[0 .. 2^M - 1]. Total footprint isspace n_max = KWise.space n_max 5 + 3 * tableSize n_max = O(n_max)words.
Self-contained operations (
initSetup,query,insert):initSetup: runsKWise.initCode 5($O(n_{\max})$ deterministic Four-Russians carryless multiplication table setup plus $5$Code.randcalls, with zero caller-supplied primes).query uandinsert u v: evaluate $b = h_\theta(u) \in \mathrm{Fin}(2^M)$ viaKWise.evalCode 5in $O(L^2 + 1)$ deterministic steps ($L = \mathrm{limbCount}(n_{\max}, U, M) = O(1)$ when $U \le n_{\max}^c$), then scan $(b + d) \bmod 2^M$ until reaching the first slotpwhereused[p] = 0orkey[p] = u. Because at most $n_{\max} \le 2^M / 8$ slots are occupied, at least $7/8$ of all slots are empty and the probe loop terminates deterministically in $\le n_{\max} < 2^M$ iterations on every seed.
Expected $O(1)$ probe bound under $5$-wise independence: If probing for $u$ from $b = h_\theta(u)$ steps over $t \ge 1$ slots occupied by keys in $S = \mathrm{dom}(s) \setminus \{u\}$, walking backwards from $b$ to the nearest preceding empty slot yields a contiguous cyclic interval
cyclicInterval M b a ℓof length $\ell = a + t \in [t, \#S]$ whose $\ell$ stored keys in $S$ all hash intocyclicInterval M b a ℓ(TableInv.hasClusterWitness_probeLen, adapting the occupied-run argument in the proof of Lemma 4.1, p. 1114 of[PaghPaghRuzic2009]). Applyingtsum_uniform_probe_le_of_cluster_witnessfromCslibFrontier.DataStructure.Dictionary.WordRAM.LinearProbing.FiveWisebounds the expected number of skipped occupied slotsTableInv.probeLenby $288 = O(1)$ (tsum_probeLen_le).
Source mapping, specialization, and cost scope #
In [PaghPaghRuzic2009], Section 4 (Theorems 4.3 and 4.5, pp. 1115–1118, built on Lemma 2.1,
pp. 1110–1111, and Lemma 4.1, p. 1114) proves that for any sequence of insertions, deletions, and
lookups in a linear-probing hash table of size $r$ using a hash function chosen uniformly at random
from a $5$-wise independent family $\mathcal{H} : U \to [r]$ with maximum load $\bar{\alpha} < 1$,
the expected number of probes made during an operation is $O((1 - \bar{\alpha})^{-5/2})$
(Theorem 4.3) and $O((1 - \bar{\alpha})^{-13/6})$ (Theorem 4.5), which is $O(1)$ for bounded load
factor. The declarations in this module and LinearProbing.FiveWise map to [PaghPaghRuzic2009] as
follows:
- Hash family (
isKWiseIndependentHash_hashFn): Uniform $5$-wise independent trinomial-field polynomial familyhashFn U n_max(trinomialPolyHashover $\mathbb{F}_{2^q}$ projected to $M$ bits, initialized and evaluated on the Word RAM viaKWise.initCode 5andKWise.evalCode 5), realizing exact uniform $5$-wise independence (IsKWiseIndependentHash 5) with maximum load $\bar{\alpha} = \alpha = \#S / 2^M$. - Operation history and oblivious adversary (
ValidHistory,seq_expected_amortized): An admissible operation sequenceops : List (Dictionary.Op U)fixed independently of the sampled hash seed $\theta \sim \mathrm{seedLaw}\ U\ n_{\max}$, matching the fixed-set / oblivious operation model of[PaghPaghRuzic2009], Section 4. - Load-factor specialization ($\alpha \le 1/8$):
While
[PaghPaghRuzic2009]Theorems 4.3 and 4.5 apply to any maximum load $\bar{\alpha} < 1$, we fix $M = \mathrm{tableLog}(n_{\max}) = \lceil \log_2 \max(8 n_{\max}, 8) \rceil$ so that $\alpha = \#\mathrm{dom}(s) / 2^M \le n_{\max} / 2^M \le 1/8$ (eight_mul_le_tableSize) andspace n_max = O(n_max)(space_le). - Probe count (
TableInv.probeLen) vs. executable Word-RAM body charge (charge): Whereas[PaghPaghRuzic2009]Section 4 (p. 1114) counts $1 + \max\{l \mid h(x) + [l] \subseteq P\}$ table probes,TableInv.probeLencounts the number of skipped occupied slots before the stopping slot ($\mathbb{E}[\mathrm{probeLen}] \le 288$ viatsum_probeLen_le). The final stopping-slot inspection, loop/branch overhead ($12 t + 27$ instructions for $t$ skipped slots), and embeddedKWise.evalCode 5hash evaluation (KWise.evalSteps n_max U (tableLog n_max) 5) are all included in the executable Word-RAM operation chargecharge U n_max = KWise.evalSteps n_max U (tableLog n_max) 5 + 12 * 288 + 27 = KWise.evalSteps n_max U (tableLog n_max) 5 + 3483(opCode_execWithin). - Original proof adaptation and explicit constants (
288and3483): Rather than formalizing the interval-grid decompositions of Lemmas 4.2 and 4.4 in[PaghPaghRuzic2009],LinearProbing.FiveWiseuses an original simplified proof adaptation at load $\le 1/8$ that covers any length-$\ell$ cyclic interval containing $b$ ($2^{j-1} \le \ell < 2^j$) by a single centered dyadic-scale windowscaleWindow M b jof size at most $2^{j+1}$ (card_scaleWindow_le), yielding the explicit scale-$j$ overload bound $18 / (2^{j-2})^2 = 288 / 4^j$ (expect_scaleOverload_le), the probe-step bound $288$, and the instruction constant $12 \cdot 288 + 27 = 3483$. - Fixed-parameter program specialization and charged-body scope (
initParams = Fin.elim0): For fixed(U, V, n_max)on an admissible external machine word widthw(2 * trinomialDeg U (tableLog n_max) ≤ w,Fits w (base + space n_max),Fits w V),subroutineCode U n_maxspecializes configuration constants as instruction literals. Expected instruction bounds (impl,implStandard, andseq_expected_amortized) charge fresh initialization (initFresh) and subroutine operation bodies (StateTransitionImpl.ExecSteps), excluding caller argument delivery, caller control/dispatch loops, and host-level code construction. - Insert-only dictionary specification vs. source deletions:
Whereas
[PaghPaghRuzic2009]Section 1 (p. 1107) and Section 4 (Theorems 4.3 and 4.5) include deletions via greedy backward-shift rearrangement,Dictionary.spec U V n_maxspecifies an insert-only bounded dictionary (queryandinsertwith at mostn_maxdistinct keys and no deletion operation), so open-addressed slots use{0, 1}occupancy flags (0= empty,1= occupied) with zero tombstones.
References #
Section 1: Parameters, memory layout, and cyclic probe geometry #
Latent seed type for the embedded $5$-wise independent trinomial-field hash family: $5$ coefficients in $\mathrm{Fin}(2^{\mathrm{trinomialDeg}(U, \mathrm{tableLog}(n_{\max}))})$.
hashFn U n_max is a $5$-wise independent hash family.
Base address of the occupancy array used[0 .. 2^M - 1].
Base address of the key array key[0 .. 2^M - 1].
Base address of the value array val[0 .. 2^M - 1].
Total contiguous memory footprint of the linear-probing dictionary (in words):
KWise.space n_max 5 + 3 * tableSize n_max = O(n_max).
Section 2: Open-addressed table invariant, termination, and cluster witness #
Linear-probing table invariant at (base, n_max) coupling abstract state
s : Dictionary.State U and seed θ : Seed U n_max with concrete memory mem:
(Dictionary.dom s).card ≤ n_maxandDictionary.ValuesBounded V s;- every slot
phasusedAt p ∈ {0, 1}; - every occupied slot
p(usedAt p = 1) stores a valid keykeyAt p < Uand valuevalAt p < Vmatchings ⟨keyAt p, _⟩ = some (valAt p); - occupied slots store pairwise distinct keys;
- every present key
uwiths u = some vresides at some probe offsetk < 2 ^ MfromhashFn U n_max θ usuch that all preceding probe offsetsj < kare occupied (usedAt = 1); - conversely, every occupied slot
p(usedAt p = 1) is reachable from its key's home buckethashFn U n_max θ ⟨keyAt p, _⟩at some offsetk < 2 ^ Malong an entirely occupied prefix (∀ j < k, usedAt = 1).
Number of stored keys is at most
n_max.- values_bounded : ValuesBounded V s
Every stored value in
sis strictly less thanV. Every occupancy entry is
0or1.- occupied_key_lt (p : Fin (2 ^ tableLog n_max)) : usedAt base n_max mem p = 1 → keyAt base n_max mem p < U
Every occupied slot holds a key
< U. - occupied_state (p : Fin (2 ^ tableLog n_max)) (u : Fin U) : usedAt base n_max mem p = 1 → keyAt base n_max mem p = ↑u → s u = some (valAt base n_max mem p)
Every occupied slot agrees with the abstract dictionary
s. - occupied_inj (p₁ p₂ : Fin (2 ^ tableLog n_max)) : usedAt base n_max mem p₁ = 1 → usedAt base n_max mem p₂ = 1 → keyAt base n_max mem p₁ = keyAt base n_max mem p₂ → p₁ = p₂
Distinct occupied slots hold distinct keys.
- present_slot (u : Fin U) (v : ℕ) : s u = some v → ∃ k < 2 ^ tableLog n_max, let p := probeSlot (tableLog n_max) (hashFn U n_max θ u) k; usedAt base n_max mem p = 1 ∧ keyAt base n_max mem p = ↑u ∧ valAt base n_max mem p = v ∧ ∀ j < k, usedAt base n_max mem (probeSlot (tableLog n_max) (hashFn U n_max θ u) j) = 1
Every present key
uwiths u = some vis stored at some offsetk < 2 ^ Mfrom its hash bucket with all intermediate probe slotsj < koccupied. - occupied_prefix (p : Fin (2 ^ tableLog n_max)) (u : Fin U) : usedAt base n_max mem p = 1 → keyAt base n_max mem p = ↑u → ∃ k < 2 ^ tableLog n_max, p = probeSlot (tableLog n_max) (hashFn U n_max θ u) k ∧ ∀ j < k, usedAt base n_max mem (probeSlot (tableLog n_max) (hashFn U n_max θ u) j) = 1
Every occupied slot
phas a contiguous occupied probe prefix from its key's hash bucket.
Any finset of occupied slots A whose keys belong to K : Finset (Fin U) has cardinality
#A ≤ #K.
Every occupied slot p with keyAt p ≠ u.val maps to a key in (Dictionary.dom s).erase u.
If probing for u : Fin U from b continues across all i < d (d ≤ 2 ^ M), then
d ≤ #((Dictionary.dom s).erase u).
Under TableInv, probing for any u : Fin U from any b stops within
#((Dictionary.dom s).erase u) steps.
Number of probe steps from b before reaching an empty slot or key u.
Exact characterization of the stopping slot p = probeSlot M (hashFn U n_max θ u) t where
t = hinv.probeLen u (hashFn U n_max θ u):
If a slot p has usedAt p = 1 and keyAt p = u.val, then p is the stopping slot
probeSlot M (hashFn U n_max θ u) (hinv.probeLen u (hashFn U n_max θ u)).
Zeroed occupancy array ∀ p, usedAt base n_max mem p = 0 satisfies TableInv for
Dictionary.empty U.
Writing (1, u.val, v) at the stopping slot p = probeSlot M (hashFn U n_max θ u) t
preserves TableInv for Function.update s u (some v) whenever .insert u v is admissible.
Left-anchored cluster witness theorem: if probing key u from b = hashFn U n_max θ u
takes t = hinv.probeLen u b ≥ 1 steps, there is a contiguous cyclic interval
cyclicInterval M b a ℓ (a < ℓ, t ≤ ℓ ≤ #((Dictionary.dom s).erase u)) into which at least
ℓ keys of (Dictionary.dom s).erase u hash.
For any coupled state distribution ν : PMF (Seed U n_max × α) whose seed marginal is bounded
by seedLaw U n_max, any step counter T_probe : Seed U n_max × α → ℕ bounded by hinv.probeLen
on ν.support has expected value at most 288.
Section 3: Word-RAM register layout, programs, and operational execution #
Total number of registers used by the linear-probing subroutine (0 .. 17 for KWise and
probe scratch, 18 .. 23 for allocation, zero-fill, and operation arguments).
Allocation span program: copies frontierReg (18) to baseReg (0) and advances
frontierReg by space n_max.
18-register initialization program setting (n_max, U, tableLog n_max) in registers
1, 2, 3 and invoking KWise.initCode 5.
24-register initialization program.
Single-slot probe inspection program: given current slot pos in reg 2, loads
used[pos] into reg 9 (cellReg) and sets reg 7 (contReg) to 1 if
used[pos] = 1 ∧ key[pos] ≠ u.val, or 0 otherwise.
Probe setup program: initializes maskReg (3), usedBaseReg (4), keyBaseReg (5),
valBaseReg (6), and cOneReg (10), then inspects the home slot pos = (h u).val (reg 2).
Probe loop body: advances pos ← (pos + 1) &&& (2 ^ M - 1) in reg 2 and inspects the new
slot via probeCheckCode.
Linear probe loop while contReg == 1 do probeBody.
Insertion suffix after probeLoop halts at slot pos (reg 2): stores used[pos] ← 1,
key[pos] ← u.val (reg 1), val[pos] ← v (reg 23), and sets outReg (reg 2) to 0.
Lookup suffix after probeLoop halts at slot pos (reg 2) with used[pos] in reg 9:
if reg 9 == 1, loads val[pos] and writes val[pos] + 1 into reg 2; otherwise writes 0
into reg 2.
Unified operation program for query u (opReg = 0) and insert u v (opReg = 1).
SubroutineCode bundle for linear probing.
Expected instruction count for initSetup U n_max:
KWise.initTableSteps n_max U (tableLog n_max) + 38 = O(n_max + log U).
Expected instruction charge per dictionary operation:
KWise.evalSteps n_max U (tableLog n_max) 5 + 3483 = O(L ^ 2 + 1).
Full seed-coupled representation invariant Rep U V n_max w base θ s x for linear probing.
- bounded : WordRAM.Data.Bounded w x
Machine configuration is word-bounded at width
w. Persistent base register
reg 0holdsbase.- kwise_rep : HashFamily.WordRAM.KWise.Rep base (HashFamily.WordRAM.KWise.chunkBits n_max) U (tableLog n_max) 5 θ x.mem
Embedded
5-wise independent hash representation at[base, base + KWise.space n_max 5). Open-addressed linear-probing table invariant at
[usedBase, valBase + tableSize n_max).
allocSpan n_max executes in 3 steps, copying frontierReg (18) to baseReg (0) and
advancing frontierReg to base + space n_max.
initSetup U n_max initializes the 5-wise independent hash table and leaves the zeroed
linear-probing arrays representing the empty dictionary in at most setupTime U n_max expected
instructions.
Single-slot inspection probeCheckCode executes in at most 8 deterministic steps, loading
usedAt base n_max x.mem pos into reg 9 and setting reg 7 to 1 iff
usedAt base n_max x.mem pos = 1 ∧ keyAt base n_max x.mem pos ≠ u.val.
probeSetupCode n_max executes in at most 16 deterministic steps, initializing registers
3, 4, 5, 6, 10 and running probeCheckCode on the initial hash bucket pos.
One iteration of probeBody advances pos to probeSlot (tableLog n_max) pos 1 and inspects
the new slot in at most 10 deterministic steps.
probeLoop halts deterministically at the stopping slot
p = probeSlot (tableLog n_max) b (hinv.probeLen u b) in at most
12 * (hinv.probeLen u b) + 2 instructions.
insertFinishCode executes in 7 deterministic steps, writing (1, u.val, v) at slot p
(insertMem base n_max x.mem p u.val v) and setting reg 2 (outReg) to 0.
queryFinishCode executes in at most 4 deterministic steps, writing val[p] + 1 into reg 2
when s u = some (val[p]) (used[p] = 1) and 0 into reg 2 when s u = none (used[p] = 0).
The key queried or updated by dictionary operation o.
opCode n_max executes any admissible operation o from Rep U V n_max w base θ s x and
argMatchOp o x.regs in at most
KWise.evalSteps n_max U (tableLog n_max) 5 + 12 * t + 27 deterministic instructions (where
t = hrep.table_inv.probeLen (opKey o) (hashFn U n_max θ (opKey o))), preserving Rep for
((Dictionary.spec U V n_max).step s o).1, decoding to
some ((Dictionary.spec U V n_max).step s o).2, and modifying only
(Block.mk base (space n_max)).footprint.
Section 4: Randomized.StateTransitionImpl certificate and expected-time sequence theorem #
StateTransitionImpl certificate for open-addressed linear probing on the randomized Word
RAM
over Cslib.WordRAM.Basis.exact:
- Space:
space n_max = KWise.space n_max 5 + 3 * 2 ^ (tableLog n_max) = O(n_max)words; - Initialization time:
setupTime U n_max = KWise.initTableSteps n_max U (tableLog n_max) + 38 = O(n_max + log U)expected instructions; - Operation charge:
charge U n_max = KWise.evalSteps n_max U (tableLog n_max) 5 + 3483 = O(L ^ 2 + 1)expected instructions perqueryorinsert, which isO(1)wheneverU ≤ n_max ^ cfor any constantc; - Potential:
0(seedSlack := 1).
Standard-basis (Basis.standard) StateTransitionImpl certificate for linear probing,
transported
from impl via Randomized.StateTransitionImpl.toStandard.
Expected amortized multi-operation theorem for linear probing: for any admissible sequence
ops : List (Dictionary.Op U) starting from the empty dictionary on fresh zeroed memory and fixed
independently of the sampled hash seed (oblivious adversary), initFresh followed by the
subroutine operation bodies (StateTransitionImpl.ExecSteps) executes with total expected
instruction
count at most 3 + setupTime U n_max + ops.length * charge U n_max (excluding caller argument
delivery, caller dispatch loops, and host-level code specialization).