SlideShare a Scribd company logo
1 of 14
R. Pavithra
I-Msc(it)
 Another important and widely useful technique for
implementing dictionaries
 Constant time per operation (on the average)
 Worst case time proportional to the size of the set for each
operation (just like array and chain implementation)
 Use hash function to map keys into positions in a hash
table Ideally
 If element e has key k and h is hash function, then e is
stored in position h(k) of table
 To search for e, compute h(k) to locate position. If no
element, dictionary does not contain e.
Dictionary Student Records
 Keys are ID numbers (951000 - 952000), no more
than 100 students
 Hash function: h(k) = k-951000 maps ID into
distinct table positions 0-1000
 array table[1001]
...
0 1 2 3 1000
hash table
buckets
 If key range too large, use hash table with fewer
buckets and a hash function which maps multiple keys
to same bucket:
h(k1) =  = h(k2): k1 and k2 have collision at slot 
 Popular hash functions: hashing by division
h(k) = k%D, where D number of buckets in hash table
 Example: hash table with 11 buckets
h(k) = k%11
80  3 (80%11= 3), 40  7, 65  10
58  3 collision!
 Two classes:
 (1) Open hashing, a.k.a. separate
chaining
 (2) Closed hashing, a.k.a. open
addressing
 Difference has to do with whether
collisions are stored outside the table
(open hashing) or whether collisions
result in storing one of the records at
another slot in the table (closed hashing)
 Associated with closed hashing is a rehash strategy:
“If we try to place x in bucket h(x) and find it occupied,
find alternative location h1(x), h2(x), etc. Try each in order,
if none empty table is full,”
 h(x) is called home bucket
 Simplest rehash strategy is called linear hashing
hi(x) = (h(x) + i) % D
 In general, our collision resolution strategy is to generate
a sequence of hash table slots (probe sequence) that can
hold the record; test each slot until find empty one
(probing)
0
2
3
4
5
6
7
1
b
a
c
Where do we insert d? 3 already filled
Probe sequence using linear hashing:
h1(d) = (h(d)+1)%8 = 4%8 = 4
h2(d) = (h(d)+2)%8 = 5%8 = 5*
h3(d) = (h(d)+3)%8 = 6%8 = 6
etc.7, 0, 1, 2
Wraps around the beginning of the
table! d
 Test for membership: findItem
 Examine h(k), h1(k), h2(k), …, until we find k or an
empty bucket or home bucket
 If no deletions possible, strategy works.
 If we reach empty bucket, cannot be sure that k is not
somewhere else and empty bucket was occupied when
k was inserted.
 Need special placeholder deleted, to distinguish bucket
that was never used from one that once held a value.
 May need to reorganize table after many deletions.
 Consider: h(x) = x%16
 poor distribution, not very random
 depends solely on least significant four bits of key
 Better, mid-square method
 if keys are integers in range 0,1,…,K , pick integer C such
that DC2 about equal to K2, then
h(x) = x2/C % D
extracts middle r bits of x2, where 2
r
=D (a base-D digit)
 better, because most or all of bits of key contribute to
result
 Folding Method:
int h(String x, int D) {
int i, sum;
for (sum=0, i=0; i<x.length(); i++)
sum+= (int)x.charAt(i);
return (sum%D);
}
 sums the ASCII values of the letters in the string
 ASCII value for “A” =65; sum will be in range 650-900 for 10
upper-case letters; good when D around 100, for example
 order of chars in string has no effect
 Each bucket in the hash table is the head of
a linked list.
 All elements that hash to a particular bucket
are placed on that bucket’s linked list.
 Records within a bucket can be ordered in
several ways by order of insertion, by key
value order, or by frequency of access order.
 Worst case performance is O(n) for both
 Number of operations for hashing
 23 6 8 10 23 5 12 4 9 19
 D=9
 h(x) = x % D
 Draw the 11 entry hash table for hashing the
keys 12, 44, 13, 88, 23, 94, 11, 39, 20 using the
function (2i+5) mod 11, closed hashing,
linear probing
 Pseudo-code for listing all identifiers in a
hash table in lexicographic order, using
open hashing, the hash function h(x) = first
character of x. What is the running time.
T
H
A
N
K
Y
O
u

More Related Content

What's hot

What's hot (20)

Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Hashing
HashingHashing
Hashing
 
Hashing
HashingHashing
Hashing
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Hash tables
Hash tablesHash tables
Hash tables
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Hash table
Hash tableHash table
Hash table
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
Hashing
HashingHashing
Hashing
 
Hashing
HashingHashing
Hashing
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
CS8391 Data Structures Part B Questions Anna University
CS8391 Data Structures Part B Questions Anna UniversityCS8391 Data Structures Part B Questions Anna University
CS8391 Data Structures Part B Questions Anna University
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
AD3251-Data Structures Design-Notes-Searching-Hashing.pdf
AD3251-Data Structures  Design-Notes-Searching-Hashing.pdfAD3251-Data Structures  Design-Notes-Searching-Hashing.pdf
AD3251-Data Structures Design-Notes-Searching-Hashing.pdf
 

Similar to Hashing in datastructure

Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
chidabdu
 
Open addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashingOpen addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashing
Haripritha
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec II
Sajid Marwat
 

Similar to Hashing in datastructure (20)

Hashing
HashingHashing
Hashing
 
Hashing
HashingHashing
Hashing
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
 
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
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
 
03.01 hash tables
03.01 hash tables03.01 hash tables
03.01 hash tables
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
13-hashing.ppt
13-hashing.ppt13-hashing.ppt
13-hashing.ppt
 
Open addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashingOpen addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashing
 
Hash Tables in data Structure
Hash Tables in data StructureHash Tables in data Structure
Hash Tables in data Structure
 
Lec5
Lec5Lec5
Lec5
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
 
Presentation1
Presentation1Presentation1
Presentation1
 
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
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
 
Lec4
Lec4Lec4
Lec4
 
Hashing using a different methods of technic
Hashing using a different methods of technicHashing using a different methods of technic
Hashing using a different methods of technic
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec II
 

More from rajshreemuthiah (20)

oracle
oracleoracle
oracle
 
quality
qualityquality
quality
 
bigdata
bigdatabigdata
bigdata
 
polymorphism
polymorphismpolymorphism
polymorphism
 
solutions and understanding text analytics
solutions and understanding text analyticssolutions and understanding text analytics
solutions and understanding text analytics
 
interface
interfaceinterface
interface
 
Testing &ampdebugging
Testing &ampdebuggingTesting &ampdebugging
Testing &ampdebugging
 
concurrency control
concurrency controlconcurrency control
concurrency control
 
Education
EducationEducation
Education
 
Formal verification
Formal verificationFormal verification
Formal verification
 
Transaction management
Transaction management Transaction management
Transaction management
 
Multi thread
Multi threadMulti thread
Multi thread
 
System testing
System testingSystem testing
System testing
 
software maintenance
software maintenancesoftware maintenance
software maintenance
 
exception handling
exception handlingexception handling
exception handling
 
e governance
e governancee governance
e governance
 
recovery management
recovery managementrecovery management
recovery management
 
Implementing polymorphism
Implementing polymorphismImplementing polymorphism
Implementing polymorphism
 
Buffer managements
Buffer managementsBuffer managements
Buffer managements
 
os linux
os linuxos linux
os linux
 

Recently uploaded

Recently uploaded (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.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
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

Hashing in datastructure

  • 2.  Another important and widely useful technique for implementing dictionaries  Constant time per operation (on the average)  Worst case time proportional to the size of the set for each operation (just like array and chain implementation)  Use hash function to map keys into positions in a hash table Ideally  If element e has key k and h is hash function, then e is stored in position h(k) of table  To search for e, compute h(k) to locate position. If no element, dictionary does not contain e.
  • 3. Dictionary Student Records  Keys are ID numbers (951000 - 952000), no more than 100 students  Hash function: h(k) = k-951000 maps ID into distinct table positions 0-1000  array table[1001] ... 0 1 2 3 1000 hash table buckets
  • 4.  If key range too large, use hash table with fewer buckets and a hash function which maps multiple keys to same bucket: h(k1) =  = h(k2): k1 and k2 have collision at slot   Popular hash functions: hashing by division h(k) = k%D, where D number of buckets in hash table  Example: hash table with 11 buckets h(k) = k%11 80  3 (80%11= 3), 40  7, 65  10 58  3 collision!
  • 5.  Two classes:  (1) Open hashing, a.k.a. separate chaining  (2) Closed hashing, a.k.a. open addressing  Difference has to do with whether collisions are stored outside the table (open hashing) or whether collisions result in storing one of the records at another slot in the table (closed hashing)
  • 6.  Associated with closed hashing is a rehash strategy: “If we try to place x in bucket h(x) and find it occupied, find alternative location h1(x), h2(x), etc. Try each in order, if none empty table is full,”  h(x) is called home bucket  Simplest rehash strategy is called linear hashing hi(x) = (h(x) + i) % D  In general, our collision resolution strategy is to generate a sequence of hash table slots (probe sequence) that can hold the record; test each slot until find empty one (probing)
  • 7. 0 2 3 4 5 6 7 1 b a c Where do we insert d? 3 already filled Probe sequence using linear hashing: h1(d) = (h(d)+1)%8 = 4%8 = 4 h2(d) = (h(d)+2)%8 = 5%8 = 5* h3(d) = (h(d)+3)%8 = 6%8 = 6 etc.7, 0, 1, 2 Wraps around the beginning of the table! d
  • 8.  Test for membership: findItem  Examine h(k), h1(k), h2(k), …, until we find k or an empty bucket or home bucket  If no deletions possible, strategy works.  If we reach empty bucket, cannot be sure that k is not somewhere else and empty bucket was occupied when k was inserted.  Need special placeholder deleted, to distinguish bucket that was never used from one that once held a value.  May need to reorganize table after many deletions.
  • 9.  Consider: h(x) = x%16  poor distribution, not very random  depends solely on least significant four bits of key  Better, mid-square method  if keys are integers in range 0,1,…,K , pick integer C such that DC2 about equal to K2, then h(x) = x2/C % D extracts middle r bits of x2, where 2 r =D (a base-D digit)  better, because most or all of bits of key contribute to result
  • 10.  Folding Method: int h(String x, int D) { int i, sum; for (sum=0, i=0; i<x.length(); i++) sum+= (int)x.charAt(i); return (sum%D); }  sums the ASCII values of the letters in the string  ASCII value for “A” =65; sum will be in range 650-900 for 10 upper-case letters; good when D around 100, for example  order of chars in string has no effect
  • 11.  Each bucket in the hash table is the head of a linked list.  All elements that hash to a particular bucket are placed on that bucket’s linked list.  Records within a bucket can be ordered in several ways by order of insertion, by key value order, or by frequency of access order.
  • 12.  Worst case performance is O(n) for both  Number of operations for hashing  23 6 8 10 23 5 12 4 9 19  D=9  h(x) = x % D
  • 13.  Draw the 11 entry hash table for hashing the keys 12, 44, 13, 88, 23, 94, 11, 39, 20 using the function (2i+5) mod 11, closed hashing, linear probing  Pseudo-code for listing all identifiers in a hash table in lexicographic order, using open hashing, the hash function h(x) = first character of x. What is the running time.