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(k,arr,mk, arr, m)

indexindex \gets H1(kk)

offsetoffset \gets H2(kk)

startindexstart \gets index

while true\text{true} do

if arr[index]==karr[index] == k then

return false\text{false}

if arr[index]==NULL or arr[index]==TOMBSTONEarr[index] == \text{NULL or } arr[index] == \text{TOMBSTONE} then

arr[index]karr[index] \gets k

return true\text{true}

index(index+offset)(modm)index \gets (index + offset) \pmod m

if index==startindex == start then

EnlargeTable(arrarr)

indexindex \gets H1(kk)

offsetoffset \gets H2(kk)

startindexstart \gets index


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 MethodProbe Sequence MechanicsProbability of Clumping
Linear ProbingHigh (Primary Clustering)
Double HashingVery Low
Random HashingVery 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.


Related Notes