π Open Addressing (Collision Resolution Technique)
Open Addressing is a collision-handling technique used in hash tables in which all key-value pairs are stored directly inside the hash table (i.e., no linked lists, no external structure).
When a collision occurs (two keys hash to the same index), the hash table looks for another empty slot inside the table according to a probing sequence.
π Key Idea
- Store every element inside the table (size = m slots).
- If the desired slot is occupied β probe (search) to find the next empty slot.
- Searching, inserting, deleting β O(1) average, O(n) worst-case.
π Core Formula
For a key k and a probing number i:
[
h(k, i) = (h(k) + f(i)) \mod m
]
Where:
h(k)= primary hash functionf(i)= probing functioni= number of collisions so far
π Requirements of Open Addressing
- Load factor must be < 1
[
\alpha = \frac{n}{m} < 1
] - Table must have at least one empty slot.
- Deletions require special handling (“lazy delete” markers).
π Types of Open Addressing
There are three main types:
1. Linear Probing
[
h(k, i) = (h(k) + i) \mod m
]
Sequence:
h(k), h(k)+1, h(k)+2, β¦
β Advantages
- Simple
- Cache-friendly (array locality)
β Disadvantages
- Primary clustering
- Long blocks of filled cells form
- Increases search/insert time
2. Quadratic Probing
[
h(k, i) = (h(k) + i^2) \mod m
]
Sequence:
h(k), h(k)+1Β², h(k)+2Β², h(k)+3Β² β¦
β Advantages
- Reduces primary clustering
- Better than linear probing
β Disadvantages
- Secondary clustering
- Keys with the same h(k) follow same probe path
- Must choose
mand coefficients carefully to ensure full table coverage
3. Double Hashing (Best Technique)
[
h(k, i) = (h_1(k) + i \cdot h_2(k)) \mod m
]
Where:
h1(k)is main hashh2(k)is secondary hash- h2(k) β 0
β Advantages
- Almost eliminates clustering
- Best distribution
- Most efficient open-addressing technique
β Disadvantages
- Slightly more complex
- Requires good second hash function
π Example of Probing (Linear Probing)
Table size = 7
Hash function: ( h(k) = k \mod 7 )
Insert keys: 10, 20, 30
- 10 β 10 % 7 = 3 β slot 3
- 20 β 20 % 7 = 6 β slot 6
- 30 β 30 % 7 = 2 β slot 2
Now insert 17: - 17 % 7 = 3 β occupied
- Try 4 β empty
β Insert at slot 4
π Deletion in Open Addressing
Removing directly will break probe chains.
Solution β Lazy deletion
- Mark cell as βdeletedβ
- Future searches continue probing
- Insertions may reuse deleted slots
π Performance of Open Addressing
| Metric | Average | Worst Case |
|---|---|---|
| Search | O(1) | O(n) |
| Insert | O(1) | O(n) |
| Delete | O(1) | O(n) |
Performance depends on:
- load factor (Ξ±)
- Probing technique
- Quality of hash function
To maintain O(1) efficiency:
[
\alpha \le 0.5 \text{ to } 0.7
]
When Ξ± becomes large β rehashing needed.
π Comparison of Probing Methods
| Method | Primary Clustering | Secondary Clustering | Complexity |
|---|---|---|---|
| Linear Probing | High β | Medium | High |
| Quadratic Probing | Medium | High β | Medium |
| Double Hashing | Very Low β | Very Low β | Best |
Double Hashing is considered the best form of open addressing.
π Advantages of Open Addressing
- No extra memory for linked lists
- Cache-friendly (array-based)
- Good average-case performance
- Simple structure
- Used in many modern hash tables
π Disadvantages
- Performance drops sharply at high load factors
- Deletion is complicated (requires lazy delete)
- Cannot store more than m keys
- Probing sequences can become long (clustering)
π Summary (Exam Notes)
- Open addressing stores all keys inside the hash table
- Uses probing to find empty slots
- Types:
β Linear Probing
β Quadratic Probing
β Double Hashing (best) - Needs load factor Ξ± < 1
- Uses lazy deletion
- Average O(1), worst O(n)
- Preferred method: Double Hashing
