Hashing: An efficient Searching
Searching
Can we decrease the time for searching?
Hashing
Index /Hash value | Actual Data |
0 | 1000 |
1 | 201 |
2 | 702 |
3 | |
… | |
… | |
99 | 699 |
Hash function
Key
Actual Data
Hash value/Hash key/ Index
generated
Hash table
Applications of Hash table
Bucket
Issues in Hashing
An example
Student records having Roll numbers
Students from various colleges
Hash Function
Properties of a good hash function
Different Hash functions
* The methods marked in Bold are important for exam
Direct Hashing
Subtraction method
Modulo-Division method
Modulo-Division method Example
Modulo-Division method- Strings as Keys
for (i=0;i<key.length();i++)
hashVal += key.charAt(i);
17
Pros and Cons
Extraction / Truncation method
Multiplication method
h(k) = floor[ m (kA mod 1) ]
Multiplication method - Example
= 100 (76.018059 mod 1)
= 100 (0.018059)
= 1
Pros and Cons of Multiplication method
Mid-square method
Mid-square method - example
Suppose, List_size = 100
So, r = 2, as 2 digits are required to map the keys to indices
Folding method
Folding method
Radix Transformation method
Rotation method
Original Key | Rotation | Rotated Key |
600351 | 351 | 135 |
600352 | 352 | 235 |
600353 | 353 | 335 |
Pseudorandom method
= (2061539 + 7) %307 +1
= (2061546) %307 + 1
= 41 + 1
= 42
Universal hashing
Collision Resolution Techniques
Collision
Avoidance vs. Resolution
Strategies for Collision Resolution
Chaining
0 | / |
1 | / |
2 | / |
3 | / |
4 | / |
5 | / |
6 | / |
7 | / |
8 | / |
9 | / |
10
/
22
/
86
/
12
/
42
/
What will happen in worst case?
Load factor?
0 | |
1 | / |
2 | |
3 | / |
4 | / |
5 | / |
6 | |
7 | / |
8 | / |
9 | / |
10
/
42
86
/
12
22
/
Deletion in separate chaining
Closed Hashing/Open Addressing
Closed Hashing/Open Addressing: Linear Probing
Separate chaining does not use all the space in the table. Why not use it?
How to deal with collisions?
If h(key) is already full,
try (h(key) + 1) % TableSize. If full,
try (h(key) + 2) % TableSize. If full,
try (h(key) + 3) % TableSize. If full…
Example: insert 38, 19, 8, 79, 10
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
Delete
Numerical
Numerical
Linear probing with chaining – without replacement
Index | Key | Chain |
0 | | -1 |
1 | | -1 |
2 | | -1 |
3 | | -1 |
4 | | -1 |
5 | | -1 |
6 | | -1 |
7 | | -1 |
8 | | -1 |
9 | | -1 |
| | |
Index | Key | Chain |
0 | | -1 |
1 | | -1 |
2 | 42 | -1 |
3 | 3 | 4 |
4 | 33 | 5 |
5 | 63 | 7 |
6 | 45 | -1 |
7 | 93 | -1 |
8 | | -1 |
9 | 89 | -1 |
| | |
Linear Probing with chaining – with replacement
Index | Key | Chain |
0 | | -1 |
1 | | -1 |
2 | 42 | -1 |
3 | 3 | 4 |
4 | 33 | 6 |
5 | 45 | -1 |
6 | 63 | 7 |
7 | 93 | -1 |
8 | | -1 |
9 | 89 | -1 |
| | |
Load factor
Problem with Linear Probing
It turns out linear probing is a bad idea, even �though the probe function is quick to compute �(which is a good thing)
Quadratic Probing
For quadratic probing, f(i) = i2:
0th probe: (h(key) + 0) % TableSize
1st probe: (h(key) + 1) % TableSize
2nd probe: (h(key) + 4) % TableSize
3rd probe: (h(key) + 9) % TableSize
…
ith probe: (h(key) + i2) % TableSize
Intuition: Probes quickly "leave the neighborhood"
Quadratic Probing - Example
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | 89 |
0 | 49 |
1 | |
2 | 58 |
3 | 79 |
4 | |
5 | |
6 | |
7 | |
8 | 18 |
9 | 89 |
Problem with Quadratic Probing
only four different values!
Double Hashing
Double Hashing - Pseudorandom
Example…
0 | |
1 | 4371 |
2 | |
3 | 1323 |
4 | 6173 |
5 | 9699 |
6 | |
7 | 4344 |
8 | |
9 | 4199 |
Pros and Cons
Double Hashing – Key Offset
Key Offset - Example
Collision Resolution Techniques - Summary
Summary
Summary - Definitions