Quadratic Probing
Problem Statement
Given a hash function, Quadratic probing is used to find the correct index of the element in the hash table. To eliminate the Primary clustering problem in Linear probing, Quadratic probing in data structure uses a Quadratic polynomial hash function to resolve the collisions in the hash table.
Example
Let there a table of size = 10 with slot position index i=0, 1, 2, 3, 4, 5 ,6 ,7,8,9 as shown below.
pos | keys |
---|---|
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 |
Let the sequence of keys = 9 ,19 ,29 ,39 ,49 ,59,71 These keys are to be inserted into the hash table.
The hash function for indexing, , where k = key value.
If quadratic probing is used for collision resolution then find the positions of each of the key elements in the hash table.
After collision Resolution the final positions of the element in the hash table will look like this:
index | keys |
---|---|
0 | 19 |
1 | 71 |
2 | |
3 | 29 |
4 | 59 |
5 | 49 |
6 | |
7 | |
8 | 39 |
9 | 9 |
Let's understand how the positions of these keys are calculated and quadratic probing is used to resolve collision in the next section.
Example Explanation
Let's understand for each key how its index is calculated in the Hash table.
The hash function for indexing is . While inserting an element in the hash table if the index position in the hash table is already occupied then the collision is resolved by the quadratic hash function as :
%
where is the next index for the key K. i is the collision number. For first collision i=1, For second collision i=2, For third collision, i=3, and so on. This is calculated until a space is assigned to the key element in the hash table.
Let's find the index value of each element in the sequence of key = 9, 19, 29,39, 49, 59,71
- K=9
the hash value can be calculated for this key by the hash function . % (available)
so, k=9 is inserted at index 9 in the hash table. as shown below
index | keys |
---|---|
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | 9 |
- K=19
the hash value can be calculated for this key by the hash function .
% (First collision) As index 9 is already occupied by key = 9 so next index is calculated by quadratic hash function % (i=1 for first collision)
% % (this index position is available in the hash table) So, K=19 is inserted at index 0 in the hash table as shown below
index | keys |
---|---|
0 | 19 |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | 9 |
- K=29
the hash value can be calculated for this key by the hash function .
% (First collision)
As index 9 is already occupied by key = 9 so next index is calculated by quadratic hash function % (i=1 for first collision)
% = % (Second collision) As index 0 is already occupied by key = 19 so next index is calculated by quadratic hash function % (i=2 for second collision)
%% (available)
So, K=29 is inserted at index 3the in the hash table.
index | keys |
---|---|
0 | 19 |
1 | |
2 | |
3 | 29 |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | 9 |
- K= 3the 9
the hash value can be calculated for this key by the hash function .
% (First collison)
As index 9 is already occupied by key = 9 so next index is calculated by quadratic hash function % (i=1 for first collision)
%%(Second collision) As index 0 is already occupied by key = 19 so next index is calculated by quadratic hash function % (i=2 for second collision)
(Third collision)
As index 3 is already occupied by key = 29 so next index is calculated by quadratic hash function % (i=3 for third collision)
%%(available)
So, K=39 is inserted at index 8 in the hash table
index | keys |
---|---|
0 | 19 |
1 | |
2 | |
3 | 29 |
4 | |
5 | |
6 | |
7 | |
8 | 39 |
9 | 9 |
- K= the 49 the hash value can be calculated for this key by the hash function .
% (First collison)
As index 9 is already occupied by key = 9 so next index is calculated by quadratic hash function % (i=1 for first collision)
%%(Second collision)
As index 0 is already occupied by key = 19 so next index is calculated by quadratic hash function % (i=2 for second collision)
(Third collision)
As index 3 is already occupied by key = 29 so next index is calculated by quadratic hash function % (i=3 for third collision)
%%(Fourth collsion)
As index 8 is already occupied by key = 39 so next index is calculated by quadratic hash function % (i=4 for fourth collision)
%% (available)
So,K=49 is inserted at index 5 in the hash table
index | keys |
---|---|
0 | 19 |
1 | |
2 | |
3 | 29 |
4 | |
5 | 49 |
6 | |
7 | |
8 | 39 |
9 | 9 |
- K=59
the hash value can be calculated for this key by the hash function .
% (First collison)
As index 9 is already occupied by key = 9 so next index is calculated by quadratic hash function % (i=1 for first collision)
%%(Second collision)
As index 0 is already occupied by key = 19 so next index is calculated by quadratic hash function % (i=2 for second collision)
(Third collision)
As index 3 is already occupied by key = 29 so next index is calculated by quadratic hash function % (i=3 for third collision)
%%(Fourth collsion)
As index 8 is already occupied by key = 39 so next index is calculated by quadratic hash function % (i=4 for fourth collision)
%% (Fifth collsion)
As index 5 is already occupied by key = 49 so next index is calculated by quadratic hash function % (i=5 for fourth collision)
%% (available)
So, K=59 is inserted at index 4 in the hash table
index | keys |
---|---|
0 | 19 |
1 | |
2 | |
3 | 29 |
4 | 59 |
5 | 49 |
6 | |
7 | |
8 | 39 |
9 | 9 |
- K=71 % (available)(No collision) So, K=71 is inserted at index 1the the in hash table
index | keys |
---|---|
0 | 19 |
1 | 71 |
2 | |
3 | 29 |
4 | 59 |
5 | 49 |
6 | |
7 | |
8 | 39 |
9 | 9 |
The final Key allocation in the hash table is
index | keys |
---|---|
0 | 19 |
1 | 71 |
2 | |
3 | 29 |
4 | 59 |
5 | 49 |
6 | |
7 | |
8 | 39 |
9 | 9 |
Constraints
The only constraint is number of Keys should be less than the hash table size.
For example, if the number of keys = 8 and Size of hash table = 5
After filling in the 5 positions in the hash table the collision problem for the rest of the 3 elements will never be resolved so the collisions will never be resolved.
How Quadratic Probing is Done?
While allocating the position to the element or key in the hash table firstly the index position is calculated using the hash function % where , S = size of hash table
Now, if the slot calculated by the hash function H(k) is full then a collision occurs and the collision is resolved by % for the first collision.
If slot calculted by % is also full then again collsion is resolved by % for second collsion.
If the slot calculated by % is also full then again collision is resolved by % for the third collision.
This process of resolving collision is continued until a vacant slot is calculated for the key.
Approach
Algorithm :
- Iterate over the given array of key
- Calculate the hash value with the help of the hash function.
- If there is no collision then insert the key at that hash value index in the hash table.
- If there is a collision iterate through all possible quadratic values.
- Compute the new hash value using the Quadratic hash function.
- Insert the key if the empty slot is found else again calculate the hash value with the quadratic hash function.
Implementation in C++:
Implementation in Java:
Implementation in Python:
- Output for the program will be:
- Time complexity of Quadratic probing algorithm :
The time complexity of the quadratic probing algorithm will be . where N is the number of keys to be inserted and S is the size of the hash table.
Best case - when there are no collisions in the insertion of all the keys in the hash table then all the elements will be inserted in one single insertion.
Worst Case - In worst case occurs when there is collision in insertion of every other element after the first element is inserted then for each of the other element the hash value for quadratic probing need to be calculated each time collision occurs.
- Space Complexity :
The space complexity of the quadratic probing algorithm will be as no extra space is being used in the algorithm.
In both best case and worst case no extra space is required other than the hash table which is already given in the problem requirement so the space complexity remains in both best abd worst case.
Learn More
You can learn more about hashing in data structure before understanding quadratic probing in data structure refer to this article
Conclusion
- Quadratic probing is a method to resolve collision while inserting an element/key in the hash table
- Primary clustering problem can be eliminated by quadratic probing.
- The hash function for ith collision in quadratic probing is %
- Time complexity of implementing the quadratic probing algorithm is where N is no of the keys to be inserted and S is the size of the hash table.
- The space complexity of quadratic probing algorithm is in both best and worst case.
FAQ
The frequently asked questions in Quadratic probing in the data structure are:
Q.What is quadratic probing and how it is used in hashing?
A.Given a hash function, Quadratic probing is used for finding the correct index of the element in the hash table. It is used in hashing to resolve collisions in the hash table.
Q.How hash value is calculated for more than one collision in quadratic probing?
A.The hash value for more than one collision is calculated by quadratic probing hash function given as % where is the next index for the key K. i is the collision number.
Q.What are the advantages of quadratic probing?
A.uadratic probing avoids the primary clustering problem which occurs in linear probing.
Q.How collision is resolved in hashing by quadratic probing?
A.Collision is resolved by finding the new position of the element to be inserted in hash table with the help of quadratic probing hash function.