In hashing, collisions occur when two or more keys yield the same hash value or hash code. Collisions are unavoidable because hash algorithms transfer a theoretically unlimited collection of keys to a finite range of hash results.
We have certain collision strategies that we can use to resolve these problems.
The collision techniques are as follows:
When a collision occurs in open addressing, a new slot within the hash table is found to store the item. There are various methods for doing this, including linear probing, quadratic probing, and double hashing.
h(k, i) = (h'(k) + i) % m
Example:
Suppose we have a hash table of size 7 and want to insert the following keys:
18 | 14 | 21 | 44 |
Our Hash Functo would be:
h(k) = k % 7
i.e,
h(18) = 4
h(14) = 0
h(21) = 0
h(44) = 2
As we can see to insert 21, we encounter a collision at slot 0, so we use linear probing to find the next available slot, which is slot 1.
Output:
Slot | Keys |
---|---|
0 | 14 |
1 | 21 |
2 | 44 |
3 | |
4 | 18 |
5 | |
6 | |
7 |
h(k, i) = (h'(k) + c1 * i + c2 * i^2) % m
Example:
Considering the above example-
When we try to insert 41, we encounter a collision at slot 6, so we use quadratic probing to find the next available slot. The next probe is at slot (6 + 11 + 31^2) % 7 = 3. However, slot 3 is not empty, so we continue probing with i = 2, which gives us slot (6 + 12 + 32^2) % 7 = 5. The final hash table will look like this:
Slot | Key |
---|---|
0 | 41 |
1 | 22 |
2 | 44 |
3 | |
4 |
h(k, i) = (h1(k) + i * h2(k)) % m
In chaining, each hash table slot holds a linked list of items with the same hash code. When there is a collision, the new item is added to the linked list in the slot that corresponds to its hash code.
Working:
Here’s how chaining works in more detail:
Suppose we have a hash table of size 5, and we want to insert the following keys-
10 | 22 | 37 | 45 | 52 |
The the Hash Function would be:
h(k) = k % 5
i.e,
h(10) = 0
h(22) = 2
h(37) = 2
h(45) = 0
h(52) = 2
Since keys 10 and 45 hash to the same slot, we create a linked list containing both keys in slot 0. Keys 22, 37, and 52 all hash to slot 2, so we create a linked list in that slot containing those keys.
Output: final hash table
Slot | Keys |
---|---|
0 | 10, 45 |
1 | |
2 | 22, 37, 52 |
3 | |
4 |
Note: also read about DSA: Collision in Hashing
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…