SlideShare a Scribd company logo
HASHING
(Separate chaining, Closed Hashing)
K.HARIPRITHA.
M.Sc(Info Tech).
Nadar Saraswathi college of arts and science.
Synopsis:
 SEPARATE CHAINING
 Open Hashtable.
 Type declaration for separate chaining.
 Insert routine for separate chaining Hash
Table
 Initialization routine for separate chaining
Hash table.
OPEN ADDRESSING
Linear Probing
Quadric Probing
Double Hashing
SEPARATE CHANNING
(Open hashing)
 Open hashing is to keep a list of all elements that hash to
the same value.
 In the space is tight , it might be preferable to avoid their
use.
 In this section that the keys are the first 10 perfect squares
and that the hash function is simply Hash(X)=X mod 10.
 That we use the hash function to determine which list to
traverse this list in the normal manner , returning the
position where the item is found.
OPEN HASH TABLE
 To be involve insert. we travese down the appropriate list to
check then the element is placed.
 If the element turns out to be new it is inserted either at the
front of the list or at the end of the list.
 This is easily addressed while the code is being written.In the
new element are sometimes are inserted at thre front of the list.The class
specification required to implement the open hashing.
 Last item is accessed by the accessing item in the current list
with the ()operator.
 The lists are automatically initialized by the list constructor.
 Routines to copy and reinitialize are provided. Line 4 through 6 allocate a
hash table structure.
 H will be point to s structure containing an integer and a pointer to a
list.Line 7 sets table size to a prime number and lines 8 through 10 attempt to
allocate an array of list.
 Implementation uses header allocate one header per list and set its next field
to NULL.
Type declaration for separate chaining
hash table
#ifndef_hashsep_H
Struct ListNode;
Typedef struct ListNode *Position;
Struct HashTbl;
typedef struct HashTbl *Hash Table;
HashTable Initialize (int Tablesize);
Void DestroyTable (HashTable H);
Position Find (ElementType Key,
HashTable);
Void insert(ElementType key, HashTable
H);
Element type retrive(position p);
/*Routines such as Delete and make
empty are omitted */
#endif /*_Hashsep_H*/
/* Place in the implementation file*/
struct ListNode
{
ElementType Element;
Position Next;
};
Typedef Position Lists;
/*List *The List will be an array of
lists,allocated later
/*The lists use header (for simplicity,*),*/
/*though this wastes space*/
Struct HashTb
{
int TableSize;
Lists*TheLists;
};
Insert routine for separate chaining Hash table
Void
Insert(ElementType key,HashTable H)
{
Position pos,Newcell;
List L;
/*1*/ Pos=Find(key, H);
/*2*/if (pos==NULL)/*Key is not found*/
/*3*/Newcell=malloc(sizeof(struct ListNode));
/*4*/if(Newcell==NULL)
/*5*/ Fatalerror(“Out of space”);
else
{
/*6*/ L=H->The Lists[Hash(Key,H->TableSize)];
/*7*/Newcell->Next=L->Next;
/*8*/Newcell->Element=key; /*Probably need strcpy!*/
/*9*/L->Next =NewCell;
}
}
}
Initialization routine for separate
chaining hash table
HashTable
InitializeTable(int Tablesize)
{
HashTable H;
int I;
/*1*/ if (TableSize<MinTableSize)
{
/*2*/ Error (“table size too small”);
/*3*/ return NULL;
}
/*Allocate table*/
/*4*/H=malloc(sizeof(struct HashTbl));
/*5*/if(H==NULL)
/*6*/FatalError(“Out of space!!!”);
/*7*/H->Tablesize = Nextprime(TableSize)
/*Allocate arraay of list*/
/*8*/H->The lists =malloc (sizeof(List)*H-
>TableSize);
/*9*/if (H->TheLists==NULL)
/*10*/ FatalError(“Out of space!!!”);
/*Allocate list header*/
/*11*/ for (i=0;i<H->TableSize;i++)
{
/*12*/H->The Lists [i]=malloc(Sizeof(struct
ListNode));
/*13*/if (H->The lists[i]==NULL)
/*14*/FatalError(“Out of space!!!”);
else
/*15*/ H->The lists[i]->Next=NULL;
}
/*16*/ return H;
}
 The deletion routine is a straightforward implementation
of deletion in a linked list,so will not bother with it here.
 If the table is large and the hash function is good,all the
lists should be short so it is not worthwhile to try anything
cmplicated.
 The loaded factor ʎ of a hash table to the ratio of the
number element to the hash table to the table
size.(ex:above ʎ=1.0. The average length of the list is ʎ)
 The number of the link to traverse is ʎ on average.A
successful search requires that about 1+(ʎ+2)link be
traversed.
 The general rule for separate chaining hashing is to make
the table size about the large as the number of the element
expected
OPEN ADDRESSING
 Open addressing, or closed hashing, is a method
of collision resolution in hash tables.
Open addressing hashing is an alternative to resolving
collisions with linked list.
Separate chaining hashing has the disadvantage of using
linked lists.
The algorithm down a bit because of the time to allocate
new cells.
Its essentially requires the implements of a second data
structure.
Cells h0(x),h1(x),h2(x)... N.
F(0)=0 the function f, is the collision
resolution strategy.
The load factor ℷ=0.5
hi(x)=(hash(x)+f(i))
LINEAR PROBING
The amounts to trying cells sequentially in search of
empty cell.
The result of inserting keys {89,18,49,58,69} into a
hash table using the same hash function.
The collision resolution strategy ,f(i)=i.
The first collision occurs when 49 is inserted; in spot
0,which is open.
Unsuccessful search ½(1+1/(1-ℷ)2)
Successful search ½(1+1/(1-ℷ))
0
1
2
3
4
5
6
7
8
9
 Linear Probing: after
checking spot h(k), try
spot h(k)+1, if that is full,
try h(k)+2, then h(k)+3,
etc.
Insert:
38
19
8
109
10
OPEN ADDRESSING HASH TABLE WITH
LINEAR PROBING
Empty table After 89 After 18 After 49 After 58 After 69
0 49 49 49
1 58 58
2 69
3
4
5
6
7
8 18 18 18 18
9 89 89 89 89 89
Quadratic Probing
f(i) = i2
Probe sequence:
0th probe = h(k) mod Table size
1th probe = (h(k) + 1) mod Table size
2th probe = (h(k) + 4) mod Table Size
3th probe = (h(k) + 9) mod Table Size
. . .
ith probe = (h(k) + i2) mod Table Size
Quadratic Probing:
Success guarantee for  < ½
• show for all 0  i,j  size/2 and i  j
(h(x) + i2) mod size  (h(x) + j2) mod size
• by contradiction: suppose that for some i  j:
(h(x) + i2) mod size = (h(x) + j2) mod size
 i2 mod size = j2 mod size
 (i2 - j2) mod size = 0
 [(i + j)(i - j)] mod size = 0
Because size is prime(i-j)or (i+j) must be zero, and
neither can be
OPEN ADDRESSING HASH TABLE WITH
QUADRATIC PROBING
Empty table After 89 After 18 After 49 After 58 After 69
0 49 49 49
1
2 58 58
3 69
4
5
6
7
8 18 18 18 18
9 89 89 89 89 89
DOUBLE HASHING
The last collision resolution method
examine is double hashing.
Double hashing f(i)=i⋅hash2(x).
Hash function to x and probe at a distance
hash2(x),2hash2(x)…,
A function such as hash2(x)=R-(x mod R),
with R a prime smaller than Table Size.
f(i) = i * g(k)
where g is a second hash function
Probe sequence:
0th probe = h(k) mod Table Size
1th probe = (h(k) + g(k)) mod Table Size
2th probe = (h(k) + 2*g(k)) mod Table Size
3th probe = (h(k) + 3*g(k)) mod Table Size
. . .
ith probe = (h(k) + i*g(k)) mod Table Size
Resolving Collisions with Double
Hashing
 Insert these values into the hash table in this order.
Resolve any collisions with double hashing:
13
28
33
147
43
Hash Functions:
H(K) = K mod M
H2(K) = 1 + ((K/M) mod (M-1))
THANK YOU

More Related Content

What's hot

Binary Search
Binary SearchBinary Search
Binary Search
kunj desai
 
Product Cipher
Product CipherProduct Cipher
Product Cipher
SHUBHA CHATURVEDI
 
Algorithm Using Divide And Conquer
Algorithm Using Divide And ConquerAlgorithm Using Divide And Conquer
Algorithm Using Divide And Conquer
UrviBhalani2
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
HTML
HTMLHTML
Collision in Hashing.pptx
Collision in Hashing.pptxCollision in Hashing.pptx
Collision in Hashing.pptx
NBACriteria2SICET
 
Website Design Issues
Website Design IssuesWebsite Design Issues
Website Design Issues
rakudepp
 
Graph coloring problem
Graph coloring problemGraph coloring problem
Graph coloring problem
V.V.Vanniaperumal College for Women
 
Subset sum problem(dp)
Subset sum problem(dp)Subset sum problem(dp)
Subset sum problem(dp)
VishnuPratap7
 
Parallel computing and its applications
Parallel computing and its applicationsParallel computing and its applications
Parallel computing and its applications
Burhan Ahmed
 
Extensible hashing
Extensible hashingExtensible hashing
Extensible hashing
rajshreemuthiah
 
Hash table
Hash tableHash table
Hash table
Vu Tran
 
AES by example
AES by exampleAES by example
AES by example
Shiraz316
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structureSajid Marwat
 
Approximation algorithms
Approximation  algorithms Approximation  algorithms
Approximation algorithms
Bipesh Raj Subedi
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
ManishPrajapati78
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
sana younas
 
Hashing
HashingHashing
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
Shubham Dwivedi
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
bca2010
 

What's hot (20)

Binary Search
Binary SearchBinary Search
Binary Search
 
Product Cipher
Product CipherProduct Cipher
Product Cipher
 
Algorithm Using Divide And Conquer
Algorithm Using Divide And ConquerAlgorithm Using Divide And Conquer
Algorithm Using Divide And Conquer
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
HTML
HTMLHTML
HTML
 
Collision in Hashing.pptx
Collision in Hashing.pptxCollision in Hashing.pptx
Collision in Hashing.pptx
 
Website Design Issues
Website Design IssuesWebsite Design Issues
Website Design Issues
 
Graph coloring problem
Graph coloring problemGraph coloring problem
Graph coloring problem
 
Subset sum problem(dp)
Subset sum problem(dp)Subset sum problem(dp)
Subset sum problem(dp)
 
Parallel computing and its applications
Parallel computing and its applicationsParallel computing and its applications
Parallel computing and its applications
 
Extensible hashing
Extensible hashingExtensible hashing
Extensible hashing
 
Hash table
Hash tableHash table
Hash table
 
AES by example
AES by exampleAES by example
AES by example
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Approximation algorithms
Approximation  algorithms Approximation  algorithms
Approximation algorithms
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
 
Hashing
HashingHashing
Hashing
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 

Similar to Open addressing &amp rehashing,extendable hashing

Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
AgonySingh
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
kratika64
 
Hashing
HashingHashing
Hashing
LavanyaJ28
 
11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm
farhankhan89766
 
Hash function
Hash functionHash function
Hash function
MDPiasKhan
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
rajshreemuthiah
 
Lec5
Lec5Lec5
Concurrent Hashing and Natural Parallelism : The Art of Multiprocessor Progra...
Concurrent Hashing and Natural Parallelism : The Art of Multiprocessor Progra...Concurrent Hashing and Natural Parallelism : The Art of Multiprocessor Progra...
Concurrent Hashing and Natural Parallelism : The Art of Multiprocessor Progra...
Subhajit Sahu
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
rajshreemuthiah
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
Krish_ver2
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7chidabdu
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
TutorialsDuniya.com
 
Presentation Of Analysis
Presentation Of Analysis  Presentation Of Analysis
Presentation Of Analysis
Mahnoor Altaf Khan Afridi
 
Data Structure and Algorithms: What is Hash Table ppt
Data Structure and Algorithms: What is Hash Table pptData Structure and Algorithms: What is Hash Table ppt
Data Structure and Algorithms: What is Hash Table ppt
JUSTFUN40
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec IISajid Marwat
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Kuntal Bhowmick
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashingchidabdu
 

Similar to Open addressing &amp rehashing,extendable hashing (20)

Hashing
HashingHashing
Hashing
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
Hashing
HashingHashing
Hashing
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
Hashing
HashingHashing
Hashing
 
11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm
 
Hash function
Hash functionHash function
Hash function
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
Lec5
Lec5Lec5
Lec5
 
Concurrent Hashing and Natural Parallelism : The Art of Multiprocessor Progra...
Concurrent Hashing and Natural Parallelism : The Art of Multiprocessor Progra...Concurrent Hashing and Natural Parallelism : The Art of Multiprocessor Progra...
Concurrent Hashing and Natural Parallelism : The Art of Multiprocessor Progra...
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
 
Hash pre
Hash preHash pre
Hash pre
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
 
Presentation Of Analysis
Presentation Of Analysis  Presentation Of Analysis
Presentation Of Analysis
 
Data Structure and Algorithms: What is Hash Table ppt
Data Structure and Algorithms: What is Hash Table pptData Structure and Algorithms: What is Hash Table ppt
Data Structure and Algorithms: What is Hash Table ppt
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec II
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
 

More from Haripritha

Mapreduce script
Mapreduce scriptMapreduce script
Mapreduce script
Haripritha
 
Wireless sensor networks
Wireless sensor networksWireless sensor networks
Wireless sensor networks
Haripritha
 
Datamining
DataminingDatamining
Datamining
Haripritha
 
Operating system
Operating systemOperating system
Operating system
Haripritha
 
Java
JavaJava
Presentation 2
Presentation 2Presentation 2
Presentation 2
Haripritha
 
Computer Organization
Computer OrganizationComputer Organization
Computer Organization
Haripritha
 
programming language in c&c++
programming language in c&c++programming language in c&c++
programming language in c&c++
Haripritha
 
encoding
encodingencoding
encoding
Haripritha
 

More from Haripritha (9)

Mapreduce script
Mapreduce scriptMapreduce script
Mapreduce script
 
Wireless sensor networks
Wireless sensor networksWireless sensor networks
Wireless sensor networks
 
Datamining
DataminingDatamining
Datamining
 
Operating system
Operating systemOperating system
Operating system
 
Java
JavaJava
Java
 
Presentation 2
Presentation 2Presentation 2
Presentation 2
 
Computer Organization
Computer OrganizationComputer Organization
Computer Organization
 
programming language in c&c++
programming language in c&c++programming language in c&c++
programming language in c&c++
 
encoding
encodingencoding
encoding
 

Recently uploaded

How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 

Recently uploaded (20)

How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 

Open addressing &amp rehashing,extendable hashing

  • 1. HASHING (Separate chaining, Closed Hashing) K.HARIPRITHA. M.Sc(Info Tech). Nadar Saraswathi college of arts and science.
  • 2. Synopsis:  SEPARATE CHAINING  Open Hashtable.  Type declaration for separate chaining.  Insert routine for separate chaining Hash Table  Initialization routine for separate chaining Hash table. OPEN ADDRESSING Linear Probing Quadric Probing Double Hashing
  • 3. SEPARATE CHANNING (Open hashing)  Open hashing is to keep a list of all elements that hash to the same value.  In the space is tight , it might be preferable to avoid their use.  In this section that the keys are the first 10 perfect squares and that the hash function is simply Hash(X)=X mod 10.  That we use the hash function to determine which list to traverse this list in the normal manner , returning the position where the item is found.
  • 5.  To be involve insert. we travese down the appropriate list to check then the element is placed.  If the element turns out to be new it is inserted either at the front of the list or at the end of the list.  This is easily addressed while the code is being written.In the new element are sometimes are inserted at thre front of the list.The class specification required to implement the open hashing.  Last item is accessed by the accessing item in the current list with the ()operator.  The lists are automatically initialized by the list constructor.  Routines to copy and reinitialize are provided. Line 4 through 6 allocate a hash table structure.  H will be point to s structure containing an integer and a pointer to a list.Line 7 sets table size to a prime number and lines 8 through 10 attempt to allocate an array of list.  Implementation uses header allocate one header per list and set its next field to NULL.
  • 6. Type declaration for separate chaining hash table #ifndef_hashsep_H Struct ListNode; Typedef struct ListNode *Position; Struct HashTbl; typedef struct HashTbl *Hash Table; HashTable Initialize (int Tablesize); Void DestroyTable (HashTable H); Position Find (ElementType Key, HashTable); Void insert(ElementType key, HashTable H); Element type retrive(position p); /*Routines such as Delete and make empty are omitted */ #endif /*_Hashsep_H*/ /* Place in the implementation file*/ struct ListNode { ElementType Element; Position Next; }; Typedef Position Lists; /*List *The List will be an array of lists,allocated later /*The lists use header (for simplicity,*),*/ /*though this wastes space*/ Struct HashTb { int TableSize; Lists*TheLists; };
  • 7. Insert routine for separate chaining Hash table Void Insert(ElementType key,HashTable H) { Position pos,Newcell; List L; /*1*/ Pos=Find(key, H); /*2*/if (pos==NULL)/*Key is not found*/ /*3*/Newcell=malloc(sizeof(struct ListNode)); /*4*/if(Newcell==NULL) /*5*/ Fatalerror(“Out of space”); else { /*6*/ L=H->The Lists[Hash(Key,H->TableSize)]; /*7*/Newcell->Next=L->Next; /*8*/Newcell->Element=key; /*Probably need strcpy!*/ /*9*/L->Next =NewCell; } } }
  • 8. Initialization routine for separate chaining hash table HashTable InitializeTable(int Tablesize) { HashTable H; int I; /*1*/ if (TableSize<MinTableSize) { /*2*/ Error (“table size too small”); /*3*/ return NULL; } /*Allocate table*/ /*4*/H=malloc(sizeof(struct HashTbl)); /*5*/if(H==NULL) /*6*/FatalError(“Out of space!!!”); /*7*/H->Tablesize = Nextprime(TableSize) /*Allocate arraay of list*/ /*8*/H->The lists =malloc (sizeof(List)*H- >TableSize); /*9*/if (H->TheLists==NULL) /*10*/ FatalError(“Out of space!!!”); /*Allocate list header*/ /*11*/ for (i=0;i<H->TableSize;i++) { /*12*/H->The Lists [i]=malloc(Sizeof(struct ListNode)); /*13*/if (H->The lists[i]==NULL) /*14*/FatalError(“Out of space!!!”); else /*15*/ H->The lists[i]->Next=NULL; } /*16*/ return H; }
  • 9.  The deletion routine is a straightforward implementation of deletion in a linked list,so will not bother with it here.  If the table is large and the hash function is good,all the lists should be short so it is not worthwhile to try anything cmplicated.  The loaded factor ʎ of a hash table to the ratio of the number element to the hash table to the table size.(ex:above ʎ=1.0. The average length of the list is ʎ)  The number of the link to traverse is ʎ on average.A successful search requires that about 1+(ʎ+2)link be traversed.  The general rule for separate chaining hashing is to make the table size about the large as the number of the element expected
  • 10. OPEN ADDRESSING  Open addressing, or closed hashing, is a method of collision resolution in hash tables. Open addressing hashing is an alternative to resolving collisions with linked list. Separate chaining hashing has the disadvantage of using linked lists. The algorithm down a bit because of the time to allocate new cells. Its essentially requires the implements of a second data structure.
  • 11. Cells h0(x),h1(x),h2(x)... N. F(0)=0 the function f, is the collision resolution strategy. The load factor ℷ=0.5 hi(x)=(hash(x)+f(i))
  • 12. LINEAR PROBING The amounts to trying cells sequentially in search of empty cell. The result of inserting keys {89,18,49,58,69} into a hash table using the same hash function. The collision resolution strategy ,f(i)=i. The first collision occurs when 49 is inserted; in spot 0,which is open. Unsuccessful search ½(1+1/(1-ℷ)2) Successful search ½(1+1/(1-ℷ))
  • 13. 0 1 2 3 4 5 6 7 8 9  Linear Probing: after checking spot h(k), try spot h(k)+1, if that is full, try h(k)+2, then h(k)+3, etc. Insert: 38 19 8 109 10
  • 14. OPEN ADDRESSING HASH TABLE WITH LINEAR PROBING Empty table After 89 After 18 After 49 After 58 After 69 0 49 49 49 1 58 58 2 69 3 4 5 6 7 8 18 18 18 18 9 89 89 89 89 89
  • 15. Quadratic Probing f(i) = i2 Probe sequence: 0th probe = h(k) mod Table size 1th probe = (h(k) + 1) mod Table size 2th probe = (h(k) + 4) mod Table Size 3th probe = (h(k) + 9) mod Table Size . . . ith probe = (h(k) + i2) mod Table Size
  • 16. Quadratic Probing: Success guarantee for  < ½ • show for all 0  i,j  size/2 and i  j (h(x) + i2) mod size  (h(x) + j2) mod size • by contradiction: suppose that for some i  j: (h(x) + i2) mod size = (h(x) + j2) mod size  i2 mod size = j2 mod size  (i2 - j2) mod size = 0  [(i + j)(i - j)] mod size = 0 Because size is prime(i-j)or (i+j) must be zero, and neither can be
  • 17. OPEN ADDRESSING HASH TABLE WITH QUADRATIC PROBING Empty table After 89 After 18 After 49 After 58 After 69 0 49 49 49 1 2 58 58 3 69 4 5 6 7 8 18 18 18 18 9 89 89 89 89 89
  • 18. DOUBLE HASHING The last collision resolution method examine is double hashing. Double hashing f(i)=i⋅hash2(x). Hash function to x and probe at a distance hash2(x),2hash2(x)…, A function such as hash2(x)=R-(x mod R), with R a prime smaller than Table Size.
  • 19. f(i) = i * g(k) where g is a second hash function Probe sequence: 0th probe = h(k) mod Table Size 1th probe = (h(k) + g(k)) mod Table Size 2th probe = (h(k) + 2*g(k)) mod Table Size 3th probe = (h(k) + 3*g(k)) mod Table Size . . . ith probe = (h(k) + i*g(k)) mod Table Size
  • 20. Resolving Collisions with Double Hashing  Insert these values into the hash table in this order. Resolve any collisions with double hashing: 13 28 33 147 43 Hash Functions: H(K) = K mod M H2(K) = 1 + ((K/M) mod (M-1))