CSC211 ยท 2081 ยท 5 marks
What is hashing? how do you apply linear probing and rehashing explain with example.
What is hashing? how do you apply linear probing and rehashing explain with example. [5]
Quick answer
Hashing is a technique used to map data (keys) to specific locations (indices) in a hash table using a hash function. The hash function converts a key into an index within the range of the table size.
Hashing, Linear Probing, and Rehashing
What is Hashing?
General form:
h(x) = x % table_size
The main advantage of hashing is that it allows O(1) average time for insertion, deletion, and search operations.
Hash Collision
A collision occurs when two distinct keys produce the same hash value (i.e., map to the same index). Collision resolution is necessary for correct operation.
Linear Probing
Linear probing is an open addressing collision resolution technique. When a collision occurs at index h(x), the algorithm searches sequentially for the next empty slot in the table.
Formula:
h(x, i) = (h(x) + i) % table_size
where i = 1, 2, 3, ... is the probe number.
Disadvantage: Linear probing suffers from primary clustering -- a group of consecutive occupied slots forms, slowing down future insertions.
Example of Linear Probing
Insert keys: {89, 49, 18, 58} into a hash table of size 10.
Hash function: h(x) = x % 10
| Step | Key | h(x) = x%10 | Action |
|---|---|---|---|
| 1 | 89 | 89%10 = 9 | Index 9 is empty, insert at 9 |
| 2 | 49 | 49%10 = 9 | Collision! Index 9 is full. Try (9+1)%10 = 0 -- empty, insert at 0 |
| 3 | 18 | 18%10 = 8 | Index 8 is empty, insert at 8 |
| 4 | 58 | 58%10 = 8 | Collision! Index 8 is full. Try (8+1)%10 = 9 -- full. Try (8+2)%10 = 0 -- full. Try (8+3)%10 = 1 -- empty, insert at 1 |
Final Hash Table:
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|---|
| Key | 49 | 58 | - | - | - | - | - | - | 18 | 89 |
Rehashing
Rehashing is a collision resolution technique where, upon collision, a new hash function is applied to the key to find another location.
Formula:
h_i(x) = (h(x) + i * h'(x)) % table_size
Or simply, a second independent hash function is used:
h2(x) = R - (x % R)
where R is a prime number smaller than the table size.
Rehashing is also performed when the load factor (ratio of filled slots to table size) becomes too high -- in that case, the table size is doubled and all keys are re-inserted using the new hash function.
Steps in Rehashing:
- Apply the primary hash function
h(x). - If collision occurs, apply a secondary hash function
h2(x). - Probe at positions:
(h(x) + i * h2(x)) % table_sizefor i = 1, 2, 3, ...
Example of Rehashing (Double Hashing)
Insert keys: {89, 49}, table size = 10, R = 7
h(x) = x % 10h2(x) = 7 - (x % 7)
| Key | h(x) | Collision? | h2(x) | New index |
|---|---|---|---|---|
| 89 | 9 | No | -- | Insert at 9 |
| 49 | 9 | Yes | 7-(49%7) = 7-0 = 7 | (9 + 1*7)%10 = 6, Insert at 6 |
Final Table:
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|---|
| Key | - | - | - | - | - | - | 49 | - | - | 89 |
Summary
| Technique | Collision Resolution Method | Problem |
|---|---|---|
| Linear Probing | Next sequential empty slot | Primary clustering |
| Rehashing | Apply a new/second hash function | More computation needed |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.