Loading lessons...
Hashing Methods & Collision Resolution
Hashing Methods & Collision Resolution
Hashing uses a Hash Function h(k) to map a search key k directly to a bucket/slot index in a Hash Table of size m, achieving O(1) average-case search, insert, and delete.
- Load Factor (λ): λ = frac{N}{TableSize} (fraction of table occupied).
- Static vs Dynamic Hashing: Static hashing uses a fixed-size table (rehashing when λ grows too high); Dynamic hashing (Extendible / Linear Hashing) grows buckets on demand for databases.
Hashing Collision Resolution
1. Hash Function Methods
A good hash function is fast to compute and distributes keys uniformly across 0 .. m - 1:
- Division Remainder:
h(k) = k mod m. Choose m as a prime number not close to a power of 2 to avoid clustering from common factors. - Mid-Square: Square the key and extract the middle digits (e.g., 31212 = 9740641 → 406). Every digit of k contributes to the result!
- Folding: Partition k into equal-width chunks and sum them (Shift Folding:
123 + 456 + 789; Boundary Folding: reverse alternating chunks123 + 654 + 789). - Truncation & Radix Conversion: Truncate selected digits, or interpret k in another base (e.g., base 11) before taking
mod m. - Pseudorandom:
y = (a * x + c) mod m.
2. Collision Resolution Techniques
A Collision occurs when k1 ≠ k2 yet h(k1) = h(k2) (guaranteed to happen eventually by the Pigeonhole Principle).
A. Open Addressing (Closed Hashing, λ ≤ 1)
All keys are stored inside the hash table array itself. On collision, probe sequence i = 0, 1, 2, …:
- Linear Probing:
H(k, i) = (h(k) + i) mod m. - Deck Example (m=10,
h(k)=k%10): Insert89(9),18(8),49(9 → collides at 9, wraps to 0!),58(8 → collides at 8, 9, 0, lands at 1!),9(9 → probes 9, 0, 1, lands at 2!). - Simple & cache-friendly, but suffers from Primary Clustering (long contiguous occupied blocks grow and merge).
- Knuth's Avg Probes: Successful ≈ (1 / 2)(1 + (1 / 1 - λ)), Unsuccessful ≈ (1 / 2)(1 + (1 / (1 - λ)2)).
- Quadratic Probing:
H(k, i) = (h(k) + i^2) mod m. Eliminates primary clustering, but keys with the same initial hash follow the same probe path (Secondary Clustering). - Double Hashing:
H(k, i) = (h1(k) + i * h2(k)) mod m. Uses a second hash h2(k) ≠ 0 (e.g., R - (k mod R) for prime R < m) for step size-eliminates both primary and secondary clustering!
B. Separate Chaining (Open Hashing)
Each slot holds a linked list of colliding keys. Load factor λ can exceed 1; average chain length is λ, and deletion is simply linked-list node removal (no tombstone markers needed!).
3. Rehashing & External Hashing
- Rehashing: When λ exceeds a threshold (e.g.,
0.7), allocate a new prime-sized table (~2× larger) and re-insert all existing keys. - External Hashing: Stores buckets in disk pages with overflow chains when a disk bucket fills.
Static vs Dynamic Hashing
| Static hashing | Dynamic hashing | |
|---|---|---|
| Table / bucket count | Fixed when the table is created | Grows and shrinks as records are added or removed |
| Hash function | Fixed (e.g., k mod m) | Changes with the table (e.g., uses more bits of the hash) |
| Problem | Performance falls as the table fills; resizing needs full rehashing | More complex bookkeeping |
| Examples | Division / mid-square / folding into an array | Extendible hashing, linear hashing (used by databases) |
Hash Function Examples (worked)
| Method | Rule | Example |
|---|---|---|
| Truncation | Keep some digits of the key, drop the rest | 123456789 → keep the last 3 digits → 789 |
| Folding (shift) | Split the key into parts and add them | 25936715 → 2593 + 6715 = 9308 |
| Radix transformation | Write the key in another base, then reduce | 55354 in base 11 = 38652 → drop the leading digit for a 4-digit address → 8652 |
| Pseudorandom | H(k) = (a·k + c) mod m | a = 17, c = 7, m = 307: (17 × 121267 + 7) mod 307 = 2061546 mod 307 = 41 |
Linear Probing: Two Full Examples
Table size 17, keys 6, 12, 34, 29, 28, 11, 23, 7, 0, 33, 30, 45 with h(k) = k mod 17:
| Slot | 0 | 1 | 2 | 3-5 | 6 | 7 | 8 | 9-10 | 11 | 12 | 13 | 14 | 15 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Key | 34 | 0 | 45 | - | 6 | 23 | 7 | - | 28 | 12 | 29 | 11 | 30 | 33 |
45 mod 17 = 11, but slots 11 → 16 and 0 → 1 are all full, so it needs 9 probes and lands in slot 2-a clear picture of primary clustering.
Table size 11, keys 20, 30, 2, 13, 25, 24, 10, 9:
| Slot | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Key | 9 | - | 2 | 13 | 25 | 24 | - | - | 30 | 20 | 10 |
| Probes | 3 | 1 | 2 | 2 | 4 | 1 | 1 | 1 |
- Average probes, successful search = (1+1+1+2+2+4+1+3)/8 = 15/8 ≈ 1.88.
- Average probes, unsuccessful search (start at each of the 11 slots and count probes until an empty slot is reached, inclusive) = (2+1+5+4+3+2+1+1+5+4+3)/11 = 31/11 ≈ 2.82.
Quadratic Probing Example
Table size 10, h(k) = k mod 10, probe sequence h, h+1², h+2², …; insert 89, 18, 49, 58, 9:
- 89 → 9, 18 → 8.
- 49 → 9 (full) → 9 + 1 = 0.
- 58 → 8 (full) → 9 (full) → 8 + 4 = 12 mod 10 = 2.
- 9 → 9 (full) → 0 (full) → 9 + 4 = 13 mod 10 = 3.
Double Hashing Example
Table size 10, h₁(k) = k mod 10, h₂(k) = 7 − (k mod 7); insert 89, 18, 49, 58, 69:
- 89 → 9, 18 → 8.
- 49 → 9 (full); h₂ = 7 − 0 = 7 → (9 + 7) mod 10 = 6.
- 58 → 8 (full); h₂ = 7 − 2 = 5 → (8 + 5) mod 10 = 3.
- 69 → 9 (full); h₂ = 7 − 6 = 1 → (9 + 1) mod 10 = 0.
Different keys that collide at the same slot get different step sizes, so they do not follow each other into clusters.
Separate Chaining Example
Keys 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 with h(k) = k mod 10:
0 → 0
1 → 1 → 81
2
3
4 → 4 → 64
5 → 25
6 → 16 → 36
7
8
9 → 9 → 49Load factor λ = 10/10 = 1; a search inspects about 1 + λ/2 nodes on average.
Load-factor guidance: keep λ ≤ 0.5 for open addressing (probe counts rise sharply after that), and λ ≈ 1 for separate chaining.
Applications of Hashing
- Symbol tables in compilers and interpreters.
- Database indexing and caches (e.g., memcached, browser caches).
- Password storage (store the hash, not the password).
- Duplicate detection, spell checkers,
dict/HashMapin programming languages. - Routing tables and file-integrity checks (checksums).
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Explain any 5 Hash Function methods with examples. (7.5 Marks)
- Write: Division Remainder (prime m), Mid-Square (31212 → 406), Shift & Boundary Folding, Truncation, and Radix Conversion.
- Q2. What is a Collision? Explain Linear Probing, Quadratic Probing, Double Hashing, and Separate Chaining. Insert
89, 18, 49, 58, 9into a table of size 10 using Linear Probing. (10 Marks) - Write: Collision & Load Factor (λ), Primary vs Secondary Clustering, formulas for all 3 open-addressing probes, Separate Chaining linked-list diagram, and the exact index table (
0:49, 1:58, 2:9, 8:18, 9:89).