SlideShare a Scribd company logo
HASHING
PRESENTED BY:
RUMAISA TASNEEM
160420742013
CONTENTS
 Introduction
 Advantages
 Hash function
 Collision Resolution techniques
 Pigeon Hole Principle
 Open Addressing
 Linear Probing
 Quadratic Probing
 Double hashing
 Separate Chaining
Introduction
 Hashing is an important data structure which is designed to use a special function called
Hash function which is used to map a given value with a particular key for faster access of
elements.
 In Hashing, large keys are converted into small keys using hash functions.
 The values are then stored in a data structure called hash table.
 The idea of Hashing is to distribute entries (key/value pairs) uniformly across an array.
 Each element is assigned a key(converted key).
 By using that key you can access the element in O(1) time. Using the key, the
algorithm(hash function) computes an index that suggests where a entry can be found or
inserted.
 Hashing is implemented into two steps:
• An element is converted into an integer by using a hash function. This element can be
used as an index to sore the original element, which falls into the hash table.
• The element is stored in the hash table where it can be quicky retrieved using hashed
key.
• Hash=hashfunc(key).
• Index=hash % array_size.
 Advantages:
• The main advantage of hash tables over other data structures is speed i.e O(1).
• This advantage is more apparent when the number of entries is large that may be
thousands or more.
Hash function
 A hash function is any function that can be used to map a data set of an arbitrary
size to a data set of fixed size, which falls into the hash table.
 The values returned by a hash function are called hash values, hash codes, hash
sums, or simply hashes.
 To achieve good hashing mechanism, it is important to have a good hash function
with the following basic requirements such as:
• Easy to compute.
• Uniform distribution
• Less collision
Collision Resolution techniques
 Collision: when two keys map to the same location in the hash table. collisions
occur when two keys, k1 and k2, are not equal, but h(k1) = h(k2).
 Two ways to resolve collisions:
 Separate Chaining (open hashing)
 Open Addressing (closed hashing )
• Linear probing.
• Quadratic probing.
• Double hashing.
Pigeon Hole Principle
 The pigeonhole principle states that if n items are
put into m containers, with n>m ,then at least one
container must contain more than one item.
 Pigeons in holes, here there are n=10 pigeons and in
m=9that is there are 9 holes. Since 10 is greater than
9,the pigeon hole principle says that at least one hole
has more than one pigeon.
 Pigeon Hole Principle says given n items to be
slotted into m holes and n > m there is at least one
hole with more than 1 item.
 So if n > m, we know we've had a collision
 We can only avoid a collision when n < m.
Open Addressing
 Open addressing is a method for solving collision.
 In open Addressing, all elements are stored in the hash table itself. so,at any point,
the size of the table must be greater than or equal to the total number of keys.
 It can be performed in the following ways that are:
Linear Probing, Quadratic Probing and Separate hashing.
Linear Probing
 The hash table in this case is implemented using an array containing M nodes, each node
of the hash table has a field k used to contain the key of the node.
 When the hash table is initialized, all fields k are assigned to -1.
 When a node with the key k needs to be added into the hash table, the hash function f( k) =
k % M will specify the address i = f( k) (i.e., an index of an array) within the range [0, M -
1].
 If there is no conflict, then this node is added into the hash table at the address i.
 If a conflict takes place, then the hash function rehashes first time f 1 to consider the next
address (i.e., i + 1). If conflict occurs again, then the hash function rehashes second time f
2 to examine the next address (i.e., i + 2).
 This process repeats until the available address found then this node will be added at this
address.The rehash function at the time t (i.e., the collision number t = 1, 2, ...)
 When searching a node, the hash function f( k) will identify the address i (i.e., i = f( k))
falling between 0 and M - 1.
Let us consider an example with hash function as “key mod 7” and sequence is given as
50,700,76,85,92,73 and 101.
 Step1:
• Draw the Hash table
• Possible range of hash value is[0,6]
• So, draw an empty hash table consisting of 7
buckets as:
0
1
2
3
4
5
6
 Step 2:
• Insert the 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:
0
1
2
3
4
5
6
 Step 3:
• 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:
0
1
2
3
4
5
6
 Step 4:
• 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:
0
1
2
3
4
5
6
 Step 5:
• 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 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:
0
1
2
3
4
5
6
 Step 6:
• 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 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:
0
1
2
3
4
5
6
 Step 7:
• 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 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:
0
1
2
3
4
5
6
 Step 8:
• 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 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:
0
1
2
3
4
5
6
Quadratic Probing
 Quadratic is an open addressing scheme in programming for resolving hash collisions in
hash tables.
 Quadratic probing operates by taking the original hash index and adding successive values
of an arbitrary quadratic polynomial until an open slot is found.
 An example sequence using quadratic probing is
H+12,H+22,H+32,H+42,……,H+k2.
 It avoids the clustering problem that can occur with linear probing.
 Let h(k) be a hash function that maps an element k to an integer in [0,m-1],where m is the
size of the table. Let the ith probe position for a value k be given by the function as:
H(k, i)=(h(k)+c1i+c2i2) (mod m)
 If there is a conflict, then this node is added into the hash table at the address i.
 If a conflict takes place, then the hash function rehashes first time f1 to consider
the address f(k)+12.
 If a conflict takes place, then the hash function rehashes first time f1 to consider
the address f(k)+22.
 This process repeats until the available address found then this node will be added
at this address.
 An Example is shown as follows:
Example: insert the keys:76,40,48 ,5 and 20.
 Step 1:
• Draw the hash table
• For the given hash function, the possible range
of hash Values is[0,6].
• So, draw an empty hash table consisting of 7
buckets
0
1
2
3
4
5
6
 Step 2:
• Insert the given keys in the hash table one by one.
The first key to be inserted in the hash table=76.
• 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 inserted in bucket 7 of the hash
table as:
0
1
2
3
4
5
6
 Step 3:
• Next key to be inserted in the hash table=40.
• Bucket of the hash table to which key 40
maps=40 mod 7=5.
• So, key 40 will inserted in bucket 5 of the hash
table as:
0
1
2
3
4
5
6
 Step 4:
• Next key to be inserted in the hash
table=48.Bucket of the hash table to which
key 48 maps=48 mod 7=6.Since bucket 6 is
already occupied, so collision occurs
• To handle the collision ,quadratic probing
technique keeps probing until an empty
bucket is found.The first empty bucket is
bucket 0.So,key 48 will inserted in bucket 0 of
the hash table as:
0
1
2
3
4
5
6
 Step 5:
• Next key to be inserted in the hash table=5.Bucket
of the hash table to which key 5 maps=5 mod
7=5.Since bucket 5 is already occupied, so
collision occurs
• To handle the collision ,quadratic probing
technique keeps probing until an empty bucket is
found.The first empty bucket is bucket 2.So,key 5
will inserted in bucket 0 of the hash table as:
0
1
2
3
4
5
6
 Step 6:
• Next key to be inserted in the hash
table=20.Bucket of the hash table to which
key 20 maps=20 mod 7=6.Since bucket 5 is
already occupied, so collision occurs
• To handle the collision ,quadratic probing
technique keeps probing until an empty bucket
is found.The first empty bucket is bucket
3.So,key 20 will inserted in bucket 3 of the
hash table as:
0
1
2
3
4
5
6
Double Hashing
 Double hashing requires that the size of the hash table is a prime number.
 Double hashing uses the idea of applying a second hash function to key when collision occurs.
 The primary hash function determines the home address. if the home address is occupied, apply a
second hash function to get a number c(c I relative to prime to N).This c is added to home address
to produce an overflow addresses: if occupied, proceed by adding c to the overflow address, until
an empty slot is found.
 f(i)=i * g(k) where g is second hash function.
 Probe sequence is:
0th probe = h(k) mod TableSize
1th probe=(h(k) + g(k)) mod TableSize
2th probe=(h(k) + 2*g(k)) mod TableSize
3th probe=(h(k) + 3*g(k)) mod TableSize
………
ith probe=(h(k) + i *g(k)) mod TableSize
Example: Given hash values are: 76,93,40,47,10 and 55.
 h(k)=k mod 7 and g(k)=5-(k mod 5)
Separate Chaining
 A chain is simply a linked list of all the elements with the same hash key. A linked list is created at each
index in the hash table.
 A data items key is hashed to the index in hashing, and the item is inserted into the linked list at the index.
 Other items that hash to the same index are simply added to the linked list.
 Hash function: h(k) = k mod m.
 Example: Assume a table has 8 slots(m=8).Using the chaining, insert the following elements into the hash
table.36,18,72,43,6,10,5 and 15 are inserted in the order.
Hash key = key % table size
36 % 8 =4
18 % 8 =2
72 % 8 =0
43 % 8 =3
6 % 8 =6
10 % 8 =2
5 % 8 =5
15 % 8 =7
 Example:

More Related Content

Similar to HASHING.ppt.pptx

hashing in data strutures advanced in languae java
hashing in data strutures advanced in languae javahashing in data strutures advanced in languae java
hashing in data strutures advanced in languae java
ishasharma835109
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
rajshreemuthiah
 
8. Hash table
8. Hash table8. Hash table
8. Hash table
Mandeep Singh
 
HASHING IS NOT YASH IT IS HASH.pptx
HASHING IS NOT YASH IT IS HASH.pptxHASHING IS NOT YASH IT IS HASH.pptx
HASHING IS NOT YASH IT IS HASH.pptx
JITTAYASHWANTHREDDY
 
Hashing .pptx
Hashing .pptxHashing .pptx
Hashing .pptx
ParagAhir1
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec IISajid Marwat
 
L21_Hashing.pdf
L21_Hashing.pdfL21_Hashing.pdf
L21_Hashing.pdf
BlessingMapadza1
 
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
 
Hash function
Hash functionHash function
Hash function
MDPiasKhan
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
Tribhuvan University
 
Hashing
HashingHashing
Hashing In Data Structure Download PPT i
Hashing In Data Structure Download PPT iHashing In Data Structure Download PPT i
Hashing In Data Structure Download PPT i
cajiwol341
 
Hashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniquesHashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniques
ssuserec8a711
 
Data Structures- Hashing
Data Structures- Hashing Data Structures- Hashing
Data Structures- Hashing
hemalatha athinarayanan
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
Home
 
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
 
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptx
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptxPPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptx
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptx
CHANDUS31
 
Parallel Hashing Algorithms
Parallel Hashing AlgorithmsParallel Hashing Algorithms
Parallel Hashing Algorithms
Akash Panchal
 

Similar to HASHING.ppt.pptx (20)

Hashing
HashingHashing
Hashing
 
hashing in data strutures advanced in languae java
hashing in data strutures advanced in languae javahashing in data strutures advanced in languae java
hashing in data strutures advanced in languae java
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
 
8. Hash table
8. Hash table8. Hash table
8. Hash table
 
HASHING IS NOT YASH IT IS HASH.pptx
HASHING IS NOT YASH IT IS HASH.pptxHASHING IS NOT YASH IT IS HASH.pptx
HASHING IS NOT YASH IT IS HASH.pptx
 
Hashing .pptx
Hashing .pptxHashing .pptx
Hashing .pptx
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec II
 
Hashing
HashingHashing
Hashing
 
L21_Hashing.pdf
L21_Hashing.pdfL21_Hashing.pdf
L21_Hashing.pdf
 
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)
 
Hash function
Hash functionHash function
Hash function
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
Hashing
HashingHashing
Hashing
 
Hashing In Data Structure Download PPT i
Hashing In Data Structure Download PPT iHashing In Data Structure Download PPT i
Hashing In Data Structure Download PPT i
 
Hashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniquesHashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniques
 
Data Structures- Hashing
Data Structures- Hashing Data Structures- Hashing
Data Structures- Hashing
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
 
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
 
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptx
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptxPPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptx
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptx
 
Parallel Hashing Algorithms
Parallel Hashing AlgorithmsParallel Hashing Algorithms
Parallel Hashing Algorithms
 

Recently uploaded

Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 

Recently uploaded (20)

Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 

HASHING.ppt.pptx

  • 2. CONTENTS  Introduction  Advantages  Hash function  Collision Resolution techniques  Pigeon Hole Principle  Open Addressing  Linear Probing  Quadratic Probing  Double hashing  Separate Chaining
  • 3. Introduction  Hashing is an important data structure which is designed to use a special function called Hash function which is used to map a given value with a particular key for faster access of elements.  In Hashing, large keys are converted into small keys using hash functions.  The values are then stored in a data structure called hash table.  The idea of Hashing is to distribute entries (key/value pairs) uniformly across an array.  Each element is assigned a key(converted key).  By using that key you can access the element in O(1) time. Using the key, the algorithm(hash function) computes an index that suggests where a entry can be found or inserted.
  • 4.  Hashing is implemented into two steps: • An element is converted into an integer by using a hash function. This element can be used as an index to sore the original element, which falls into the hash table. • The element is stored in the hash table where it can be quicky retrieved using hashed key. • Hash=hashfunc(key). • Index=hash % array_size.  Advantages: • The main advantage of hash tables over other data structures is speed i.e O(1). • This advantage is more apparent when the number of entries is large that may be thousands or more.
  • 5. Hash function  A hash function is any function that can be used to map a data set of an arbitrary size to a data set of fixed size, which falls into the hash table.  The values returned by a hash function are called hash values, hash codes, hash sums, or simply hashes.  To achieve good hashing mechanism, it is important to have a good hash function with the following basic requirements such as: • Easy to compute. • Uniform distribution • Less collision
  • 6. Collision Resolution techniques  Collision: when two keys map to the same location in the hash table. collisions occur when two keys, k1 and k2, are not equal, but h(k1) = h(k2).  Two ways to resolve collisions:  Separate Chaining (open hashing)  Open Addressing (closed hashing ) • Linear probing. • Quadratic probing. • Double hashing.
  • 7. Pigeon Hole Principle  The pigeonhole principle states that if n items are put into m containers, with n>m ,then at least one container must contain more than one item.  Pigeons in holes, here there are n=10 pigeons and in m=9that is there are 9 holes. Since 10 is greater than 9,the pigeon hole principle says that at least one hole has more than one pigeon.  Pigeon Hole Principle says given n items to be slotted into m holes and n > m there is at least one hole with more than 1 item.  So if n > m, we know we've had a collision  We can only avoid a collision when n < m.
  • 8. Open Addressing  Open addressing is a method for solving collision.  In open Addressing, all elements are stored in the hash table itself. so,at any point, the size of the table must be greater than or equal to the total number of keys.  It can be performed in the following ways that are: Linear Probing, Quadratic Probing and Separate hashing.
  • 9. Linear Probing  The hash table in this case is implemented using an array containing M nodes, each node of the hash table has a field k used to contain the key of the node.  When the hash table is initialized, all fields k are assigned to -1.  When a node with the key k needs to be added into the hash table, the hash function f( k) = k % M will specify the address i = f( k) (i.e., an index of an array) within the range [0, M - 1].  If there is no conflict, then this node is added into the hash table at the address i.  If a conflict takes place, then the hash function rehashes first time f 1 to consider the next address (i.e., i + 1). If conflict occurs again, then the hash function rehashes second time f 2 to examine the next address (i.e., i + 2).  This process repeats until the available address found then this node will be added at this address.The rehash function at the time t (i.e., the collision number t = 1, 2, ...)  When searching a node, the hash function f( k) will identify the address i (i.e., i = f( k)) falling between 0 and M - 1.
  • 10. Let us consider an example with hash function as “key mod 7” and sequence is given as 50,700,76,85,92,73 and 101.  Step1: • Draw the Hash table • Possible range of hash value is[0,6] • So, draw an empty hash table consisting of 7 buckets as: 0 1 2 3 4 5 6  Step 2: • Insert the 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: 0 1 2 3 4 5 6
  • 11.  Step 3: • 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: 0 1 2 3 4 5 6  Step 4: • 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: 0 1 2 3 4 5 6
  • 12.  Step 5: • 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 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: 0 1 2 3 4 5 6  Step 6: • 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 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: 0 1 2 3 4 5 6
  • 13.  Step 7: • 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 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: 0 1 2 3 4 5 6  Step 8: • 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 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: 0 1 2 3 4 5 6
  • 14. Quadratic Probing  Quadratic is an open addressing scheme in programming for resolving hash collisions in hash tables.  Quadratic probing operates by taking the original hash index and adding successive values of an arbitrary quadratic polynomial until an open slot is found.  An example sequence using quadratic probing is H+12,H+22,H+32,H+42,……,H+k2.  It avoids the clustering problem that can occur with linear probing.  Let h(k) be a hash function that maps an element k to an integer in [0,m-1],where m is the size of the table. Let the ith probe position for a value k be given by the function as: H(k, i)=(h(k)+c1i+c2i2) (mod m)
  • 15.  If there is a conflict, then this node is added into the hash table at the address i.  If a conflict takes place, then the hash function rehashes first time f1 to consider the address f(k)+12.  If a conflict takes place, then the hash function rehashes first time f1 to consider the address f(k)+22.  This process repeats until the available address found then this node will be added at this address.  An Example is shown as follows:
  • 16. Example: insert the keys:76,40,48 ,5 and 20.  Step 1: • Draw the hash table • For the given hash function, the possible range of hash Values is[0,6]. • So, draw an empty hash table consisting of 7 buckets 0 1 2 3 4 5 6  Step 2: • Insert the given keys in the hash table one by one. The first key to be inserted in the hash table=76. • 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 inserted in bucket 7 of the hash table as: 0 1 2 3 4 5 6
  • 17.  Step 3: • Next key to be inserted in the hash table=40. • Bucket of the hash table to which key 40 maps=40 mod 7=5. • So, key 40 will inserted in bucket 5 of the hash table as: 0 1 2 3 4 5 6  Step 4: • Next key to be inserted in the hash table=48.Bucket of the hash table to which key 48 maps=48 mod 7=6.Since bucket 6 is already occupied, so collision occurs • To handle the collision ,quadratic probing technique keeps probing until an empty bucket is found.The first empty bucket is bucket 0.So,key 48 will inserted in bucket 0 of the hash table as: 0 1 2 3 4 5 6
  • 18.  Step 5: • Next key to be inserted in the hash table=5.Bucket of the hash table to which key 5 maps=5 mod 7=5.Since bucket 5 is already occupied, so collision occurs • To handle the collision ,quadratic probing technique keeps probing until an empty bucket is found.The first empty bucket is bucket 2.So,key 5 will inserted in bucket 0 of the hash table as: 0 1 2 3 4 5 6  Step 6: • Next key to be inserted in the hash table=20.Bucket of the hash table to which key 20 maps=20 mod 7=6.Since bucket 5 is already occupied, so collision occurs • To handle the collision ,quadratic probing technique keeps probing until an empty bucket is found.The first empty bucket is bucket 3.So,key 20 will inserted in bucket 3 of the hash table as: 0 1 2 3 4 5 6
  • 19. Double Hashing  Double hashing requires that the size of the hash table is a prime number.  Double hashing uses the idea of applying a second hash function to key when collision occurs.  The primary hash function determines the home address. if the home address is occupied, apply a second hash function to get a number c(c I relative to prime to N).This c is added to home address to produce an overflow addresses: if occupied, proceed by adding c to the overflow address, until an empty slot is found.  f(i)=i * g(k) where g is second hash function.  Probe sequence is: 0th probe = h(k) mod TableSize 1th probe=(h(k) + g(k)) mod TableSize 2th probe=(h(k) + 2*g(k)) mod TableSize 3th probe=(h(k) + 3*g(k)) mod TableSize ……… ith probe=(h(k) + i *g(k)) mod TableSize
  • 20. Example: Given hash values are: 76,93,40,47,10 and 55.  h(k)=k mod 7 and g(k)=5-(k mod 5)
  • 21. Separate Chaining  A chain is simply a linked list of all the elements with the same hash key. A linked list is created at each index in the hash table.  A data items key is hashed to the index in hashing, and the item is inserted into the linked list at the index.  Other items that hash to the same index are simply added to the linked list.  Hash function: h(k) = k mod m.  Example: Assume a table has 8 slots(m=8).Using the chaining, insert the following elements into the hash table.36,18,72,43,6,10,5 and 15 are inserted in the order. Hash key = key % table size 36 % 8 =4 18 % 8 =2 72 % 8 =0 43 % 8 =3 6 % 8 =6 10 % 8 =2 5 % 8 =5 15 % 8 =7