Collision
Collision
In this article, we will discuss about Open Addressing.
Open Addressing-
In open addressing,
Unlike separate chaining, all the keys are stored inside the hash table.
No key is stored outside the hash table.
Techniques used for open addressing are-
Linear Probing
Quadratic Probing
Double Hashing
Operations in Open Addressing-
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-
To search any particular key,
Delete Operation-
NOTE-
During insertion, the buckets marked as “deleted” are treated like any other
empty bucket.
During searching, the search is not terminated on encountering the bucket
marked as “deleted”.
The search terminates only after the required key or an empty bucket is found.
Open Addressing Techniques-
Techniques used for open addressing are-
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.
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-
50, 700, 76, 85, 92, 73 and 101
Use linear probing technique for collision resolution.
Solution-
The given sequence of keys will be inserted in the hash table as-
Step-01:
Step-02:
Step-03:
Step-04:
Step-05:
Step-06:
Step-07:
Step-08:
This technique creates a linked list to the slot for which collision occurs.
The new key is then inserted in the linked list.
These linked lists to the slots appear like chains.
That is why, this technique is called as separate chaining.
Time Complexity-
For Searching-
In worst case, all the keys might map to the same bucket of the hash table.
In such a case, all the keys will be present in a single linked list.
Sequential search will have to be performed on the linked list to perform the
search.
So, time taken for searching in worst case is O(n).
For Deletion-
In worst case, the key might have to be searched first and then deleted.
In worst case, time taken for searching is O(n).
So, time taken for deletion in worst case is O(n).
If Load factor (α) = constant, then time complexity of Insert, Search, Delete = Θ(1)
Problem-
Using the hash function ‘key mod 7’, insert the following sequence of keys in the hash
table-
50, 700, 76, 85, 92, 73 and 101
Use separate chaining technique for collision resolution.
Solution-
The given sequence of keys will be inserted in the hash table as-
Step-01:
Step-02:
Step-03:
Step-04:
Step-05:
Step-06:
Step-07:
Step-08: