SlideShare a Scribd company logo
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
• 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 2
• 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 3
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 4
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 5
• Table size=11
• Hash function: h(x)= x mod 11
• Insert keys:20,30,2,13,25,24,10,9
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 6
h(20)= 20 % 11=9
h(30)= 30 % 11=8
h(2)= 2 % 11 =2
h(13)= 13 % 11=2
h(25)= 25 % 11=3
h(24)= 24 % 11=2
h(10)= 10 % 11=10
h(9)= 9 % 11=9
0
1
2
3
4
5
6
7
8
9
10
2
30
20
0
1
2
3
4
5
6
7
8
9
10
2
13
30
20
0
1
2
3
4
5
6
7
8
9
10
2
13
25
30
20
0
1
2
3
4
5
6
7
8
9
10
2
13
25
24
30
20
Insert 13 Insert 25 Insert 24
0
1
2
3
4
5
6
7
8
9
10
2
13
25
24
30
20
10
Insert 10
0
1
2
3
4
5
6
7
8
9
10
9
2
13
25
24
30
20
10
Insert 9
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 7
• 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.
• 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 8
#define MAX 10
int insert_lb(int key)
{
j=key % MAX;
for(i=0;i < MAX ; i++)
{
if(ht[j]== -1)
{
ht[j]=key;
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 9
return(j);
}
j=(j+1)%MAX;
}
return(-1);
}
Procedure to insert key in hash table
• Procedure to search key in hash table
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 10
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;
}
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 11
• For example, consider elements:
131, 3, 4, 21, 61, 6, 71, 8, 9
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 12
0
1
2
3
4
5
6
7
8
9
-1 -1
131 -1
-1 -1
3 -1
4 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
h(131)= 131%10=1
h(3)= 3%10=3
h(4)= 4%10=4
h(21)= 21%10=1
h(61)= 61%10=1
h(6)= 6%10=6
h(71)= 71%10=1
h(8)= 8%10=8
h(9)= 9%10=9
0
1
2
3
4
5
6
7
8
9
-1 -1
131 2
21 5
3 -1
4 -1
61 -1
-1 -1
-1 -1
-1 -1
-1 -1
Insert 21,61
0
1
2
3
4
5
6
7
8
9
-1 -1
131 2
21 5
3 -1
4 -1
61 -1
6 -1
-1 -1
-1 -1
-1 -1
Insert 6
0
1
2
3
4
5
6
7
8
9
-1 -1
131 2
21 5
3 -1
4 -1
61 7
6 -1
71 -1
-1 -1
-1 -1
Insert 71
0
1
2
3
4
5
6
7
8
9
-1 -1
131 2
21 5
3 -1
4 -1
61 7
6 -1
71 -1
8 -1
9 -1
Insert 8,9
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 13
• 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 14
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 15
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
• 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 16
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 17
void insertwor(int b[][2])
{
int r,s,t,a;
printf("n Enter Data");
scanf("%d",&a);
s= a % MAX;
if(b[s][0]==-1)
b[s][0]=a;
else
{
while(1)
{
printf("nTrue Collision Occurs");
getch();
if(b[s][1]== -1)
{
t=s;
while(b[s][0]!=-1)
{
s=(s+1)%MAX;
if(s==t)
{
printf("Overflow");
break;
}
}
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 18
void displaywor(int b[][2])
{
int i,r,s,t,a;
printf("nINDEXtDATAtCHAIN");
for(i=0;i<max;i++)
{
if(b[i][0]!=-1)
{
printf("n%d",i);
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 19
printf("t%d",b[i][0]);
//if(b[i][1]!=-1)
printf("t%d",b[i][1]);
}
}
getch();
}
void searchwor(int b[][2])
{
int r,s,a;
printf("Enter Data Which U Want To Search");
scanf("%d",&a);
s=a%MAX;
while(1)
{
if(b[s][0]==a)
break;
s=b[s][1];
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 20
if(s==-1)
{
printf("Not Found");
break;
}
}//end of while
else
{
printf("nIndex Is %d:",s);
printf("nData Is %d:",b[s][0]);
printf("n Index Which Is Chained From It:%d",b[s][1]);
}
}
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 21
11,32,41,54,33,22
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 22
0
1
2
3
4
5
6
7
8
9
-1 -1
11 3
32 -1
41 -1
54 -1
-1 -1
-1 -1
-1 -1
-1 -1
-1 -1
0
1
2
3
4
5
6
7
8
9
-1 -1
11 5
32 -1
33 -1
54 -1
41 -1
-1 -1
-1 -1
-1 -1
-1 -1
Insert 33
0
1
2
3
4
5
6
7
8
9
-1 -1
11 5
32 6
33 -1
54 -1
41 -1
22 -1
-1 -1
-1 -1
-1 -1
Insert 22
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
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 23
0
1
2
3
4
5
6
7
8
9
10 -1
1 -1
12 6
3 -1
4 9
5 -1
2 -1
7 -1
8 -1
14 -1
Avg. Cost= (1+1+1+1+1+1+1+2+1+2)/no. of buckets
=12/10
=1.2
• Pseudo-code to insert Data in Linear probing with replacement
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 24
• 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 25

More Related Content

Similar to closed hashing.pptx

Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
Sayed Chhattan Shah
 
International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)
IJERD Editor
 
20 convex hull last 7
20 convex hull last 720 convex hull last 7
20 convex hull last 7
Alexander Decker
 
Unit 04
Unit 04Unit 04
Unit 04
Partha_bappa
 
Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure
Eman magdy
 
AD3251-Data Structures Design-Notes-Searching-Hashing.pdf
AD3251-Data Structures  Design-Notes-Searching-Hashing.pdfAD3251-Data Structures  Design-Notes-Searching-Hashing.pdf
AD3251-Data Structures Design-Notes-Searching-Hashing.pdf
Ramco Institute of Technology, Rajapalayam, Tamilnadu, India
 
Oct27
Oct27Oct27
Oct27
Tak Lee
 
Linear timesorting
Linear timesortingLinear timesorting
Merge sort
Merge sortMerge sort
Merge sort
Md. Rakib Trofder
 
What is Algorithm - An Overview
What is Algorithm - An OverviewWhat is Algorithm - An Overview
What is Algorithm - An Overview
Ramakant Soni
 
01_intro-cpp.ppt
01_intro-cpp.ppt01_intro-cpp.ppt
01_intro-cpp.ppt
SWETHAABIRAMIM
 
01_intro-cpp.ppt
01_intro-cpp.ppt01_intro-cpp.ppt
01_intro-cpp.ppt
DrBashirMSaad
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
Tosin Amuda
 
DAA Notes.pdf
DAA Notes.pdfDAA Notes.pdf
DAA Notes.pdf
SauravPawar14
 
Lesson 6 recursion
Lesson 6  recursionLesson 6  recursion
Lesson 6 recursion
MLG College of Learning, Inc
 
Sortsearch
SortsearchSortsearch
SCS-MCSA- Based Architecture for Montgomery Modular Multiplication
SCS-MCSA- Based Architecture for Montgomery Modular MultiplicationSCS-MCSA- Based Architecture for Montgomery Modular Multiplication
SCS-MCSA- Based Architecture for Montgomery Modular Multiplication
IRJET Journal
 
Test Automation Day 2018
Test Automation Day 2018Test Automation Day 2018
Test Automation Day 2018
Maurício Aniche
 
Comparison among Different Adders
Comparison among Different Adders Comparison among Different Adders
Comparison among Different Adders
iosrjce
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
chin463670
 

Similar to closed hashing.pptx (20)

Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
 
International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)
 
20 convex hull last 7
20 convex hull last 720 convex hull last 7
20 convex hull last 7
 
Unit 04
Unit 04Unit 04
Unit 04
 
Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure
 
AD3251-Data Structures Design-Notes-Searching-Hashing.pdf
AD3251-Data Structures  Design-Notes-Searching-Hashing.pdfAD3251-Data Structures  Design-Notes-Searching-Hashing.pdf
AD3251-Data Structures Design-Notes-Searching-Hashing.pdf
 
Oct27
Oct27Oct27
Oct27
 
Linear timesorting
Linear timesortingLinear timesorting
Linear timesorting
 
Merge sort
Merge sortMerge sort
Merge sort
 
What is Algorithm - An Overview
What is Algorithm - An OverviewWhat is Algorithm - An Overview
What is Algorithm - An Overview
 
01_intro-cpp.ppt
01_intro-cpp.ppt01_intro-cpp.ppt
01_intro-cpp.ppt
 
01_intro-cpp.ppt
01_intro-cpp.ppt01_intro-cpp.ppt
01_intro-cpp.ppt
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
 
DAA Notes.pdf
DAA Notes.pdfDAA Notes.pdf
DAA Notes.pdf
 
Lesson 6 recursion
Lesson 6  recursionLesson 6  recursion
Lesson 6 recursion
 
Sortsearch
SortsearchSortsearch
Sortsearch
 
SCS-MCSA- Based Architecture for Montgomery Modular Multiplication
SCS-MCSA- Based Architecture for Montgomery Modular MultiplicationSCS-MCSA- Based Architecture for Montgomery Modular Multiplication
SCS-MCSA- Based Architecture for Montgomery Modular Multiplication
 
Test Automation Day 2018
Test Automation Day 2018Test Automation Day 2018
Test Automation Day 2018
 
Comparison among Different Adders
Comparison among Different Adders Comparison among Different Adders
Comparison among Different Adders
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
 

Recently uploaded

ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
Mukeshwaran Balu
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
PuktoonEngr
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 

Recently uploaded (20)

ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 

closed hashing.pptx

  • 1. 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
  • 2. • 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 2
  • 3. • 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 3
  • 4. 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 4
  • 5. DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 5
  • 6. • Table size=11 • Hash function: h(x)= x mod 11 • Insert keys:20,30,2,13,25,24,10,9 DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 6 h(20)= 20 % 11=9 h(30)= 30 % 11=8 h(2)= 2 % 11 =2 h(13)= 13 % 11=2 h(25)= 25 % 11=3 h(24)= 24 % 11=2 h(10)= 10 % 11=10 h(9)= 9 % 11=9 0 1 2 3 4 5 6 7 8 9 10 2 30 20 0 1 2 3 4 5 6 7 8 9 10 2 13 30 20 0 1 2 3 4 5 6 7 8 9 10 2 13 25 30 20 0 1 2 3 4 5 6 7 8 9 10 2 13 25 24 30 20 Insert 13 Insert 25 Insert 24 0 1 2 3 4 5 6 7 8 9 10 2 13 25 24 30 20 10 Insert 10 0 1 2 3 4 5 6 7 8 9 10 9 2 13 25 24 30 20 10 Insert 9
  • 7. DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 7 • 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.
  • 8. • 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 8
  • 9. #define MAX 10 int insert_lb(int key) { j=key % MAX; for(i=0;i < MAX ; i++) { if(ht[j]== -1) { ht[j]=key; DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 9 return(j); } j=(j+1)%MAX; } return(-1); } Procedure to insert key in hash table
  • 10. • Procedure to search key in hash table DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 10 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; }
  • 11. 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 11
  • 12. • For example, consider elements: 131, 3, 4, 21, 61, 6, 71, 8, 9 DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 12 0 1 2 3 4 5 6 7 8 9 -1 -1 131 -1 -1 -1 3 -1 4 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 h(131)= 131%10=1 h(3)= 3%10=3 h(4)= 4%10=4 h(21)= 21%10=1 h(61)= 61%10=1 h(6)= 6%10=6 h(71)= 71%10=1 h(8)= 8%10=8 h(9)= 9%10=9 0 1 2 3 4 5 6 7 8 9 -1 -1 131 2 21 5 3 -1 4 -1 61 -1 -1 -1 -1 -1 -1 -1 -1 -1 Insert 21,61 0 1 2 3 4 5 6 7 8 9 -1 -1 131 2 21 5 3 -1 4 -1 61 -1 6 -1 -1 -1 -1 -1 -1 -1 Insert 6 0 1 2 3 4 5 6 7 8 9 -1 -1 131 2 21 5 3 -1 4 -1 61 7 6 -1 71 -1 -1 -1 -1 -1 Insert 71 0 1 2 3 4 5 6 7 8 9 -1 -1 131 2 21 5 3 -1 4 -1 61 7 6 -1 71 -1 8 -1 9 -1 Insert 8,9
  • 13. 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 13
  • 14. • 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 14
  • 15. DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 15 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
  • 16. • 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 16
  • 17. DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 17 void insertwor(int b[][2]) { int r,s,t,a; printf("n Enter Data"); scanf("%d",&a); s= a % MAX; if(b[s][0]==-1) b[s][0]=a; else { while(1) { printf("nTrue Collision Occurs"); getch(); if(b[s][1]== -1) { t=s; while(b[s][0]!=-1) { s=(s+1)%MAX; if(s==t) { printf("Overflow"); break; } }
  • 18. 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 18
  • 19. void displaywor(int b[][2]) { int i,r,s,t,a; printf("nINDEXtDATAtCHAIN"); for(i=0;i<max;i++) { if(b[i][0]!=-1) { printf("n%d",i); DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 19 printf("t%d",b[i][0]); //if(b[i][1]!=-1) printf("t%d",b[i][1]); } } getch(); }
  • 20. void searchwor(int b[][2]) { int r,s,a; printf("Enter Data Which U Want To Search"); scanf("%d",&a); s=a%MAX; while(1) { if(b[s][0]==a) break; s=b[s][1]; DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 20 if(s==-1) { printf("Not Found"); break; } }//end of while else { printf("nIndex Is %d:",s); printf("nData Is %d:",b[s][0]); printf("n Index Which Is Chained From It:%d",b[s][1]); } }
  • 21. 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 21
  • 22. 11,32,41,54,33,22 DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 22 0 1 2 3 4 5 6 7 8 9 -1 -1 11 3 32 -1 41 -1 54 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 1 2 3 4 5 6 7 8 9 -1 -1 11 5 32 -1 33 -1 54 -1 41 -1 -1 -1 -1 -1 -1 -1 -1 -1 Insert 33 0 1 2 3 4 5 6 7 8 9 -1 -1 11 5 32 6 33 -1 54 -1 41 -1 22 -1 -1 -1 -1 -1 -1 -1 Insert 22
  • 23. 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 DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 23 0 1 2 3 4 5 6 7 8 9 10 -1 1 -1 12 6 3 -1 4 9 5 -1 2 -1 7 -1 8 -1 14 -1 Avg. Cost= (1+1+1+1+1+1+1+2+1+2)/no. of buckets =12/10 =1.2
  • 24. • Pseudo-code to insert Data in Linear probing with replacement DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 24
  • 25. • 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 25