SlideShare a Scribd company logo
1 of 16
Hash Function Examples 
Let h(k) = k % 15. Then, 
if k = 25 129 35 2501 47 36 
h(k) = 10 9 5 11 2 6 
Storing the keys in the array is straightforward: 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
_ _ 47 _ _ 35 36 _ _ 129 25 2501 _ _ _ 
Thus, delete and find can be done in O(1), and 
also insert, except…
Hash Function 
What happens when you try to insert: k = 65 ? 
k = 65 
h(k) = 5 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
_ _ 47 _ _ 35 36 _ _ 129 25 2501 _ _ _ 
65(?) 
This is called a collision.
Handling Collisions 
 Chaining (Hashing with Chaining) 
Open Addressing 
– Linear Probing
Handling Collisions 
Chaining
Separate Chaining 
Let each array element be the head of a chain. 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
      
47 65 36 129 25 2501 
 
35 
Where would you store: 29, 16, 14, 99, 127 ?
Separate Chaining 
Let each array element be the head of a chain: 
Where would you store: 29, 16, 14, 99, 127 ? 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
         
16 47 65 36 127 99 25 2501 14 
   
35 129 29 
New keys go at the front of the relevant chain.
Separate Chaining: Disadvantages 
• Parts of the array might never be used. 
• As chains get longer, search time increases 
to O(n) in the worst case. 
• Constructing new chain nodes is relatively 
expensive . 
• Is there a way to use the “unused” space in 
the array instead of using chains to make 
more space?
Handling Collisions 
Linear Probing
Linear Probing 
Let key k be stored in element h(k)=t of the array 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
47 35 36 129 25 2501 
65(?) 
What do you do in case of a collision? 
If the hash table is not full, attempt to store key in the 
next array element (in this case (t+1)%N, (t+2)%N, 
(t+3)%N …). 
until you find an empty slot.
Linear Probing 
Where do you store 65 ? [Here N is 15]. 
65%15=5 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
47 35 36 65 129 25 2501 
   
attempts 
Where would you store: 29?
Linear Probing 
If the hash table is not full, attempt to store key 
in array elements (t+1)%N, (t+2)%N, … 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
47 35 36 65 129 25 2501 29 
 
attempts 
Where would you store: 16?
Linear Probing 
If the hash table is not full, attempt to store key 
in array elements (t+1)%N, (t+2)%N, … 
[16%15=1] 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
16 47 35 36 65 129 25 2501 29 
 
Where would you store: 14? 
[14%15=14]
Linear Probing 
If the hash table is not full, attempt to store key 
in array elements (t+1)%N, (t+2)%N, … 
[14%15=14] 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
14 16 47 35 36 65 129 25 2501 29 
  
attempts 
Where would you store: 99?
Linear Probing 
If the hash table is not full, attempt to store key 
in array elements (t+1)%N, (t+2)%N, … 
[99%15=9] 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
14 16 47 35 36 65 129 25 2501 99 29 
    
attempts 
Where would you store: 127 ?
Linear Probing 
If the hash table is not full, attempt to store key 
in array elements (t+1)%N, (t+2)%N, … 
[127%15=7] 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
16 47 35 36 65 127 129 25 2501 29 99 14 
  
attempts
Linear Probing 
• Eliminates need for separate data structures 
(chains), and the cost of constructing nodes. 
• Leads to problem of clustering. Elements tend 
to cluster in dense intervals in the array. 
     
• Search efficiency problem remains.

More Related Content

What's hot

Cryptographic Hashing Functions
Cryptographic Hashing FunctionsCryptographic Hashing Functions
Cryptographic Hashing Functions
Yusuf Uzun
 

What's hot (20)

Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Extensible hashing
Extensible hashingExtensible hashing
Extensible hashing
 
Compiler Design Unit 4
Compiler Design Unit 4Compiler Design Unit 4
Compiler Design Unit 4
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
 
Cryptographic Hashing Functions
Cryptographic Hashing FunctionsCryptographic Hashing Functions
Cryptographic Hashing Functions
 
Hash table
Hash tableHash table
Hash table
 
Hashing
HashingHashing
Hashing
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithm
 
Hash table
Hash tableHash table
Hash table
 
SHA- Secure hashing algorithm
SHA- Secure hashing algorithmSHA- Secure hashing algorithm
SHA- Secure hashing algorithm
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Code generation
Code generationCode generation
Code generation
 
Data Structures- Hashing
Data Structures- Hashing Data Structures- Hashing
Data Structures- Hashing
 
Hashing and Hash Tables
Hashing and Hash TablesHashing and Hash Tables
Hashing and Hash Tables
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
 
Floating point arithmetic
Floating point arithmeticFloating point arithmetic
Floating point arithmetic
 
Operating System- Multilevel Paging, Inverted Page Table
Operating System- Multilevel Paging, Inverted Page TableOperating System- Multilevel Paging, Inverted Page Table
Operating System- Multilevel Paging, Inverted Page Table
 
Producer consumer
Producer consumerProducer consumer
Producer consumer
 

Viewers also liked

Concept of hashing
Concept of hashingConcept of hashing
Concept of hashing
Rafi Dar
 

Viewers also liked (20)

Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Hashing
HashingHashing
Hashing
 
Hashing
HashingHashing
Hashing
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashing
 
Hash tables
Hash tablesHash tables
Hash tables
 
Ch17 Hashing
Ch17 HashingCh17 Hashing
Ch17 Hashing
 
11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil
 
Hashing
HashingHashing
Hashing
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 
Secure hashing algorithm
Secure hashing algorithmSecure hashing algorithm
Secure hashing algorithm
 
Hash Function
Hash FunctionHash Function
Hash Function
 
kerberos
kerberoskerberos
kerberos
 
Kerberos
KerberosKerberos
Kerberos
 
Hashing Part One
Hashing Part OneHashing Part One
Hashing Part One
 
Smart antennas
Smart antennasSmart antennas
Smart antennas
 
Probabilistic Data Structures and Approximate Solutions Oleksandr Pryymak
Probabilistic Data Structures and Approximate Solutions Oleksandr PryymakProbabilistic Data Structures and Approximate Solutions Oleksandr Pryymak
Probabilistic Data Structures and Approximate Solutions Oleksandr Pryymak
 
Computer
ComputerComputer
Computer
 
Security in the Real World - JavaOne 2013
Security in the Real World - JavaOne 2013Security in the Real World - JavaOne 2013
Security in the Real World - JavaOne 2013
 
Merging
MergingMerging
Merging
 
Intro to Hash tables
Intro to Hash tablesIntro to Hash tables
Intro to Hash tables
 

Similar to Hashing Techniques in Data Structures Part2

Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec II
Sajid Marwat
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programming
Damian T. Gordon
 

Similar to Hashing Techniques in Data Structures Part2 (20)

Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec II
 
Collision in Hashing.pptx
Collision in Hashing.pptxCollision in Hashing.pptx
Collision in Hashing.pptx
 
Hashing .pptx
Hashing .pptxHashing .pptx
Hashing .pptx
 
Trig packet1 000
Trig packet1 000Trig packet1 000
Trig packet1 000
 
Trig packet1 000
Trig packet1 000Trig packet1 000
Trig packet1 000
 
13-hashing.ppt
13-hashing.ppt13-hashing.ppt
13-hashing.ppt
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programming
 
LECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdfLECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdf
 
Randamization.pdf
Randamization.pdfRandamization.pdf
Randamization.pdf
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Matlab on basic mathematics
Matlab on basic mathematicsMatlab on basic mathematics
Matlab on basic mathematics
 
Hashing Algorithm
Hashing AlgorithmHashing Algorithm
Hashing Algorithm
 
Laplace transforms
Laplace transformsLaplace transforms
Laplace transforms
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
Introduction to Logarithm
Introduction to LogarithmIntroduction to Logarithm
Introduction to Logarithm
 
Ratio and Proportion, Indices and Logarithm Part 4
Ratio and Proportion, Indices and Logarithm Part 4Ratio and Proportion, Indices and Logarithm Part 4
Ratio and Proportion, Indices and Logarithm Part 4
 
Study on a class of recursive functions
Study on a class of recursive functionsStudy on a class of recursive functions
Study on a class of recursive functions
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 

Hashing Techniques in Data Structures Part2

  • 1. Hash Function Examples Let h(k) = k % 15. Then, if k = 25 129 35 2501 47 36 h(k) = 10 9 5 11 2 6 Storing the keys in the array is straightforward: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 _ _ 47 _ _ 35 36 _ _ 129 25 2501 _ _ _ Thus, delete and find can be done in O(1), and also insert, except…
  • 2. Hash Function What happens when you try to insert: k = 65 ? k = 65 h(k) = 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 _ _ 47 _ _ 35 36 _ _ 129 25 2501 _ _ _ 65(?) This is called a collision.
  • 3. Handling Collisions  Chaining (Hashing with Chaining) Open Addressing – Linear Probing
  • 5. Separate Chaining Let each array element be the head of a chain. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14       47 65 36 129 25 2501  35 Where would you store: 29, 16, 14, 99, 127 ?
  • 6. Separate Chaining Let each array element be the head of a chain: Where would you store: 29, 16, 14, 99, 127 ? 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14          16 47 65 36 127 99 25 2501 14    35 129 29 New keys go at the front of the relevant chain.
  • 7. Separate Chaining: Disadvantages • Parts of the array might never be used. • As chains get longer, search time increases to O(n) in the worst case. • Constructing new chain nodes is relatively expensive . • Is there a way to use the “unused” space in the array instead of using chains to make more space?
  • 9. Linear Probing Let key k be stored in element h(k)=t of the array 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 47 35 36 129 25 2501 65(?) What do you do in case of a collision? If the hash table is not full, attempt to store key in the next array element (in this case (t+1)%N, (t+2)%N, (t+3)%N …). until you find an empty slot.
  • 10. Linear Probing Where do you store 65 ? [Here N is 15]. 65%15=5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 47 35 36 65 129 25 2501    attempts Where would you store: 29?
  • 11. Linear Probing If the hash table is not full, attempt to store key in array elements (t+1)%N, (t+2)%N, … 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 47 35 36 65 129 25 2501 29  attempts Where would you store: 16?
  • 12. Linear Probing If the hash table is not full, attempt to store key in array elements (t+1)%N, (t+2)%N, … [16%15=1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 47 35 36 65 129 25 2501 29  Where would you store: 14? [14%15=14]
  • 13. Linear Probing If the hash table is not full, attempt to store key in array elements (t+1)%N, (t+2)%N, … [14%15=14] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 14 16 47 35 36 65 129 25 2501 29   attempts Where would you store: 99?
  • 14. Linear Probing If the hash table is not full, attempt to store key in array elements (t+1)%N, (t+2)%N, … [99%15=9] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 14 16 47 35 36 65 129 25 2501 99 29     attempts Where would you store: 127 ?
  • 15. Linear Probing If the hash table is not full, attempt to store key in array elements (t+1)%N, (t+2)%N, … [127%15=7] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 47 35 36 65 127 129 25 2501 29 99 14   attempts
  • 16. Linear Probing • Eliminates need for separate data structures (chains), and the cost of constructing nodes. • Leads to problem of clustering. Elements tend to cluster in dense intervals in the array.      • Search efficiency problem remains.