0% found this document useful (0 votes)
24 views

Closed Hashing-ADS

The document discusses open addressing or closed hashing techniques for resolving collisions in hash tables. It describes linear probing as a method for open addressing where the next empty slot is used to store a colliding element. The document provides examples and procedures to insert and search elements in a hash table using linear probing.

Uploaded by

Aditya More
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Closed Hashing-ADS

The document discusses open addressing or closed hashing techniques for resolving collisions in hash tables. It describes linear probing as a method for open addressing where the next empty slot is used to store a colliding element. The document provides examples and procedures to insert and search elements in a hash table using linear probing.

Uploaded by

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

Sanjivani Rural Education Society’s

Sanjivani College of Engineering, Kopargaon-423 603


(An Autonomous Institute, Affiliated to Savitribai Phule Pune University, Pune)
NACC ‘A’ Grade Accredited, ISO 9001:2015 Certified

Department of Computer Engineering


(NBA Accredited)

Subject- Data Structures-I(CO203)


Unit III- Closed Hashing

Prof. B.B.Kotame
Assistant Professor
E-mail : [email protected]
Contact No:9561010331
• Open Addressing/Closed Hashing:

1. Separate chaining requires additional memory space for pointers.

2. With this method, a hash collision is resolved by probing, or searching through


alternative locations in the array until an empty bucket is found.

3. As all the elements are stored inside the table, a large memory space is
needed for open addressing.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


• There are three different popular methods for open addressing techniques. These
methods are −

• Linear Probing

• Quadratic Probing

• Double Hashing

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


Linear Probing
• In this method, hash address of an identifier x is obtained

• If collision occurs, the identifier is place in the next empty slot after its home
bucket

• If slot hash(x) % S is full, then we try (hash(x) + 1) % S

• If (hash(x) + 1) % S is also full, then we try (hash(x) + 2) % S

• If (hash(x) + 2) % S is also full, then we try (hash(x) + 3) % S

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon
• Table size=11
• Hash function: h(x)= x mod 11 Insert 13 Insert 25 Insert 24 Insert 10 Insert 9

• Insert keys:20,30,2,13,25,24,10,9 0 0 0 0 0 0 9
1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2
h(20)= 20 % 11=9 3 3 13 3 13 3 13 3 13 3 13
h(30)= 30 % 11=8 4 4 4 25 4 25 4 25 4 25
h(2)= 2 % 11 =2 5 5 5 5 24 5 24 5 24
h(13)= 13 % 11=2 6 6 6 6 6 6

h(25)= 25 % 11=3 7 7 7 7 7 7

h(24)= 24 % 11=2 8 30 8 30 8 30 8 30 8 30 8 30
h(10)= 10 % 11=10 9 20 9 20 9 20 9 20 9 20 9 20
h(9)= 9 % 11=9 10 10 10 10 10 10 10 10

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


• Let us see the following example to get better idea. If we have some elements like
{15, 47, 23, 34, 85, 97, 65, 89, 70}. And our hash function is h(x) = x mod 7.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


• Advantages :

1. Simple to implement.

2. Best case time complexity=O(1)

• Disadvantages :

1. If an element is not found at computed location. Searching goes in linear way.

2. Hash table may become Full.

3. Make clustering: many consecutive elements form groups.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


Procedure to insert key in hash table

#define MAX 10 return(j);


int insert_lb(int key) }
{ j=(j+1)%MAX;
j=key % MAX;
}
for(i=0;i < MAX ; i++)
return(-1);
{
if(ht[j]== -1) }
{
ht[j]=key;

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


• Procedure to search key in hash table
int search_lb()
{
cout<<“Enter the key to be search”;
cin>>key;
j= key % MAX;
for(i=0;i<MAX;i++)
{
if(ht[j]==key)
return(j);
else
j=(j+1)%MAX;
}
return -1;
}

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


Linear Probing with Chaining
• In this method, another field get added into the table.

• In this field, the address of the colliding data can be stored with the first colliding
element in the chain table, without replacement.

• Records can be easily located.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


• For example, consider elements:
131, 3, 4, 21, 61, 6, 71, 8, 9
Insert 21,61 Insert 6 Insert 71 Insert 8,9
0 -1 -1 0 -1 -1 0 -1 -1 0 -1 -1 0 -1 -1
131 2 131 2 131 2 1 131 2
h(131)= 131%10=1 1 131 -1 1 1 1

h(3)= 3%10=3 2 -1 -1 2 21 5 2 21 5 2 21 5 2 21 5

h(4)= 4%10=4 3 3 -1 3 3 -1 3 3 -1 3 3 -1 3 3 -1

h(21)= 21%10=1 4 4 -1 4 4 -1 4 4 -1 4 4 -1 4 4 -1

h(61)= 61%10=1 5 -1 -1 5 61 -1 5 61 -1 5 61 7 5 61 7

h(6)= 6%10=6 6 -1 -1 6 -1 -1 6 6 -1 6 6 -1 6 6 -1

h(71)= 71%10=1 7 -1 -1 7 -1 -1 7 -1 -1 7 71 -1 7 71 -1
h(8)= 8%10=8 8 -1 -1 8 -1 -1 8 -1 -1 8 -1 -1 8 8 -1
h(9)= 9%10=9 9 -1 -1 9 -1 -1 9 -1 -1 9 -1 -1 9 9 -1

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


From the example, you can see that the chain is maintained from the number who
demands for location 1.
First number 131 comes, we place it at index 1. Next comes 21, but collision occurs
so by linear probing we will place 21 at index 2, and chain is maintained by writing
2 in chain table at index 1.
Similarly next comes 61, by linear probing we can place 61 at index 5 and chain will
maintained at index 2.
Thus any element which gives hash key as 1 will be stored by linear probing at
empty location but a chain is maintained so that traversing the hash table will be
efficient.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


• Insert following keys into hash table with linear probing with chaining without
replacement method. Use H(x)= x % 10 function

1. 0,1,4,7,64,89,11,33,55 Use H(x)= x % 10 function

2. 10,12,20,18,15 Use H(x)= x % 8 function

3. 11,32,41,54,33,1 Use H(x)= x % 10 function

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


0 0

1 1

2 2

3 3

4 4

5 5

6 6

7 7

8 8

9 9

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


• Disadvantages:

1. Main purpose of hashing is not achieved, because all records are not
mapped at correct position or mapped at wrong position/address.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


void insertwor(int b[][2]) if(b[s][1]== -1)
{ {
int r,s,t,a; t=s;
printf("\n Enter Data"); while(b[s][0]!=-1)
scanf("%d",&a); {
s= a % MAX; s=(s+1)%MAX;
if(b[s][0]==-1) if(s==t)
b[s][0]=a; {
else printf("Overflow");
{ break;
while(1) }
{ }
printf("\nTrue Collision Occurs");
getch();
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon
if(s!=t)
{
b[s][0]=a;
b[t][1]=s;
}
break;
} //if end
else
s=b[s][1];
}//while end
}//else end
}

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


void displaywor(int b[][2]) printf("\t%d",b[i][0]);
{ //if(b[i][1]!=-1)
int i,r,s,t,a; printf("\t%d",b[i][1]);
printf("\nINDEX\tDATA\tCHAIN"); }
for(i=0;i<max;i++) }
{ getch();
if(b[i][0]!=-1) }
{
printf("\n%d",i);

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


if(s==-1)
void searchwor(int b[][2])
{
{
printf("Not Found");
int r,s,a;
break;
printf("Enter Data Which U Want To Search");
}
scanf("%d",&a);
}//end of while
s=a%MAX;
else
while(1)
{
{
printf("\nIndex Is %d:",s);
if(b[s][0]==a)
printf("\nData Is %d:",b[s][0]);
break;
printf("\n Index Which Is Chained From It:%d",b[s][1]);
s=b[s][1];
}
}
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon
Linear Probing with Replacement
• In this method, if another identifier ‘Y’ is occupying the position of an identifier
‘X’, X replaces it and then ‘Y’ is relocated to a new position.

• e.g. 11,32,41,54,33

• First 3 will get placed at address 1,2,3,4. but when 33 need to be placed it
position is occupied by 41.

• Therefore 33 replaces 41 and 41 inserted in new empty bucket i.e. 5 and chain
from element 11 at position 1 is modified

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


11,32,41,54,33,22
Insert 33 Insert 22
0 -1 -1 0 -1 -1 0 -1 -1
1 11 3 1 11 5 1 11 5
2 32 -1 2 32 -1 2 32 6
3 41 -1 3 33 -1 3 33 -1
4 54 -1 4 54 -1 54 -1
4
5 -1 -1 5 41 -1 5 41 -1
6 -1 -1 6 -1 -1
6 22 -1
7 -1 -1 7 -1 -1
7 -1 -1
8 -1 -1 8 -1 -1
8 -1 -1
9 -1 -1 9 -1 -1
9 -1 -1

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


Insert following data 12,1,4,3,7,8,10,2,5,14 using linear probing with chaining with
replacement. Calculate average cost or no. of comparisons
0 10 -1
1 1 -1 Avg. Cost= (1+1+1+1+1+1+1+2+1+2)/no. of buckets
2 12 6
3 -1
=12/10
3
4 4 9 =1.2
5 5 -1
6 2 -1
7 7 -1
8 8 -1
9 14 -1

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


• Pseudo-code to insert Data in Linear probing with replacement

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon


• The load factor for an open addressing technique should be 0.5. For separate
chaining technique, the load factor is 1.

DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon

You might also like