DSA Notes
Complete guide to open addressing for hash collision resolution — linear probing with clustering analysis, quadratic probing, double hashing, tombstone deletion, the DELETED sentinel pattern, performance at different load factors, and Python dict
Open Addressing Fundamentals
In open addressing, all elements are stored in the hash table array itself — no external linked lists or trees. When a collision occurs, the algorithm probes a sequence of alternative slots until finding an empty one.
| Insert key=k1 (hash=3): slot 3 is empty | place at 3 |
| Insert key=k2 (hash=3): slot 3 occupied | probe slot 4 → empty → place at 4 |
| Insert key=k3 (hash=3): probe 3 | 4→5 → empty → place at 5 |
| Table | [_, _, _, k1, k2, k3, _] |
The probe sequence determines which slots are tried in order.
Linear Probing — Clustering Analysis
The major weakness of linear probing is primary clustering: long runs of occupied consecutive slots form, making probing slow.
Table (showing probe lengths for each element)
[3, 1, 4, 2, 5, 1, _, _, 1, 2, _]
↑ long run here
When inserting a new key that hashes to index 0
Must probe through the entire cluster: 0,1,2,3,4,5 = 6 probes!
The cluster grows longer with each insertion into or near it.
Knuth's analysis — expected probes for linear probing at load factor α:
| Successful lookup | (1 + 1/(1-α)²) / 2 ≈ 1/(2(1-α)) |
| Unsuccessful lookup | (1 + 1/(1-α)²) / 2 ≈ 1/(2(1-α))² |
| At α = 0.5 | ~2.5 probes (acceptable) |
| At α = 0.7 | ~5.8 probes (degrading) |
| At α = 0.9 | ~50 probes (terrible) |
Quadratic Probing
Quadratic probing reduces primary clustering by jumping farther and farther. However, it introduces secondary clustering: all keys with the same initial hash follow the same probe sequence.
Important constraint: With quadratic probing, not all slots are necessarily reached. To guarantee every slot is probed, the table size must be a prime and load factor < 0.5.
Double Hashing
Double hashing distribution:
- Each key has a unique probe sequence (determined by h2)
- No clustering of any kind
- Uniformly distributed probing
- Best theoretical performance among open addressing methods
Probe Sequence Comparison
| Method | Probe Sequence | Clustering | Table Size Required |
|---|---|---|---|
| Linear | h+0, h+1, h+2, ... | Primary | Any |
| Quadratic | h+0, h+1, h+4, h+9 | Secondary | Prime, α<0.5 |
| Double Hash | h, h+h2, h+2h2, ... | None | Prime |
Expected Probes at Different Load Factors
At α = 0.5, double hashing is optimal: exactly 1/(1-α) = 2.0 probes. Linear probing degrades much faster due to clustering.
Python Dict's Probing Strategy
Python's dict uses a perturbation-based scheme that's more complex than simple linear probing:
# Simplified version of Python's probe sequence
def python_like_probe(key, size):
idx = hash(key) % size
perturb = hash(key)
while True:
yield idx
perturb >>= 5
idx = (idx * 5 + perturb + 1) % sizeThis achieves good distribution while being cache-friendly and fast for the common case where the first probe succeeds.
Summary
| Method | Avg probes (α=0.5) | Avg probes (α=0.8) | Clustering | Memory |
|---|---|---|---|---|
| Linear Probing | ~2.5 | ~8.0 | Primary | O(1) per entry |
| Quadratic | ~1.9 | ~4.0 | Secondary | O(1) per entry |
| Double Hashing | ~1.5 | ~2.5 | None | O(1) per entry |
Open addressing advantages over chaining:
- Better cache performance (all data in one array)
- Lower memory overhead (no pointer per entry)
- Better for small key-value pairs that fit in cache lines
Open addressing disadvantages:
- Load factor must stay below ~0.7
- Deletion requires tombstones (complicates logic)
- Clustering degrades performance as table fills
*Next Lesson: Hashing Problems*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Open Addressing — Linear Probing, Quadratic Probing, and Double Hashing.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, hashing, open, addressing
Related Data Structures & Algorithms Topics