Hashing - Open Addressing III: Double Hashing and Secondary Clustering
This chapter explores the phenomenon of Secondary Clustering, a key performance bottleneck that arises when using Quadratic Probing in open addressing. We will contrast secondary clustering with primary clustering, analyze why it occurs using concrete mathematical traces, and introduce Double Hashing as the definitive solution to eliminate redundant probe paths.
1. The Problem of Secondary Clustering
While Quadratic Probing successfully resolves the "continuous block" issue of Primary Clustering (by using non-linear step sizes), it introduces a different efficiency bottleneck known as Secondary Clustering.
Secondary clustering occurs when multiple keys hash to the same initial slot. Because the probe sequence formula relies solely on the initial hash value and the probe parameter , any two keys that share the same initial hash index will traverse the exact same sequence of probe slots during collision resolution. This leads to redundant collision checks and significantly degrades insertion and search performance.
2. Mathematical Walkthrough of Secondary Clustering
To understand how secondary clustering manifests, let us trace the insertion of keys into a hash table of size using Quadratic Probing.
The hash function formula for quadratic probing is:
Where:
- (the primary hash function)
- is the probe attempt number ()
Step-by-Step Insertion of Key 34
- Initial Probe ():
Status: Slot 4 is already occupied. Collision occurs.
- Second Probe ():
Status: Slot 5 is already occupied. Collision occurs.
- Third Probe ():
Status: Slot 8 is already occupied. Collision occurs.
- Fourth Probe ():
Status: Slot 3 is already occupied. Collision occurs.
- Fifth Probe ():
Status: Slot 0 is empty. Key 34 is stored at index 0.
Note on Calculation: The speaker calculates the probe index by adding the quadratic step directly to the key before applying the modulo operator: . This is mathematically equivalent to and yields the exact same probe sequence.
Step-by-Step Insertion of Key 24
Now, let us insert key 24. Note that , which is the exact same initial hash index as 34.
- Initial Probe (): Index (Collision)
- Second Probe (): Index (Collision)
- Third Probe (): Index (Collision)
- Fourth Probe (): Index (Collision)
- Fifth Probe (): Index (Collision)
Because 24 and 34 share the same initial hash index, 24 is forced to trace the identical path of collisions before finding an open slot. This shared path traversal is the core of the secondary clustering problem.
3. Primary Clustering vs. Secondary Clustering
4. Introduction to Double Hashing
To resolve secondary clustering, we must ensure that keys hashing to the same initial index do not follow the same probe path.
Double Hashing achieves this by using a second, independent hash function to determine the step size for each key. This means the step size is no longer a fixed constant (as in Linear Probing) or a simple function of the probe attempt (as in Quadratic Probing); instead, the step size is dynamically computed based on the key itself.
5. Mathematical Formulation of Double Hashing
The probe sequence formula for Double Hashing is defined as:
Where:
- is the key to be hashed.
- is the probe attempt number ().
- is the primary hash function, which determines the starting slot.
- is the secondary hash function, which determines the step size.
- is the total size of the hash table.
Crucial Rule for : The secondary hash function must never evaluate to . If , the step size becomes , causing the algorithm to get stuck in an infinite loop on the initial collision slot .
6. Step-by-Step Execution of Double Hashing
Let us trace an insertion workflow using Double Hashing on a table of size .
Setup Functions
- Primary Hash Function:
- Secondary Hash Function:
- Combined Formula:
Step 1: Insert Key 32
- For :
- Result: Slot
2is empty. Store32at index2.
Step 2: Insert Key 47
- For :
Status: Slot 2 is occupied by 32. Collision detected!
- For : We must calculate the step size using the secondary hash function :
Now, apply the double hashing formula:
- Result: Slot
7is empty. Store47at index7.
7. Why Double Hashing Eliminates Secondary Clustering
To see how Double Hashing solves secondary clustering, let us introduce a third key, 52, which hashes to the same initial index as 32 and 47.
- Primary Hash: (Collision at slot
2) - Secondary Hash (Step Size):
Now, let us calculate the next probe index for 52 at :
Comparison of Probe Paths
Even though 47 and 52 start at the exact same slot (2), they diverge immediately on the very first collision because their step sizes are different ( and , respectively). This personalized step calculation completely eliminates secondary clustering.
8. Summary and Key Takeaways
- Secondary Clustering Cause: It occurs in Quadratic Probing because the probe sequence is determined solely by the initial hash index. Keys with identical initial hashes trace identical probe paths.
- Primary vs. Secondary Clustering: Primary clustering forms a single localized block of occupied slots (Linear Probing), while secondary clustering creates identical search paths across non-contiguous slots (Quadratic Probing).
- Double Hashing Solution: It solves secondary clustering by using a second hash function to compute a unique step size based on the key itself.
- Double Hashing Formula: .
- The Non-Zero Rule: The secondary hash function must never return to prevent infinite loops during collision resolution.
- Performance Benefit: By ensuring that keys with the same initial hash index diverge immediately upon collision, Double Hashing minimizes probe lengths and keeps insertion/lookup times close to .