Abstract
Double Hashing is an advanced Open Addressing strategy designed to eliminate Primary Clustering. By using a second hash function to determine the “skip” or offset, it ensures that keys which hash to the same initial index follow different probe sequences, leading to a more uniform distribution.
- Category: Open Addressing Strategy
- Stores: Internal key allocations distributed within a bounded flat array.
- Built on top of: Contiguous arrays.
- Typical use cases: Tables requiring maximum memory distribution with high lookups and zero external memory allocations.
Core Structure
Keys reside directly within the backing array. Rather than probing adjacent cells sequentially by a constant 1, collisions trigger a personalized step offset size generated by a secondary hash function:
- : The primary hash function determining the starting position.
- : The secondary hash function determining the step size.
- Constraint: must return a value greater than 0 and should ideally be relatively prime to the table size to ensure the probe sequence visits every slot in the array.
Key Idea
Even if two keys collide at the same initial index , their secondary hash will likely be different, sending them along entirely distinct probe sequences across the array.
Data Structure Operations
Insert(k)
Calculates a primary index and a personalized jump offset, probing the array until an open slot, a tombstone, or a duplicate key is reached.
- Time Complexity: average; worst-case.
Algorithm 55 Double Hashing Insertion
procedure InsertDoubleHash()
H1()
H2()
while do
if then
return
if then
return
if then
EnlargeTable()
H1()
H2()
Common Pitfalls
- Incompatible Table Sizes: Failing to verify that is relatively prime to the table size can result in an incomplete search sequence that misses open slots despite the array having capacity.
- Zero Step Offset: Allowing to output 0 causes an infinite self-looping stall on the same index during collision resolution.
Trade-offs Compared to Other Data Structures
| Collision Resolution Method | Probe Sequence Mechanics | Probability of Clumping |
|---|---|---|
| Linear Probing | High (Primary Clustering) | |
| Double Hashing | Very Low | |
| Random Hashing | Very Low |
When to Reach for This Structure
Double Hashing is an Open Addressing strategy because keys are stored directly within the backing array. Reach for it when you want to eliminate primary clustering without incurring the execution overhead of tracking random number generator state updates.