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 
Ad

More Related Content

What's hot (20)

Hash table
Hash tableHash table
Hash table
Rajendran
 
Splay Tree
Splay TreeSplay Tree
Splay Tree
Dr Sandeep Kumar Poonia
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
Muhammad Hammad Waseem
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
Radhakrishnan Chinnusamy
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
rajshreemuthiah
 
Hash table
Hash tableHash table
Hash table
Vu Tran
 
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
Aamir Sohail
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
Home
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
rajshreemuthiah
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Dr Shashikant Athawale
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
Ashim Lamichhane
 
Linked List
Linked ListLinked List
Linked List
Ashim Lamichhane
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
Kandarp Tiwari
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
Dr Sandeep Kumar Poonia
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Heaps
HeapsHeaps
Heaps
Hafiz Atif Amin
 
Red black tree
Red black treeRed black tree
Red black tree
Rajendran
 
Searching and sorting
Searching  and sortingSearching  and sorting
Searching and sorting
PoojithaBollikonda
 

Similar to Hashing PPT (20)

hashing1.pptx Data Structures and Algorithms
hashing1.pptx Data Structures and Algorithmshashing1.pptx Data Structures and Algorithms
hashing1.pptx Data Structures and Algorithms
snehalkulkarni78
 
presentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptxpresentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptx
jainaaru59
 
Hashing
HashingHashing
Hashing
amoldkul
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
TutorialsDuniya.com
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
AgonySingh
 
hasing introduction.pptx
hasing introduction.pptxhasing introduction.pptx
hasing introduction.pptx
vvwaykule
 
Hashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdfHashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdf
JaithoonBibi
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
chidabdu
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
chidabdu
 
Lec4
Lec4Lec4
Lec4
Nikhil Chilwant
 
AI&DS_SEVANTHI_DATA STRUCTURES_HASHING.pptx
AI&DS_SEVANTHI_DATA STRUCTURES_HASHING.pptxAI&DS_SEVANTHI_DATA STRUCTURES_HASHING.pptx
AI&DS_SEVANTHI_DATA STRUCTURES_HASHING.pptx
S.A Engineering College
 
unit 3 Divide and Conquer Rule and Sorting.pptx
unit 3 Divide and Conquer Rule and Sorting.pptxunit 3 Divide and Conquer Rule and Sorting.pptx
unit 3 Divide and Conquer Rule and Sorting.pptx
JayashreeCSENMIT
 
13-hashing.ppt
13-hashing.ppt13-hashing.ppt
13-hashing.ppt
soniya555961
 
Presentation1
Presentation1Presentation1
Presentation1
Saurabh Mishra
 
Hash Tables in data Structure
Hash Tables in data StructureHash Tables in data Structure
Hash Tables in data Structure
Prof Ansari
 
Hashing
HashingHashing
Hashing
VARSHAKUMARI49
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
kratika64
 
BCS304 Module 5 slides DSA notes 3rd sem
BCS304 Module 5 slides DSA notes 3rd semBCS304 Module 5 slides DSA notes 3rd sem
BCS304 Module 5 slides DSA notes 3rd sem
ticonah393
 
Skiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sortingSkiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sorting
zukun
 
introduction to trees,graphs,hashing
introduction to trees,graphs,hashingintroduction to trees,graphs,hashing
introduction to trees,graphs,hashing
Akhil Prem
 
hashing1.pptx Data Structures and Algorithms
hashing1.pptx Data Structures and Algorithmshashing1.pptx Data Structures and Algorithms
hashing1.pptx Data Structures and Algorithms
snehalkulkarni78
 
presentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptxpresentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptx
jainaaru59
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
TutorialsDuniya.com
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
AgonySingh
 
hasing introduction.pptx
hasing introduction.pptxhasing introduction.pptx
hasing introduction.pptx
vvwaykule
 
Hashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdfHashing and File Structures in Data Structure.pdf
Hashing and File Structures in Data Structure.pdf
JaithoonBibi
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
chidabdu
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
chidabdu
 
AI&DS_SEVANTHI_DATA STRUCTURES_HASHING.pptx
AI&DS_SEVANTHI_DATA STRUCTURES_HASHING.pptxAI&DS_SEVANTHI_DATA STRUCTURES_HASHING.pptx
AI&DS_SEVANTHI_DATA STRUCTURES_HASHING.pptx
S.A Engineering College
 
unit 3 Divide and Conquer Rule and Sorting.pptx
unit 3 Divide and Conquer Rule and Sorting.pptxunit 3 Divide and Conquer Rule and Sorting.pptx
unit 3 Divide and Conquer Rule and Sorting.pptx
JayashreeCSENMIT
 
Hash Tables in data Structure
Hash Tables in data StructureHash Tables in data Structure
Hash Tables in data Structure
Prof Ansari
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
kratika64
 
BCS304 Module 5 slides DSA notes 3rd sem
BCS304 Module 5 slides DSA notes 3rd semBCS304 Module 5 slides DSA notes 3rd sem
BCS304 Module 5 slides DSA notes 3rd sem
ticonah393
 
Skiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sortingSkiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sorting
zukun
 
introduction to trees,graphs,hashing
introduction to trees,graphs,hashingintroduction to trees,graphs,hashing
introduction to trees,graphs,hashing
Akhil Prem
 
Ad

Recently uploaded (20)

Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)
Allon Mureinik
 
How can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptxHow can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptx
laravinson24
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Dele Amefo
 
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
University of Hawai‘i at Mānoa
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
ssuserb14185
 
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
Egor Kaleynik
 
Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]
saniaaftab72555
 
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)
Allon Mureinik
 
How can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptxHow can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptx
laravinson24
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Dele Amefo
 
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
University of Hawai‘i at Mānoa
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
ssuserb14185
 
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
Egor Kaleynik
 
Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]Get & Download Wondershare Filmora Crack Latest [2025]
Get & Download Wondershare Filmora Crack Latest [2025]
saniaaftab72555
 
Ad

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;