Collision Resolution Techniques
Collision Resolution Techniques
1. Separate Chaining
2. Open Addressing
The number of keys to be stored in the hash The number of keys to be stored in the hash
table can even exceed the size of the hash table. table can never exceed the size of the hash table.
Some buckets of the hash table are never used Buckets may be used even if no key maps to
which leads to wastage of space. those particular buckets.
Which is the Preferred Technique?
The performance of both the techniques depend on the kind of operations that are required to
be performed on the keys stored in the hash table-
Separate Chaining-
Separate Chaining is advantageous when it is required to perform all the following operations
on the keys stored in the hash table-
Insertion Operation
Deletion Operation
Searching Operation
Open Addressing-
Open addressing is advantageous when it is required to perform only the following operations on
the keys stored in the hash table-
Insertion Operation
Searching Operation
Before you go through this article, make sure that you have gone through the previous article
on Collision Resolution Techniques.
We have discussed-
Hash value is then used as an index to store the key in the hash table.
In case of collision,
Search Operation-
Delete Operation-
The key is first searched and then deleted.
Insert Operation-
Hash function is used to compute the hash value for a key to be inserted.
Hash value is then used as an index to store the key in the hash table.
In case of collision,
Search Operation-
Delete Operation-
1. Linear Probing-
In linear probing,
Advantage-
It is easy to compute.
Disadvantage-
Time Complexity-
This is because-
Even if there is only one element present and all other elements are deleted.
Then, “deleted” markers present in the hash table makes search the entire table.
2. Quadratic Probing-
In quadratic probing,
3. Double Hashing-
In double hashing,
We use another hash function hash2(x) and look for i * hash2(x) bucket in ith iteration.
It requires more computation time as two hash functions need to be computed.
Number of Probe
Sequence m m m2
(m = size of table)
Conclusions-
Linear Probing has the best cache performance but suffers from clustering.
Quadratic probing lies between the two in terms of cache performance and clustering.
Double caching has poor cache performance but no clustering.
In open addressing, the value of load factor always lie between 0 and 1.
This is because-
In open addressing, all the keys are stored inside the hash table.
So, size of the table is always greater or at least equal to the number of keys stored in the
table.
Problem-
Using the hash function ‘key mod 7’, insert the following sequence of keys in the hash table-
The given sequence of keys will be inserted in the hash table as-
Step-01:
Step-02:
Step-03:
The next key to be inserted in the hash table = 700.
Bucket of the hash table to which key 700 maps = 700 mod 7 = 0.
So, key 700 will be inserted in bucket-0 of the hash table as-
Step-04:
Step-05:
Step-07:
The next key to be inserted in the hash table = 73.