SlideShare a Scribd company logo
CSCE 3110
Data Structures & Algorithm
Analysis
Rada Mihalcea
http://www.cs.unt.edu/~rada/CSCE3110
Hashing
Reading: Chap.5, Weiss
Dictionaries stores elements so that they can be
located quickly using keys.
For eg
A Dictionary may hold bank accounts.
In which key will be account number.
And each account may stores many additional
information.
Dictionaries
How to Implement a Dictionary?
Different data structure to realize a key
Array , Linked list
Binary tree
Hash table
Red/Black tree
AVL Tree
B-Tree
Why Hashing??
The sequential search algorithm takes time
proportional to the data size, i.e, O(n).
Binary search improves on liner search reducing
the search time to O(log n).
With a BST, an O(log n) search efficiency can
be obtained; but the worst-case complexity is
O(n).
To guarantee the O(log n) search time, BST
height balancing is required ( i.e., AVL trees).
Why Hashing?? (Cntd.)
Suppose that we want to store 10,000 students records
(each with a 5-digit ID) in a given container.
· A linked list implementation would take O(n) time.
· A height balanced tree would give O(log n)
access time.
· Using an array of size 100,000 would give O(1)
access time but will lead to a lot of space wastage.
Why Hashing?? (Cntd.)
Is there some way that we could get O(1) access
without wasting a lot of space?
The answer is hashing.
Hashing
Another important and widely useful
technique for implementing dictionaries
Constant time per operation (on the average)
Like an array, come up with a function to map
the large range into one which we can
manage.
Basic Idea
Use hash function to map keys into positions
in a hash table
Ideally
If Student A has ID(Key) k and h is hash
function, then A’s Details is stored in position
h(k) of table
To search for A, compute h(k) to locate
position. If no element, dictionary does not
contain A.
Example
Let keys be ID of 100 students
And ID in form of like 345610.
Now, we decided to take A[100]
And, Hash function is , say , LAST TWO DIGIT
So, 103062 will go to location 62
And same if some one have 113062
Then again goes to the location 62
THIS EVENT IS CALLED COLLISION
Collision Resolution
Chaining
Linear Probe
Double hashing
Chaining
Hash Functions
A Good Hash function is one which distribute keys
evenly among the slots.
And It is said that Hash Function is more art than a
science. Becoz it need to analyze the data.
Key
Hash
Function Slot
Hash Function(cntd.)
Need of choose a good Hash function
Quick Compute.
Distributes keys in uniform manner throughout the
table.
How to deal with Hashing non integer Key???
1.Find some way of turning keys into integer.
eg if key is in character then convert it into integer using
ASCII
2.Then use standard Hash Function on the integer.
Hash Function (contd.)
Hash code map
Keys Integer
Compression map
Integer A[0….m-1]
The Mapping of keys to indices of a hash table is called a
hash function.
The Hash Function is ussually the composition of two
maps:
Collision Resolution (contd.)
Now, there is two more techniques to deal
with collision
Linear Probing
Double Hashing
Linear probe
Linearprobeinsert(k)
If(table is full)
{error}
probe =h(k)
while(table[probe] is occupied)
{probe = (probe + 1) % m //m is no. of slots
}
Table[probe]=k
Linear Probe(contd.)
If the current location is used, Try the next
table Location.
Used less memory than chaining as one does
not have to store all those link(i.e. address of
others).
Slower than chaining as one might have to
walk along the table for a long time.
Linear Probe (contd.)
Linear probe (contd.)
Deletion in Linear probe
Double Hashing
h1(k) - Position in the table where we first
check for the key
h2(k) – Determine offset when h1(k) is
already occupied
In Linear probing offset is always 1.
Double Hashing (contd.)
Doublehashing insert(k)
If (table is full)
{error
}
Probe=h1(k); offset=h2(k);
While (table[probe] is occupied)
{probe=(probe + offset)%m
}
table[probe]=k;
Double Hashing(contd.)
Double Hashing(contd.)
Double Hashing(contd.)
Double Hashing(contd.)
Double Hashing(contd.)
Double Hashing(contd.)
Double Hashing(contd.)
Double Hashing(contd.)
Thank
U 

More Related Content

PPTX
Hashing
PPT
Data Structure and Algorithms Hashing
PPT
Chapter 12 ds
PDF
CNS - Unit - 4 - Public Key Cryptosystem
PPTX
Huffman coding
PPTX
Data Structures : hashing (1)
PPTX
Data structure tries
Hashing
Data Structure and Algorithms Hashing
Chapter 12 ds
CNS - Unit - 4 - Public Key Cryptosystem
Huffman coding
Data Structures : hashing (1)
Data structure tries

What's hot (20)

PPTX
Hashing in datastructure
PPTX
Hashing Technique In Data Structures
PPTX
Hash table in data structure and algorithm
PPT
Hash table
PPTX
Hashing In Data Structure
PPTX
Disjoint sets union, find
PPTX
AVL Tree in Data Structure
PPTX
STACKS IN DATASTRUCTURE
PPT
Pattern matching
PPTX
Data structure - Graph
PPTX
Theory of automata and formal language
PPT
Data Structure and Algorithms Binary Search Tree
PPT
Linked lists
PPTX
Constraint satisfaction problems (csp)
PPTX
Collision in Hashing.pptx
PPT
PPT
b+ tree
PPTX
B and B+ tree
PPT
Lec 17 heap data structure
Hashing in datastructure
Hashing Technique In Data Structures
Hash table in data structure and algorithm
Hash table
Hashing In Data Structure
Disjoint sets union, find
AVL Tree in Data Structure
STACKS IN DATASTRUCTURE
Pattern matching
Data structure - Graph
Theory of automata and formal language
Data Structure and Algorithms Binary Search Tree
Linked lists
Constraint satisfaction problems (csp)
Collision in Hashing.pptx
b+ tree
B and B+ tree
Lec 17 heap data structure
Ad

Similar to Hashing PPT (20)

PPTX
hashing1.pptx Data Structures and Algorithms
PPTX
presentation on important DAG,TRIE,Hashing.pptx
PPT
Hashing
PDF
Algorithms notes tutorials duniya
PPTX
Presentation.pptx
PPTX
hasing introduction.pptx
PDF
Hashing and File Structures in Data Structure.pdf
PDF
Algorithm chapter 7
PDF
Sienna 9 hashing
PPTX
AI&DS_SEVANTHI_DATA STRUCTURES_HASHING.pptx
PPTX
unit 3 Divide and Conquer Rule and Sorting.pptx
PPT
13-hashing.ppt
PPTX
Presentation1
PDF
Hash Tables in data Structure
PPTX
Quadratic probing
PPT
PPTX
Hashing.pptx
PPTX
BCS304 Module 5 slides DSA notes 3rd sem
PDF
Skiena algorithm 2007 lecture06 sorting
hashing1.pptx Data Structures and Algorithms
presentation on important DAG,TRIE,Hashing.pptx
Hashing
Algorithms notes tutorials duniya
Presentation.pptx
hasing introduction.pptx
Hashing and File Structures in Data Structure.pdf
Algorithm chapter 7
Sienna 9 hashing
AI&DS_SEVANTHI_DATA STRUCTURES_HASHING.pptx
unit 3 Divide and Conquer Rule and Sorting.pptx
13-hashing.ppt
Presentation1
Hash Tables in data Structure
Quadratic probing
Hashing.pptx
BCS304 Module 5 slides DSA notes 3rd sem
Skiena algorithm 2007 lecture06 sorting
Ad

Recently uploaded (20)

PPTX
AIRLINE PRICE API | FLIGHT API COST |
PDF
System and Network Administration Chapter 2
PPTX
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
PDF
Perfecting Gamer’s Experiences with Performance Testing for Gaming Applicatio...
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Become an Agentblazer Champion Challenge Kickoff
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
PDF
How to Choose the Most Effective Social Media Agency in Bangalore.pdf
PPTX
Presentation of Computer CLASS 2 .pptx
PPTX
Introduction to Artificial Intelligence
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Online Work Permit System for Fast Permit Processing
PDF
Become an Agentblazer Champion Challenge
PDF
Jenkins: An open-source automation server powering CI/CD Automation
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
AIRLINE PRICE API | FLIGHT API COST |
System and Network Administration Chapter 2
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
Perfecting Gamer’s Experiences with Performance Testing for Gaming Applicatio...
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Become an Agentblazer Champion Challenge Kickoff
How to Migrate SBCGlobal Email to Yahoo Easily
2025 Textile ERP Trends: SAP, Odoo & Oracle
How Creative Agencies Leverage Project Management Software.pdf
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
How to Choose the Most Effective Social Media Agency in Bangalore.pdf
Presentation of Computer CLASS 2 .pptx
Introduction to Artificial Intelligence
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Which alternative to Crystal Reports is best for small or large businesses.pdf
Online Work Permit System for Fast Permit Processing
Become an Agentblazer Champion Challenge
Jenkins: An open-source automation server powering CI/CD Automation
ManageIQ - Sprint 268 Review - Slide Deck

Hashing PPT

  • 1. CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea http://www.cs.unt.edu/~rada/CSCE3110 Hashing Reading: Chap.5, Weiss
  • 2. Dictionaries stores elements so that they can be located quickly using keys. For eg A Dictionary may hold bank accounts. In which key will be account number. And each account may stores many additional information. Dictionaries
  • 3. How to Implement a Dictionary? Different data structure to realize a key Array , Linked list Binary tree Hash table Red/Black tree AVL Tree B-Tree
  • 4. Why Hashing?? The sequential search algorithm takes time proportional to the data size, i.e, O(n). Binary search improves on liner search reducing the search time to O(log n). With a BST, an O(log n) search efficiency can be obtained; but the worst-case complexity is O(n). To guarantee the O(log n) search time, BST height balancing is required ( i.e., AVL trees).
  • 5. Why Hashing?? (Cntd.) Suppose that we want to store 10,000 students records (each with a 5-digit ID) in a given container. · A linked list implementation would take O(n) time. · A height balanced tree would give O(log n) access time. · Using an array of size 100,000 would give O(1) access time but will lead to a lot of space wastage.
  • 6. Why Hashing?? (Cntd.) Is there some way that we could get O(1) access without wasting a lot of space? The answer is hashing.
  • 7. Hashing Another important and widely useful technique for implementing dictionaries Constant time per operation (on the average) Like an array, come up with a function to map the large range into one which we can manage.
  • 8. Basic Idea Use hash function to map keys into positions in a hash table Ideally If Student A has ID(Key) k and h is hash function, then A’s Details is stored in position h(k) of table To search for A, compute h(k) to locate position. If no element, dictionary does not contain A.
  • 9. Example Let keys be ID of 100 students And ID in form of like 345610. Now, we decided to take A[100] And, Hash function is , say , LAST TWO DIGIT So, 103062 will go to location 62 And same if some one have 113062 Then again goes to the location 62 THIS EVENT IS CALLED COLLISION
  • 12. Hash Functions A Good Hash function is one which distribute keys evenly among the slots. And It is said that Hash Function is more art than a science. Becoz it need to analyze the data. Key Hash Function Slot
  • 13. Hash Function(cntd.) Need of choose a good Hash function Quick Compute. Distributes keys in uniform manner throughout the table. How to deal with Hashing non integer Key??? 1.Find some way of turning keys into integer. eg if key is in character then convert it into integer using ASCII 2.Then use standard Hash Function on the integer.
  • 14. Hash Function (contd.) Hash code map Keys Integer Compression map Integer A[0….m-1] The Mapping of keys to indices of a hash table is called a hash function. The Hash Function is ussually the composition of two maps:
  • 15. Collision Resolution (contd.) Now, there is two more techniques to deal with collision Linear Probing Double Hashing
  • 16. Linear probe Linearprobeinsert(k) If(table is full) {error} probe =h(k) while(table[probe] is occupied) {probe = (probe + 1) % m //m is no. of slots } Table[probe]=k
  • 17. Linear Probe(contd.) If the current location is used, Try the next table Location. Used less memory than chaining as one does not have to store all those link(i.e. address of others). Slower than chaining as one might have to walk along the table for a long time.
  • 20. Double Hashing h1(k) - Position in the table where we first check for the key h2(k) – Determine offset when h1(k) is already occupied In Linear probing offset is always 1.
  • 21. Double Hashing (contd.) Doublehashing insert(k) If (table is full) {error } Probe=h1(k); offset=h2(k); While (table[probe] is occupied) {probe=(probe + offset)%m } table[probe]=k;