Two different keys may hash to the same slot. This is a collision. Since the number of possible keys is far larger than the table, collisions cannot be avoided and must be resolved.
Collision
A collision occurs when two keys k1 ≠ k2 give h(k1) = h(k2). The load factor α = n/m, the number of items divided by the table size, measures how full the table is. As α grows, collisions become more frequent and operations slow down. There are two main families of resolution: open addressing and chaining.
Definition: Collision. The situation in which two different keys are mapped by the hash function to the same slot of a hash table.
Open Addressing
In open addressing, all items are kept in the table itself. On a collision, we probe other slots in a fixed order until an empty one is found. In linear probing, we try h(k), h(k) + 1, h(k) + 2, … (mod m). It is simple, but long runs of filled slots form, called primary clustering. In quadratic probing, we try h(k) + 1², h(k) + 2², h(k) + 3², … (mod m), which reduces clustering. In double hashing, the step is given by a second hash function: h(k) + i × h2(k), which gives the best spread. Searching follows the same probe sequence until the key or an empty slot is found. Deletion must mark a slot as deleted rather than empty, or later searches would stop too early. The load factor must stay below 1, and performance is good up to about 0.7.
Chaining
In separate chaining, each slot holds a linked list of all the items that hash to it. On a collision, the new item is simply added to the list in that slot. Search computes the slot and then looks through its list. Chaining is easy to implement, handles a load factor above 1, and deletion is simple. Its cost is the extra memory for the pointers, and long chains if the hash function is poor. The average length of a chain equals the load factor α.
Table 2.6: Collision resolution methods
| Method | Idea | Weakness |
|---|---|---|
| Linear probing | Next slot, then the next | Primary clustering |
| Quadratic probing | Steps 1, 4, 9, … | Secondary clustering |
| Double hashing | Step from a second hash | More computation |
| Chaining | Linked list per slot | Extra pointer memory |
Example 2.6: Linear probing
Problem: Insert 25, 36, 47, 14 into a table of size 11 using h(k) = k mod 11 and linear probing.
Solution:
- 25 → 3. Empty, so place it in slot 3.
- 36 → 3. Full. Try 4. Empty, place it in slot 4.
- 47 → 3. Full. 4 full. Try 5, place it in slot 5.
- 14 → 3. Full. 4, 5 full. Try 6, place it in slot 6.
Result: Slots 3, 4, 5, 6 hold 25, 36, 47, 14, showing a cluster forming.
#include <stdio.h>
#define M 11
int main(void) {
int table[M], keys[] = {25, 36, 47, 14, 20};
for (int i = 0; i < M; i++) table[i] = -1;
for (int i = 0; i < 5; i++) {
int s = keys[i] % M;
while (table[s] != -1) s = (s + 1) % M;
table[s] = keys[i];
}
for (int i = 0; i < M; i++) printf("%d:%d ", i, table[i]);
printf("\n");
return 0;
}
Output
0:-1 1:-1 2:-1 3:25 4:36 5:47 6:14 7:-1 8:-1 9:20 10:-1
Common mistakes
- Marking a deleted slot as empty in open addressing, which breaks later searches.
- Letting the load factor reach 1 in open addressing, so that insertion fails.
- Using linear probing with a poor hash function, which makes clusters worse.
Key takeaways
- A collision is two keys in one slot; the load factor is α = n/m.
- Open addressing probes the table: linear, quadratic, double hashing.
- Chaining keeps a linked list in each slot.
- Delete in open addressing by marking the slot as deleted.
Practice questions
- What is primary clustering?
- How does chaining resolve a collision?
- What is the load factor?
Answers to the practice questions
- The forming of long runs of occupied slots in linear probing.
- Each slot holds a linked list, and colliding items are added to that list.
- The number of items divided by the number of slots, α = n/m.
Sources
- Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit II
