CO4 - Hashing in Data Structure
CO4 - Hashing in Data Structure
Hash Function-
Hash function is a function that maps any big number or string to a small integer value.
Hash function takes the data item as an input and returns a small integer value as an
output.
The small integer value is called as a hash value.
Hash value of the data item is then used as an index for storing it into the hash table.
In this method firstly key is squared and then mid part of the result is taken as the index. For
example: consider that if we want to place a record of 3101 and the size of table is 1000. So
3101*3101=9616201 i.e. h (3101) = 162 (middle 3 digit)
In this method the key is divided into separate parts and by using some simple operations
these parts are combined to produce a hash key. For example: consider a record of 12465512
then it will be divided into parts i.e. 124, 655, 12. After dividing the parts combine these parts
by adding it.
H(key)=124+655+12
=791
Collision
It is a situation in which the hash function returns the same hash key for more than one
record, it is called as collision. Sometimes when we are going to resolve the collision it may
lead to a overflow condition and this overflow and collision condition makes the poor hash
function.
1) Linear probing
It is very easy and simple method to resolve or to handle the
collision. In this collision can be solved by placing the second
record linearly down, whenever the empty place is found. In this
method there is a problem of clustering which means at some
place block of a data is formed in a hash table.
In this we can see if we insert 67, 90, and 55 it can be inserted easily
but at case of 17 hash function is used in such a manner that :-
(7+0*0)%10=7 (when i=0 it provide the index value 7 only) by
making the increment in value of i. let i =1 so (7+1*1)%10=8.in this
case bucket 8 is empty hence we will place 17 at index 8.
3) Double hashing
It is a technique in which two hash function are used when there is
an occurrence of collision. In this method 1 hash function is simple as
same as division method. But for the second hash function there are two
important rules which are
1. It must never evaluate to zero.
2. Must sure about the buckets, that they are probed.
The hash functions for this technique are:
H1(key)= (key % table size + i * H2(key))% table size
Whereas H2(key)=P-(key mod P); i=0,1..<size
Where, P is a prime number which should be taken smaller than the size
of a hash table.
Step-02:
Insert the given keys in the hash table one by one.
The first key to be inserted in the hash table = 50.
Bucket of the hash table to which key 50 maps = 50 mod 7 = 1.
So, key 50 will be inserted in bucket-1 of the hash table as above.
Keep other locations as “-1”.
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 above
Keep other locations as “-1”.
Step-04:
The next key to be inserted in the hash table = 76.
Bucket of the hash table to which key 76 maps = 76 mod 7 = 6.
So, key 76 will be inserted in bucket-6 of the hash table as above.
Keep other locations as “-1”.
Step-05:
The next key to be inserted in the hash table = 85.
Bucket of the hash table to which key 85 maps = 85 mod 7 = 1.
Since bucket-1 is already occupied, so collision occurs.
To handle the collision, linear probing technique keeps probing
linearly until an empty bucket is found.
The first empty bucket is bucket-2.
So, key 85 will be inserted in bucket-2 of the hash table as above.
Keep other locations as “-1”.
Step-06:
The next key to be inserted in the hash table = 92.
Bucket of the hash table to which key 92 maps = 92 mod 7 = 1.
Since bucket-1 is already occupied, so collision occurs.
To handle the collision, linear probing technique keeps probing
linearly until an empty bucket is found.
The first empty bucket is bucket-3.
So, key 92 will be inserted in bucket-3 of the hash table as above.
Keep other locations as “-1”.
Step-07:
The next key to be inserted in the hash table = 73.
Bucket of the hash table to which key 73 maps = 73 mod 7 = 3.
Since bucket-3 is already occupied, so collision occurs.
To handle the collision, linear probing technique keeps probing
linearly until an empty bucket is found.
The first empty bucket is bucket-4.
So, key 73 will be inserted in bucket-4 of the hash table as above.
Keep other locations as “-1”.
Step-08:
The next key to be inserted in the hash table = 101.
Bucket of the hash table to which key 101 maps = 101 mod 7 = 3.
Since bucket-3 is already occupied, so collision occurs.
To handle the collision, linear probing technique keeps probing
linearly until an empty bucket is found.
The first empty bucket is bucket-5.
So, key 101 will be inserted in bucket-5 of the hash table as above.
PRACTICE PROBLEM BASED ON SEPARATE CHAINING-
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:
Draw an empty hash table with NULL value.
For the given hash function, the possible range of hash values is [0, 6].
So, draw an empty hash table consisting of 7 buckets as above.
Keep all locations as NULL
Step-02:
Insert the given keys in the hash table one by one.
The first key to be inserted in the hash table = 50.
Bucket of the hash table to which key 50 maps = 50 mod 7 = 1.
So, key 50 will be inserted in bucket-1 of the hash table as above.
Keep other locations as NULL
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 above.
Keep other locations as NULL
Step-04:
The next key to be inserted in the hash table = 76.
Bucket of the hash table to which key 76 maps = 76 mod 7 = 6.
So, key 76 will be inserted in bucket-6 of the hash table as above.
Keep other locations as NULL
Step-05:
The next key to be inserted in the hash table = 85.
Bucket of the hash table to which key 85 maps = 85 mod 7
= 1.
Since bucket-1 is already occupied, so collision occurs.
Separate chaining handles the collision by creating a linked
list to bucket-1.
So, key 85 will be inserted in bucket-1 of the hash table as
above.
Keep other locations as NULL
Step-06:
The next key to be inserted in the hash table = 92.
Bucket of the hash table to which key 92 maps = 92 mod 7
= 1.
Since bucket-1 is already occupied, so collision occurs.
Separate chaining handles the collision by creating a linked
list to bucket-1.
So, key 92 will be inserted in bucket-1 of the hash table as
above.
Keep other locations as NULL
Step-07:
The next key to be inserted in the hash table = 73.
Bucket of the hash table to which key 73 maps = 73 mod 7
= 3.
So, key 73 will be inserted in bucket-3 of the hash table as
above.
Keep other locations as NULL
Step-08:
The next key to be inserted in the hash table = 101.
Bucket of the hash table to which key 101 maps = 101
mod 7 = 3.
Since bucket-3 is already occupied, so collision
occurs.
Separate chaining handles the collision by creating a
linked list to bucket-3.
So, key 101 will be inserted in bucket-3 of the hash
table as above.
Keep other locations as NULL.
Implementation of Hashing – Linear Probing
package Hashing;
import java.util.*;
int hi=ele%size;
if (HT[hi][key] == -1)
{
HT[hi][key] = ele;
}
else
{
for(hi=hi+1; hi<size; hi++)
{
if(HT[hi][key] == -1)
{
HT[hi][key] = ele;
break;
}
}
}
}
public static void display()
{
int i;
System.out.println("The elements in hash table are as follows");
System.out.println("Hash key(index)\t\tKeys");
for(i=0;i<size;i++)
System.out.println("H_table["+ HT[i][index] + "] =" + HT[i][key]);
}
for(i=0;i<size;i++)
{
if(HT[i][key] == ele)
{
flag=1;
break;
}
}
if(flag==1)
System.out.println("Key is Found");
else
System.out.println("Key is NOT found");
}
if(ht[hi]==null)
{
ht[hi]=newnode;
}
else
{
Htable p;
for(p=ht[hi];p.next !=null ;p=p.next);
p.next = newnode;
}
}