SlideShare a Scribd company logo
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

PPT
Hashing
VARSHAKUMARI49
 
PPT
Data Structure and Algorithms Hashing
ManishPrajapati78
 
PPT
Hash table
Rajendran
 
PPT
Hashing PPT
Saurabh Kumar
 
PPTX
Collision in Hashing.pptx
NBACriteria2SICET
 
PPTX
Hashing In Data Structure
Meghaj Mallick
 
PPT
Chapter 12 ds
Hanif Durad
 
Data Structure and Algorithms Hashing
ManishPrajapati78
 
Hash table
Rajendran
 
Hashing PPT
Saurabh Kumar
 
Collision in Hashing.pptx
NBACriteria2SICET
 
Hashing In Data Structure
Meghaj Mallick
 
Chapter 12 ds
Hanif Durad
 

What's hot (20)

PPTX
Hashing
Amar Jukuntla
 
PPTX
Hashing Technique In Data Structures
SHAKOOR AB
 
PPT
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
PPT
Queue data structure
anooppjoseph
 
PPTX
Dfs presentation
Alizay Khan
 
PPTX
AVL Tree in Data Structure
Vrushali Dhanokar
 
PPTX
Queue in Data Structure
Janki Shah
 
PPSX
Stack
Seema Sharma
 
PPT
Bubble sort
Manek Ar
 
PPT
Graphs In Data Structure
Anuj Modi
 
PPTX
Stack data structure
Tech_MX
 
PPTX
Binary Search Tree in Data Structure
Dharita Chokshi
 
PPT
Extensible hashing
rajshreemuthiah
 
PPTX
Hashing
kurubameena1
 
PPTX
Sparse matrix and its representation data structure
Vardhil Patel
 
PPTX
Data structure - Graph
Madhu Bala
 
PPT
Heaps
Hafiz Atif Amin
 
PPTX
Data Structures : hashing (1)
Home
 
PPT
17. Trees and Graphs
Intro C# Book
 
Hashing
Amar Jukuntla
 
Hashing Technique In Data Structures
SHAKOOR AB
 
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Queue data structure
anooppjoseph
 
Dfs presentation
Alizay Khan
 
AVL Tree in Data Structure
Vrushali Dhanokar
 
Queue in Data Structure
Janki Shah
 
Bubble sort
Manek Ar
 
Graphs In Data Structure
Anuj Modi
 
Stack data structure
Tech_MX
 
Binary Search Tree in Data Structure
Dharita Chokshi
 
Extensible hashing
rajshreemuthiah
 
Hashing
kurubameena1
 
Sparse matrix and its representation data structure
Vardhil Patel
 
Data structure - Graph
Madhu Bala
 
Data Structures : hashing (1)
Home
 
17. Trees and Graphs
Intro C# Book
 
Ad

Similar to Hashing in datastructure (20)

PDF
hashtableeeeeeeeeeeeeeeeeeeeeeeeeeee.pdf
timoemin50
 
PPT
Hashing
amoldkul
 
PDF
LECT 10, 11-DSALGO(Hashing).pdf
MuhammadUmerIhtisham
 
PPTX
HASHING IS NOT YASH IT IS HASH.pptx
JITTAYASHWANTHREDDY
 
PDF
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Kuntal Bhowmick
 
PPT
Hashing in Data Structure and analysis of Algorithms
KavitaSingh962656
 
PPTX
Lecture14_15_Hashing.pptx
SLekshmiNair
 
PPTX
Hashing techniques, Hashing function,Collision detection techniques
ssuserec8a711
 
PDF
Hashing components and its laws 2 types
abhinavkumar77723
 
PPTX
Open addressiing &amp;rehashing,extendiblevhashing
SangeethaSasi1
 
PPT
Advance algorithm hashing lec II
Sajid Marwat
 
PPTX
hashing in data structures and its applications
manjeshbngowda
 
PPTX
8. Hash table
Mandeep Singh
 
PPTX
Data Structures-Topic-Hashing, Collision
sailaja156145
 
PPTX
Hashing.pptx
kratika64
 
PPTX
Hashing .pptx
ParagAhir1
 
PPTX
Lec12-Hash-Tables-27122022-125641pm.pptx
IqraHanif27
 
PDF
Hashing and File Structures in Data Structure.pdf
JaithoonBibi
 
PPT
11_hashtable-1.ppt. Data structure algorithm
farhankhan89766
 
PPTX
HASHING.ppt.pptx
MohammedAbdulNaseer5
 
hashtableeeeeeeeeeeeeeeeeeeeeeeeeeee.pdf
timoemin50
 
Hashing
amoldkul
 
LECT 10, 11-DSALGO(Hashing).pdf
MuhammadUmerIhtisham
 
HASHING IS NOT YASH IT IS HASH.pptx
JITTAYASHWANTHREDDY
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Kuntal Bhowmick
 
Hashing in Data Structure and analysis of Algorithms
KavitaSingh962656
 
Lecture14_15_Hashing.pptx
SLekshmiNair
 
Hashing techniques, Hashing function,Collision detection techniques
ssuserec8a711
 
Hashing components and its laws 2 types
abhinavkumar77723
 
Open addressiing &amp;rehashing,extendiblevhashing
SangeethaSasi1
 
Advance algorithm hashing lec II
Sajid Marwat
 
hashing in data structures and its applications
manjeshbngowda
 
8. Hash table
Mandeep Singh
 
Data Structures-Topic-Hashing, Collision
sailaja156145
 
Hashing.pptx
kratika64
 
Hashing .pptx
ParagAhir1
 
Lec12-Hash-Tables-27122022-125641pm.pptx
IqraHanif27
 
Hashing and File Structures in Data Structure.pdf
JaithoonBibi
 
11_hashtable-1.ppt. Data structure algorithm
farhankhan89766
 
HASHING.ppt.pptx
MohammedAbdulNaseer5
 
Ad

More from rajshreemuthiah (20)

PPTX
oracle
rajshreemuthiah
 
PPTX
quality
rajshreemuthiah
 
PPTX
bigdata
rajshreemuthiah
 
PPTX
polymorphism
rajshreemuthiah
 
PPTX
solutions and understanding text analytics
rajshreemuthiah
 
PPTX
interface
rajshreemuthiah
 
PPTX
Testing &ampdebugging
rajshreemuthiah
 
PPTX
concurrency control
rajshreemuthiah
 
PPTX
Education
rajshreemuthiah
 
PPTX
Formal verification
rajshreemuthiah
 
PPTX
Transaction management
rajshreemuthiah
 
PPTX
Multi thread
rajshreemuthiah
 
PPTX
System testing
rajshreemuthiah
 
PPTX
software maintenance
rajshreemuthiah
 
PPTX
exception handling
rajshreemuthiah
 
PPTX
e governance
rajshreemuthiah
 
PPTX
recovery management
rajshreemuthiah
 
PPTX
Implementing polymorphism
rajshreemuthiah
 
PPSX
Buffer managements
rajshreemuthiah
 
PPTX
os linux
rajshreemuthiah
 
polymorphism
rajshreemuthiah
 
solutions and understanding text analytics
rajshreemuthiah
 
interface
rajshreemuthiah
 
Testing &ampdebugging
rajshreemuthiah
 
concurrency control
rajshreemuthiah
 
Education
rajshreemuthiah
 
Formal verification
rajshreemuthiah
 
Transaction management
rajshreemuthiah
 
Multi thread
rajshreemuthiah
 
System testing
rajshreemuthiah
 
software maintenance
rajshreemuthiah
 
exception handling
rajshreemuthiah
 
e governance
rajshreemuthiah
 
recovery management
rajshreemuthiah
 
Implementing polymorphism
rajshreemuthiah
 
Buffer managements
rajshreemuthiah
 
os linux
rajshreemuthiah
 

Recently uploaded (20)

PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
AVTRON Technologies LLC
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
Doc9.....................................
SofiaCollazos
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Captain IT
 
PDF
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
 
PDF
DevOps & Developer Experience Summer BBQ
AUGNYC
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
agentic-ai-and-the-future-of-autonomous-systems.pdf
siddharthnetsavvies
 
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Test Bank, Solutions for Java How to Program, An Objects-Natural Approach, 12...
famaw19526
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
AVTRON Technologies LLC
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Doc9.....................................
SofiaCollazos
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Captain IT
 
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
 
DevOps & Developer Experience Summer BBQ
AUGNYC
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
agentic-ai-and-the-future-of-autonomous-systems.pdf
siddharthnetsavvies
 
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Test Bank, Solutions for Java How to Program, An Objects-Natural Approach, 12...
famaw19526
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 

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.