Collision in Hashing
Collision in Hashing
1) Separate Chaining
The idea is to make each cell of the hash table point to a linked list of records that have
the same hash function value. Chaining is simple but requires additional memory
outside the table.
Example:
We have given a hash function and we have to insert some elements in the hash table
using a separate chaining method for collision resolution technique.
Hash function = key % 5,
Elements = 12, 15, 22, 25 and 37.
Let’s see step by step approach to how to solve the above problem:
Step 1: First draw the empty hash table which will have a possible range of hash
values from 0 to 4 according to the hash function provided.
Hash table
Step 2: Now insert all the keys in the hash table one by one. The first key to be
inserted is 12 which is mapped to bucket number 2 which is calculated by using the
hash function 12%5=2.
Insert 12
Step 3: Now the next key is 22. It will map to bucket number 2 because 22%5=2.
But bucket 2 is already occupied by key 12.
Insert 22
Step 4: The next key is 15. It will map to slot number 0 because 15%5=0.
Insert 15
Step 5: Now the next key is 25. Its bucket number will be 25%5=0. But bucket 0 is
already occupied by key 25. So separate chaining method will again handle the
collision by creating a linked list to bucket 0.
Insert 25
Hence In this way, the separate chaining method is used as the collision resolution
technique.
2) Open Addressing
In open addressing, all elements are stored in the hash table itself. Each table entry
contains either a record or NIL. When searching for an element, we examine the table
slots one by one until the desired element is found or it is clear that the element is not in
the table.
2. a) Linear Probing
In linear probing, the hash table is searched sequentially that starts from the original
location of the hash. If in case the location that we get is already occupied, then we
check for the next location.
Algorithm:
1. Calculate the hash key. i.e. key = data % size
2. Check, if hashTable[key] is empty
store the value directly by hashTable[key] = data
3. If the hash index already has some value then
check for next index using key = (key+1) % size
4. Check, if the next index is available hashTable[key] then store the value. Otherwise
try for next index.
5. Do the above process till we find the space.
Example: Let us consider a simple hash function as “key mod 5” and a sequence of
keys that are to be inserted are 50, 70, 76, 85, 93.
Step 1: First draw the empty hash table which will have a possible range of hash
values from 0 to 4 according to the hash function provided.
Hash table
Step 2: Now insert all the keys in the hash table one by one. The first key is 50. It
will map to slot number 0 because 50%5=0. So insert it into slot number 0.
Step 4: The next key is 76. It will map to slot number 1 because 76%5=1 but 70 is
already at slot number 1 so, search for the next empty slot and insert it.
Step 5: The next key is 93 It will map to slot number 3 because 93%5=3, So insert it
into slot number 3.
Insert 93 into hash table