Loading lessons...
Collision Resolution - Open Addressing, Chaining & Rehashing
Collision Resolution - Open Addressing, Chaining & Rehashing
A collision happens when two different keys hash to the same slot. Collisions cannot be avoided, so every hash table needs a collision-resolution technique. There are two families:
| Family | Where colliding keys go | Methods |
|---|---|---|
| Open addressing (closed hashing) | Into another empty slot of the same table, found by probing | Linear probing, quadratic probing, double hashing |
| Separate chaining (open hashing) | Into a linked list hanging from the home slot | Chaining |
1. Open Addressing
All keys live inside the table array, so the load factor λ = n / m can never exceed 1. When the home slot is full, the table is probed - slots are tried in a fixed order - until an empty slot is found. The i-th probe (i = 0, 1, 2, …) looks at slot H(k, i).
General insert and search
Algorithm OA_INSERT(T, M, KEY)
for I ← 0 to M - 1 do
J ← H(KEY, I) // I-th probe position
if T[J] is EMPTY or DELETED then
T[J] ← KEY
return J
return OVERFLOW // table fullDeletion: a deleted slot cannot simply be emptied - that would create a gap that stops later searches too early and makes keys beyond it "disappear". Instead it is marked DELETED (a tombstone): searches pass over it, insertions may reuse it.
(a) Linear probing
H(k, i) = (h(k) + i) mod m - try the home slot, then the next slot, then the next, wrapping round to 0.
#define M 10
#define EMPTY -1
int table[M]; /* all set to EMPTY at the start */
int insertLinear(int key) {
int home = key % M;
for (int i = 0; i < M; i++) {
int j = (home + i) % M;
if (table[j] == EMPTY) { table[j] = key; return j; }
}
return -1; /* table full */
}Example: m = 10, h(k) = k mod 10; insert 89, 18, 49, 58, 9.
| Key | Home | Slots probed | Placed at | Probes |
|---|---|---|---|---|
| 89 | 9 | 9 | 9 | 1 |
| 18 | 8 | 8 | 8 | 1 |
| 49 | 9 | 9, 0 | 0 | 2 |
| 58 | 8 | 8, 9, 0, 1 | 1 | 4 |
| 9 | 9 | 9, 0, 1, 2 | 2 | 4 |
slot: 0 1 2 3 4 5 6 7 8 9
+----+----+----+----+----+----+----+----+----+----+
| 49 | 58 | 9 | | | | | | 18 | 89 |
+----+----+----+----+----+----+----+----+----+----+Primary clustering: occupied slots form long runs. Any key hashing anywhere into a run lands at its end and makes the run longer, and neighbouring runs merge. The longer a run, the more likely it is to grow.
Expected probes (Knuth), load factor λ:
- Successful search ≈ (1 / 2)(1 + (1 / 1 - λ))
- Unsuccessful search ≈ (1 / 2)(1 + (1 / (1 - λ)2))
At λ = 0.5 that is 1.5 and 2.5 probes; at λ = 0.9 it is 5.5 and 50.5 probes. Keep λ ≤ 0.5-0.7 for open addressing.
Worked example with probe counts
Table size 11, h(k) = k mod 11; insert 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 successful search = (1 + 1 + 1 + 2 + 2 + 4 + 1 + 3) / 8 = 15 / 8 ≈ 1.88 probes.
- Average unsuccessful search (start at each slot, count probes up to and including the first empty slot) = (2 + 1 + 5 + 4 + 3 + 2 + 1 + 1 + 5 + 4 + 3) / 11 = 31 / 11 ≈ 2.82 probes.
A long cluster
Table size 17, keys 6, 12, 34, 29, 28, 11, 23, 7, 0, 33, 30, 45:
| 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 45 needs 9 probes and lands in slot 2.
(b) Quadratic probing
H(k, i) = (h(k) + i2) mod m - jump 1, 4, 9, 16, … slots away from home.
Example: m = 10; insert 89, 18, 49, 58, 9.
| Key | Home | Slots probed | Placed at |
|---|---|---|---|
| 89 | 9 | 9 | 9 |
| 18 | 8 | 8 | 8 |
| 49 | 9 | 9, 9 + 1 = 0 | 0 |
| 58 | 8 | 8, 9, 8 + 4 = 12 → 2 | 2 |
| 9 | 9 | 9, 0, 9 + 4 = 13 → 3 | 3 |
- Removes primary clustering: keys do not pile up in one run.
- Secondary clustering: keys with the same home slot still follow the same probe sequence.
- It may fail to find an empty slot even when one exists. If m is prime and λ < 0.5, an empty slot is always found.
(c) Double hashing
H(k, i) = (h1(k) + i × h2(k)) mod m - the step size comes from a second hash function.
- h2(k) must never be 0; a common choice is h2(k) = R - (k mod R) for a prime R < m.
Example: m = 10, h1(k) = k mod 10, h2(k) = 7 − (k mod 7); insert 89, 18, 49, 58, 69.
| Key | h1 | h2 | Slots probed | Placed at |
|---|---|---|---|---|
| 89 | 9 | - | 9 | 9 |
| 18 | 8 | - | 8 | 8 |
| 49 | 9 | 7 − 0 = 7 | 9, 16 → 6 | 6 |
| 58 | 8 | 7 − 2 = 5 | 8, 13 → 3 | 3 |
| 69 | 9 | 7 − 6 = 1 | 9, 10 → 0 | 0 |
49 and 69 both start at slot 9 but take different steps (7 and 1), so double hashing removes both primary and secondary clustering. It is the best open-addressing method but needs two hash computations.
Comparison of probing methods
| Basis | Linear probing | Quadratic probing | Double hashing |
|---|---|---|---|
| Probe sequence | h, h + 1, h + 2, … | h, h + 1, h + 4, h + 9, … | h₁, h₁ + h₂, h₁ + 2h₂, … |
| Primary clustering | Yes | No | No |
| Secondary clustering | Yes | Yes | No |
| Finds an empty slot | Always (if one exists) | Guaranteed only if m prime and λ < 0.5 | If h2(k) and m are coprime |
| Speed of computing | Fastest; cache-friendly | Fast | Two hash functions |
2. Separate Chaining
Each slot holds a pointer to a linked list of all keys that hash to it. A collision just adds a node to the list.
Algorithm CHAIN_INSERT(T, KEY)
I ← h(KEY)
create node NEW with DATA ← KEY
NEW.NEXT ← T[I] // insert at the front of chain I
T[I] ← NEW
Algorithm CHAIN_SEARCH(T, KEY)
P ← T[h(KEY)]
while P ≠ NULL and P.DATA ≠ KEY do
P ← P.NEXT
return P // NULL if not foundExample: keys 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 with h(k) = k mod 10 (new keys appended at the end of each chain for readability):
[0] → 0
[1] → 1 → 81
[2]
[3]
[4] → 4 → 64
[5] → 25
[6] → 16 → 36
[7]
[8]
[9] → 9 → 49Analysis: the average chain length is λ = n / m (here 10 / 10 = 1).
- Unsuccessful search: about 1 + λ (compute the hash, then scan a whole chain).
- Successful search: about 1 + (λ / 2).
- λ may be greater than 1; keep λ ≈ 1.
| Advantages | Disadvantages |
|---|---|
| Table never overflows; λ can exceed 1 | Extra memory for pointers |
| Deletion is ordinary list deletion - no tombstones | Poorer cache performance (nodes scattered) |
| No clustering; performance degrades gracefully | A long chain makes search slow (O(n) worst) |
3. Open Addressing vs Separate Chaining
| Basis | Open addressing | Separate chaining |
|---|---|---|
| Where keys are stored | In the table itself | In linked lists outside the table |
| Load factor | λ ≤ 1 (keep ≤ 0.5-0.7) | λ can exceed 1 (keep ≈ 1) |
| Extra memory | None | Pointers |
| Clustering | Yes (linear/quadratic) | No |
| Deletion | Needs DELETED markers | Simple |
| Best when | Number of keys known, memory tight | Number of keys unknown, many deletions |
4. Rehashing
As the table fills, probe sequences or chains become long. Rehashing builds a bigger table (usually the next prime above twice the old size) and re-inserts every key using the new hash function.
Algorithm REHASH(T, M)
NEWM ← smallest prime ≥ 2 × M
create table NEWT of size NEWM, all EMPTY
for each key K stored in T do
insert K into NEWT using h(K) = K mod NEWM
T ← NEWT; M ← NEWMWhen to rehash: when λ crosses a threshold (e.g. 0.5 or 0.7 for open addressing), when an insertion fails, or when there are too many DELETED markers.
Cost: one rehash is O(n), but since the table doubles, it happens rarely - the amortised cost per insertion stays O(1).
Example: table size 7 with h(k) = k mod 7 holds 13, 15, 24, 6 (λ = 4/7 ≈ 0.57). Rehash to size 17 (the next prime after 14): 13 → 13, 15 → 15, 24 → 7, 6 → 6.
5. Extendible Hashing (Dynamic Hashing)
Used for large files on disk where the data size changes.
- A directory of 2d pointers is indexed by the first d bits of h(k) (d = global depth).
- Each pointer leads to a disk bucket holding several records.
- When a bucket overflows it is split into two, using one more bit; only if needed is the directory doubled.
- A search needs at most two disk accesses (directory, then bucket), and the file grows without rehashing everything.
Key points to remember - Linear: h + i (primary clustering). Quadratic: h + i² (secondary clustering). Double: h₁ + i × h₂ (no clustering). - Insert 89, 18, 49, 58, 9 into size 10: linear → 49:0, 58:1, 9:2; quadratic → 49:0, 58:2, 9:3. - Open addressing deletes with DELETED markers; keep λ ≤ 0.5-0.7. - Chaining: lists per slot, λ may exceed 1, easy deletion, extra pointer memory. - Rehash to about twice the size (a prime) when λ is too high.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. What is a collision? Explain linear probing, quadratic probing and double hashing. Insert 89, 18, 49, 58, 9 into a table of size 10. (10 Marks)
- Write: the three probe formulas, the three insertion tables, primary and secondary clustering.
- Q2. Explain separate chaining with an example. Compare it with open addressing. (7.5 Marks)
- Write: the chain diagram for 0, 1, 4, …, 81, analysis 1 + λ, and the comparison table.
- Q3. Insert 20, 30, 2, 13, 25, 24, 10, 9 into a table of size 11 by linear probing and find the average number of probes for successful and unsuccessful search. (7.5 Marks)
- Q4. What is rehashing? When is it needed? (2.5 Marks)