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

What's hot

SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
chauhankapil
 
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
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
Hossain Md Shakhawat
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
Dharita Chokshi
 
Linked list
Linked listLinked list
Linked list
akshat360
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
junnubabu
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
Lemia Algmri
 
Hash table
Hash tableHash table
Hash table
Vu Tran
 
Tree and graph
Tree and graphTree and graph
Tree and graph
Muhaiminul Islam
 
Hashing data
Hashing dataHashing data
Hashing data
umair khan
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
Abrish06
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
Elavarasi K
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
Home
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
Muhammad Amjad Rana
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Trupti Agrawal
 
Hashing
HashingHashing
Hashing
kurubameena1
 
Sorting
SortingSorting
stack presentation
stack presentationstack presentation

What's hot (20)

SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
 
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
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Linked list
Linked listLinked list
Linked list
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Hash table
Hash tableHash table
Hash table
 
Tree and graph
Tree and graphTree and graph
Tree and graph
 
Hashing data
Hashing dataHashing data
Hashing data
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Hashing
HashingHashing
Hashing
 
Sorting
SortingSorting
Sorting
 
stack presentation
stack presentationstack presentation
stack presentation
 

Similar to Hashing PPT

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 7chidabdu
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashingchidabdu
 
Lec4
Lec4Lec4
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
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
rajshreemuthiah
 
Hashing
HashingHashing
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
kratika64
 
Skiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sortingSkiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sortingzukun
 
introduction to trees,graphs,hashing
introduction to trees,graphs,hashingintroduction to trees,graphs,hashing
introduction to trees,graphs,hashing
Akhil Prem
 
hashing.pdf
hashing.pdfhashing.pdf
hashing.pdf
Yuvraj919347
 
Hashing
HashingHashing
Hashing
debolina13
 
Radix Sorting With No Extra Space
Radix Sorting With No Extra SpaceRadix Sorting With No Extra Space
Radix Sorting With No Extra Spacegueste5dc45
 

Similar to Hashing PPT (20)

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
 
Hashing
HashingHashing
Hashing
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
hasing introduction.pptx
hasing introduction.pptxhasing introduction.pptx
hasing introduction.pptx
 
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
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
 
Lec4
Lec4Lec4
Lec4
 
13-hashing.ppt
13-hashing.ppt13-hashing.ppt
13-hashing.ppt
 
Presentation1
Presentation1Presentation1
Presentation1
 
Hash Tables in data Structure
Hash Tables in data StructureHash Tables in data Structure
Hash Tables in data Structure
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
 
Hashing
HashingHashing
Hashing
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
Skiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sortingSkiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sorting
 
introduction to trees,graphs,hashing
introduction to trees,graphs,hashingintroduction to trees,graphs,hashing
introduction to trees,graphs,hashing
 
hashing.pdf
hashing.pdfhashing.pdf
hashing.pdf
 
Hashing
HashingHashing
Hashing
 
Radix Sorting With No Extra Space
Radix Sorting With No Extra SpaceRadix Sorting With No Extra Space
Radix Sorting With No Extra Space
 

Recently uploaded

Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 

Recently uploaded (20)

Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 

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;