0% found this document useful (0 votes)
130 views13 pages

CO4 - Hashing in Data Structure

Hashing is a technique used to store and search data in an efficient manner. It works by using a hash function to map data to integer indices in a hash table. A good hash function distributes data uniformly while minimizing collisions. Common hash functions include division, mid-square, and digit folding. Collisions occur when different data maps to the same index. They are resolved using open addressing techniques like linear probing, quadratic probing or separate chaining which uses linked lists. Hashing provides constant time lookups making it very efficient compared to other search methods.

Uploaded by

Gopal Kamineni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views13 pages

CO4 - Hashing in Data Structure

Hashing is a technique used to store and search data in an efficient manner. It works by using a hash function to map data to integer indices in a hash table. A good hash function distributes data uniformly while minimizing collisions. Common hash functions include division, mid-square, and digit folding. Collisions occur when different data maps to the same index. They are resolved using open addressing techniques like linear probing, quadratic probing or separate chaining which uses linked lists. Hashing provides constant time lookups making it very efficient compared to other search methods.

Uploaded by

Gopal Kamineni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

Hashing in Data Structure

 In data structures,


 Hashing is a well-known technique to search any particular element among several
elements.
 It minimizes the number of comparisons while performing the search.
 Advantages:
Unlike other searching techniques,
 Hashing is extremely efficient.
 The time taken by it to perform the search does not depend upon the total number of
elements.
 It completes the search with constant time complexity O(1).
 Hashing Mechanism:
 In hashing,
 An array data structure called as Hash table is used to store the data items.
 Based on the hash key value, data items are inserted into the hash table.
 Hash Key Value: 
 Hash key value is a special value that serves as an index for a data item.
 It indicates where the data item should be be stored in the hash table.
 Hash key value is generated using a hash function.
 

 
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.

 Types of Hash Functions-


 There are various types of hash functions available such as-
1. Division Hash Function
2. Mid Square Hash Function
3. Folding Hash Function etc
 
It depends on the user which hash function he wants to use.
 
Properties of Hash Function-
 The properties of a good hash function are-
 It is efficiently computable.
 It minimizes the number of collisions.
 It distributes the keys uniformly over the table.

Types of hash function


There are various types of hash function which are used to place the data in a hash table,
1. Division method
In this the hash function is dependent upon the remainder of a division. For example:-if the
record 52,68,99,84 is to be placed in a hash table and let us take the table size is 10.
Then:
h(key)=record% table size.
2=52%10
8=68%10
9=99%10
4=84%10

2. Mid square method

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)

3. Digit folding method

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.

Collision resolution technique


If there is a problem of collision occurs then it can be handled by apply some technique.
These techniques are called as collision resolution techniques. There are generally four
techniques which are described below.

 In Hashing, collision resolution techniques are classified as-

I ) Open Addressing (Close Hashing)

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.

Example: Let us consider a hash table of size 10 and hash


function is defined as
H(key)=((key % table size)+i)%table size).
Whereas i=0,1..<size
Consider that following keys are to be inserted that are
56,64,36,71.
In this diagram we can see that 56 and 36 need to be placed at
same bucket but by linear probing technique the records linearly
placed downward if place is empty i.e. it can be seen 36 is placed
at index 7.
2) Quadratic probing

This is a method in which solving of clustering problem is done. In


this method the hash function is defined by the

H(key)=((key % table size + i*i) % table size


Whereas i=0,1..<size

Let us consider we have to insert following elements that are:-67,


90,55,17,49.

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.

Example: Let us consider we have to insert 67, 90,55,17,49.


In this we can see 67, 90 and 55 can be inserted in a hash table by using first hash function
but in case of 17 again the bucket is full and in this case we have to use the second hash
function which is H2(key)=P-(key mode P) here p is a prime number which should be taken
smaller than the hash table so value of p will be the 7.
i.e. H2(17)=7- (17%7) = 7-3 = 4 that means H1(key)= (7+4)%10=1, so key jumps for placing
the 17. Therefore 17 will be placed at index 1.

II) Separate Chaining (Open Hashing)


1) Chaining: It is a method in which additional field
with data i.e. chain is introduced. A chain is maintained
at the home bucket. In this when a collision occurs then
a linked list is maintained for colliding data.
Example: Let us consider a hash table of size 10 and
we apply a hash function of H(key)=key % size of
table. Let us take the keys to be inserted are
31,33,77,61. In the above diagram we can see at same bucket 1 there are two records which
are maintained by linked list or we can say by chaining method.
PRACTICE PROBLEM BASED ON OPEN ADDRESSING-
 
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:
 
 Draw an empty hash table having “-1” in all 0 to 6 locations.
 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.
 

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.*;

public class LinearProbing


{
public static Scanner s=new Scanner(System.in);
public static final int size=10;
public static final int index=0;
public static final int key=1;
public static int HT[][]= new int[size][2];

public static void Initialization()


{
for(int i=0;i<size;i++)
{
HT[i][index] = i;
HT[i][key] = -1;
}
}

public static void insert()


{
System.out.println("Enter the value of key\n");
int ele=s.nextInt();

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]);
}

public static void find()


{
int i,flag=0;
System.out.println("Enter the elelment");
int ele=s.nextInt();

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");
}

public static void main(String args[])


{
Initialization();
while(true)
{
System.out.println("\n*** Main Menu ***");
System.out.println("\n1.Insert\n2.Find\n3.Display\n4.Exit\n");
System.out.println("Enter your choice :");
int cho=s.nextInt();
switch(cho)
{
case 1: insert();break;
case 2: find();break;
case 3: display(); break;
case 4: System.exit(0);
default:System.out.println("Pls Enter from 1 to 4");
}
}
}
}
Implementation of SEPARATE CHAINING
package Hashing;
public class Htable
{
int key;
Htable next;
}
--------------------------------------------------------------------------------
package Hashing;
import java.util.Scanner;
public class SeparateChaining
{
public static Scanner s=new Scanner(System.in);
public static final int size=10;
public static Htable ht[]=new Htable[size];

public static void Initialization()


{
for(int i=0;i<size;i++)
{
ht[i]=null;
}
}

public static void insert()


{
System.out.println("Enter the value of Key");
int ele=s.nextInt();
int hi=ele%size;
Htable newnode=new Htable();
newnode.key = ele;
newnode.next=null;

if(ht[hi]==null)
{
ht[hi]=newnode;
}
else
{
Htable p;
for(p=ht[hi];p.next !=null ;p=p.next);
p.next = newnode;
}
}

public static void find()


{
int flag=0;
System.out.println("Enter the element to be found");
int n=s.nextInt();
Htable p;
for(p=ht[n%size];p!=null;p=p.next)
{
if(p.key==n)
{ flag=1;
break;
}
}
if(flag==1)
System.out.println("Found");
else
System.out.println("Not Found");
}
public static void display()
{
System.out.println("\n***The Hash table***\n\n HashTable[Hash Key]\t
Chain of Key's\n");
for(int i=0;i<size;i++)
{
if(ht[i] == null)
System.out.print("Hash_Table[" + i +"] = null");
else
{
System.out.print("Hash_Table[" + i +"] = ");
Htable p;
for(p=ht[i]; p != null; p =p.next)
System.out.print("-> "+ p.key);
}
System.out.println("");
}
}
public static void main(String arg[])
{
Initialization();
while(true)
{
System.out.println("\n*** Main Menu ***");
System.out.println("\n1.Insert\n2.Find\n3.Display\n4.Exit\n");
System.out.println("Enter your choice :");
int cho=s.nextInt();
switch(cho)
{
case 1:insert();break;
case 2:find(); break;
case 3:display();break;
case 4:System.exit(0);
default: System.out.println("Enter the number from 1 to 4");
}
}
} }

You might also like