---
title: Two-Way Linear Probing
url: https://www.emergentmind.com/topics/two-way-linear-probing
type: topic
---

# Two-Way Linear Probing

Two-way linear probing is a hashing scheme designed to improve the performance of hash tables by reducing the worst-case search and insertion times associated with classical linear probing. By assigning each key two independent probe sequences—rather than one—and judiciously selecting which sequence to use upon insertion, two-way linear probing achieves asymptotically almost sure $O(\log\log n)$ upper bounds on worst-case unsuccessful search times, significantly outperforming the $\Theta(\log n)$ bound of traditional linear probing for constant load factors. This scheme operates pointer-free, utilizes only two hash functions of constant independence, and exploits the full storage capacity of the table, making it both theoretically efficient and practically appealing [2309.05308].

## 1. Motivation and Basic Definitions

Classical linear probing (LP) resolves collisions by selecting a hash value $f(x) \in \{0, \dots, n-1\}$ and sequentially probing forward one cell at a time (modulo $n$) until an empty position appears. When inserting $m = \alpha n$ keys into a table of size $n$ ($\alpha \in (0,1)$ constant), primary clustering leads to a maximum cluster length of $\max\{\text{cluster size}\} \approx \frac{\log n}{\alpha-1-\ln\alpha} = \Theta(\log n)$ with high probability, yielding a worst-case unsuccessful search time of the same asymptotic order.

Two-way linear probing extends this paradigm by providing each key with two independent probe sequences, each defined by uniform and independent hash functions $f, g : \mathcal U \rightarrow \{0, \dots, n-1\}$:
- $f_1(x) = f(x),\quad f_{i+1}(x) \equiv f_i(x)+1 \pmod n$
- $g_1(x) = g(x),\quad g_{i+1}(x) \equiv g_i(x)+1 \pmod n$

Insertion is performed by evaluating both probe sequences to their respective empty cells and selecting one according to a specific strategy.

## 2. Algorithmic Framework and Strategies

A generic two-way linear probing insertion (using a first-come-first-served policy) is described as follows:
```python
TwoWayInsert(x):
    h1 ← f(x),  h2 ← g(x)
    i ← j ← 0
    while cell[f_{i+1}(x)] and cell[g_{j+1}(x)] are both occupied:
        i ← i+1; j ← j+1
    p1 ← f_{i+1}(x); p2 ← g_{j+1}(x) # terminal empty cells reached
    choose one of p1,p2 according to strategy S; insert x there
```
The search procedure alternates probes along the two sequences until the key is found or both sequences reach their terminal empty cells.

Several explicit strategies for selecting $p_1$ or $p_2$ have been analyzed:
- **ShortSeq:** Insert at the position where the shorter probe walk terminated.
- **SmallCluster:** Insert adjacent to the smaller neighboring cluster.
- **LocallyLinear:** Partition the table into blocks of size $\beta_1$. Insert into the least-loaded initial block and probe only within it.
- **DecideFirst:** Partition into blocks of size $\beta_2$. Compare the total keys assigned to each initial block and probe linearly from the less populated.
- **WalkFirst:** With blocks of size $\beta_3$, probe along both sequences to terminal cells $U, V$, then insert into the terminal cell whose block has lower load.

## 3. Theoretical Guarantees and Lower Bounds

- **Universal Lower Bound:** Any two-way linear probing algorithm using two independent probe sequences and inserting into one of the two discovered terminal empty cells must, with high probability, generate a cluster of size at least $\log_2\log n - O(1)$ [Theorem 3.1].
- **Limits of Greedy Strategies:** Algorithms that always select an empty initial cell (e.g., ShortSeq, SmallCluster) inevitably yield clusters of size $\Omega(\log n)$ with high probability [Theorem 3.2].
- **Double-Logarithmic Upper Bounds:**
    1. **LocallyLinear:** With block-size $\beta_1(n)=\frac{\log_2\log n + C}{1-\alpha}+1$, the maximum unsuccessful search time is at most $2\beta_1 = O(\log\log n)$ with high probability.
    2. **DecideFirst:** For block-size $\beta_2(n)=\frac{1+\sqrt{2-\alpha}}{\sqrt{2-\alpha}(1-\alpha)}(\log_2\log n+\eta)$, the bound is $O((1-\alpha)^{-2}\log\log n)$.
    3. **WalkFirst:** For any $\delta > 2\alpha$ and block-size $\beta_3(n)=\frac{\log_2\log n + 8}{1-\delta}$, the bound is $4\beta_3-2 = O(\log\log n)$.

## 4. Analytical Techniques and Key Lemmas

The theoretical analysis leverages advanced probabilistic tools and witness structures:
- **Chernoff and Binomial Tails:** Used to bound the probability that blocks become overfull.
- **Negative Association (Dubhashi–Ranjan):** Ensures that the event of many full blocks declines exponentially.
- **Witness-Tree Argument for WalkFirst (Cole et al.):** Construction of “history trees” to trace dependencies among insertions. If a block's load exceeds $h+\xi$, the existence of a corresponding truncated witness tree of size $w$ is used for probabilistic bounding via union bounds.

Key lemmas include:
- **Lemma 4.1:** Negative association to control full blocks for LocallyLinear.
- **Lemma 4.2:** Upper bounds on the probability of a specific witness tree structure.
- **Lemma 4.3:** Tail bounds on block counts of large load.
- **Lemma 4.4:** Lower bounds on the number of black nodes in a witness tree as function of its height.

## 5. Comparison to Alternative Hashing Schemes

| Scheme              | Worst-case Search Time                | Memory Model         |
|---------------------|--------------------------------------|---------------------|
| Linear Probing      | $\Theta(\log n)$                     | Pointer-free        |
| Two-way Linear Probing | $O(\log\log n)$ a.a.s.             | Pointer-free        |
| Two-way Chaining    | $\Theta(\log\log n)$                 | Pointers per slot   |
| Cuckoo Hashing      | $O(1)$                               | Two tables, key kicks & rehash |
| $d$-way LP ($d>2$)  | $O((\log\log n)/\log d)$             | $d$ hash fns/probes |

Classical LP suffers from primary clustering, yielding logarithmic worst-case time. Two-way chaining (GreedyMC with $d=2$) achieves similar asymptotic bounds to two-way LP but uses explicit pointers. Cuckoo hashing achieves constant bounds but at the expense of bisected table space, key relocation, and potential need for global rehash. $d$-way LP (for $d>2$) can further reduce the worst-case to $O((\log\log n)/\log d)$, but at the cost of extra probes and hash functions; $d=3$ is identified as a practical minimum for further improvements [2309.05308].

## 6. Empirical Performance and Parameter Selection

The block-size constants that determine partitioning in LocallyLinear, DecideFirst, and WalkFirst grow as $1/(1-\alpha)$ or $1/(1-\alpha)^2$. These remain manageable for $\alpha < 0.9$. Empirical simulations up to $n=2^{22}$ and $\alpha=0.4, 0.9$ demonstrate that:
- LocallyLinear and ShortSeq exhibit superior average insertion/search performance.
- WalkFirst and LocallyLinear minimize the maximum cluster size.
- DecideFirst is slightly less efficient at high load factors.

Two-way linear probing requires only two hash functions of constant independence (e.g., 5-wise tabulation suffices), requires no pointers, and per-block counters (needed in strategies such as LocallyLinear) incur negligible memory overhead: $O(n/\log\log n)$ words in the word RAM.

## 7. Synthesis and Deployment Considerations

Two-way linear probing fuses the multiple-choice paradigm with the in-place, cache-efficient structure of classical linear probing, achieving asymptotically optimal probe bounds of $O(\log\log n)$, $O(1)$ hash function overhead, and full table utilization without global rehashing or key relocation. This positions it as an efficient alternative in environments demanding provably low probe sequences, pointer-free implementation, and maximal in-memory packing [2309.05308].

Source: https://www.emergentmind.com/topics/two-way-linear-probing