SlideShare a Scribd company logo
1 of 22
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 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.pptxJITTAYASHWANTHREDDY
 
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
 
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 icajiwol341
 
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 techniquesssuserec8a711
 
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 algorithmfarhankhan89766
 
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.pptxCHANDUS31
 
Parallel Hashing Algorithms
Parallel Hashing AlgorithmsParallel Hashing Algorithms
Parallel Hashing AlgorithmsAkash Panchal
 

Similar to HASHING.ppt.pptx (20)

Hashing
HashingHashing
Hashing
 
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
 
Hashing_UNIT2.pptx
Hashing_UNIT2.pptxHashing_UNIT2.pptx
Hashing_UNIT2.pptx
 

Recently uploaded

Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
buds n tech IT solutions
buds n  tech IT                solutionsbuds n  tech IT                solutions
buds n tech IT solutionsmonugehlot87
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 

Recently uploaded (20)

Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
buds n tech IT solutions
buds n  tech IT                solutionsbuds n  tech IT                solutions
buds n tech IT solutions
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 

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