SlideShare a Scribd company logo
1 of 29
Hashing
Hashing
When implementing a hash table using arrays, the nodes/elements are not stored
consecutively, instead the location of an element/node to be
inserted/deleted/searched is computed using the key and a hash function.
Hash Table
• Hash table is a data structure which is designed specifically
with the objective of providing efficient insertion and
searching operations (deletion is not a primary objective).
• Hash Table achieves constant time performance O(1) time
• To achieve constant time performance objective, the
implementation must be based in some way on an array
rather than a linked list.
Hash Table
• Hash table is a container which will be used to hold some number
of items of a given set K. We call the elements of the set K keys and
K is called the key space
• In the general case, we expect the size of the set of keys, denoted
as |K|, to be relatively large in comparison with the number of
items stored in the table of size M
• The general approach is to store the keys in an array. The position
(also called location or index) i of a key k in the array is given by a
function hash(k) or f(k), called a hash function
• Hashing – Hash function determines the position of a given key
directly from that key (i.e., i = f(k) is called the hash code of k)
Example :For a Student Record USN is the key
Example USN= 1234
Value = Hash(1234) or value =f(1234) where value is the hash value of
the Student Record with key(USN) 1234.
Hashing : Hash Function &
Hash Collision
• Hash Function f
f: K → {0, 1, ..., M-1}.
• Hash function f maps (or transforms) the set of key values(K) to
subscripts (indexes) in an array(Table) of size M.
• In general, since |K| >> M, the mapping defined by a hash
function will be a many-to-one mapping.
• Hash Collision : Hash Collision occurs when hash function
maps distinct keys x and y to the same index, i.e. When x not
equal to y f(x) = f(y) .
• Collisions can be reduced with a selection of a good hash
function
Hashing: Hash Function Example
Example : A record with key value 37
f(37) : 37%10
= 7
A record with key value 37 is mapped to index 7
Example : A record with key value 87
f(87) : 87%10
= 7
A record with key value 37 is mapped to index 7
Two records/items with different key values ie
37,87 mapped to same index ie 7 of the hash
table/array results in collision
Hashing: Hash Function Example
Example:
•K = {0, 1, ..., 199}, ie Number of different
key values =200
•Hash Table Size (Array Size) M =10
•Hash Function -for each key k in K,
Hash function Defintion
f(k) : k % M
i.e. f(k) : k % Table Size
i.e. f(k) : k % 10
Example : A record with key value 25
f(25) : 25%10
= 5
A record with key value 25 is mapped to
index 5
Example :
Insert 36,48,90,12 using key%10 hash
function
Index HT[index]
0 90
1 12
2
3
4
5
6 36
7
8 48
9
10
Hash Function
The characteristics of a good hash function are
as follows.
• It avoids collisions.
• It tends to spread keys evenly in the array.
• It is easy to compute (i.e., computational time
of a hash function should be O(1)
Collision Resolution Techniques
Collision Resolution Techniques
• Open Hashing : In open hashing, keys are stored in linked lists
attached to cells of a hash table
 Chaining( Separate Chaining)
• Closed Hashing(Open Addressing):In closed hashing, all keys
are stored in the hash table itself without the use of linked
lists.
 Linear Probing Method
 Quadratic Probing Method
 Double Hashing Method
Separate Chaining
• When an element needs to be searched on the hash table, the hash
function f(k) = k % M will specify the address i within the range [0,
M -1]corresponding the singly linked list i that may contain the
node.
• Searching an element on the hash table turns out the problem of
searching an element on the singly linked list
• The hash table is implemented by using singly linked list.
• Elements on the hash table are hashed into M (e.g., M = 10) singly
linked lists (from list 0 to list M-1).
• Elements conflicted at the address i are directly connected in the
list i, where 0 ≤i ≤M-1.
• When an element with the key k is added into the hash table, the
hash function f(k) = k % M will identify the address i between 0 and
M -1 corresponding the singly linked list i where this node will be
added
Separate Chaining(Table size 10 hash
function f(k)= k%10)
Linear Probing Method
• All elements are stored in the hash table itself
• The hash table is initialized, all M locations assigned to -1 (i.e., empty or vacant).
• When a node with the key k needs to be added into the hash table, the hash function
f(k) = k % M
will specify the address/index
i = f(k) (i.e., an index of an array)
within the range [0, M-1].
• If there is no conflict (i.e., the cell i is unoccupied), then this node is added into the hash
table at the address i.
• If a collision takes place, then the hash function rehashes first time to consider the
next address (i.e., i + 1).
• If conflict occurs again, then the hash function rehashes second time to examine the
next address (i.e., i + 2).
• This process repeats until the available address found then this node will be added at
this address.
15
Linear probing: Inserting a key
• Idea: when there is a collision, check the next available
position in the table (i.e., probing)
h(k,i) = (h1(k) + i) mod m
i=0,1,2,...
• First slot probed: h1(k)
• Second slot probed: h1(k) + 1
• Third slot probed: h1(k)+2, and so on
• Can generate m probe sequences maximum, why?
probe sequence: < h1(k), h1(k)+1 , h1(k)+2 , ....>
wrap around
16
Linear probing: Deleting a key
• Problems
– Cannot mark the slot as empty
– Impossible to retrieve keys inserted
after that slot was occupied
• Solution
– Mark the slot with a sentinel value
DELETED
• The deleted slot can later be used
for insertion
• Searching will be able to find all the
keys
0
m - 1
Linear Probing
Linear Probing
• Hash Table Size : 7 , So f(k) = k%7
Probe Sequence is for Insert(47): {5,6,0,1,2,3,4}
Table[4]= 47 //Insert
Linear Probing
Linear Probing
Quadratic Probing
Quadratic Probing
Quadratic Probing
Quadratic Probing
Quadratic Probing
26
Double Hashing
(1) Use one hash function to determine the first slot
(2) Use a second hash function to determine the
increment for the probe sequence
h(k,i) = (h1(k) + i h2(k) ) mod m, i=0,1,...
• Initial probe: h1(k)
• Second probe is offset by h2(k) mod m, so on ...
• Advantage: avoids clustering
• Disadvantage: harder to delete an element
• Can generate m2 probe sequences maximum
27
Double Hashing: Example
h1(k) = k mod 13
h2(k) = 1+ (k mod 11)
h(k,i) = (h1(k) + i h2(k) ) mod 13
• Insert key 14:
h1(14,0) = 14 mod 13 = 1
h(14,1) = (h1(14) + h2(14)) mod 13
= (1 + 4) mod 13 = 5
h(14,2) = (h1(14) + 2 h2(14)) mod 13
= (1 + 8) mod 13 = 9
79
69
98
72
50
0
9
4
2
3
1
5
6
7
8
10
11
12
14
Example
For the input 30, 20, 56, 75, 31, 19 and hash function h(K) = K mod 11
construct the open hash table.
• find the largest number of key comparisons in a successful search in
this table.
• find the average number of key comparisons in a successful search
in this table.
For the input 30, 20, 56, 75, 31, 19 and hash function h(K) = K mod 11
construct the closed hash table.
• find the largest number of key comparisons in a successful search
in this table
• find the average number of key comparisons in a successful search
in this table.

More Related Content

Similar to 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 IISajid Marwat
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptxkratika64
 
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptx
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptxPPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptx
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptxCHANDUS31
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashingRafi Dar
 
11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithmfarhankhan89766
 
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 icajiwol341
 
HASHING IS NOT YASH IT IS HASH.pptx
HASHING IS NOT YASH IT IS HASH.pptxHASHING IS NOT YASH IT IS HASH.pptx
HASHING IS NOT YASH IT IS HASH.pptxJITTAYASHWANTHREDDY
 
Hash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptxHash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptxmy6305874
 
Hashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniquesHashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniquesssuserec8a711
 

Similar to Hashing using a different methods of technic (20)

Hashing .pptx
Hashing .pptxHashing .pptx
Hashing .pptx
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec II
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
Randamization.pdf
Randamization.pdfRandamization.pdf
Randamization.pdf
 
Hashing
HashingHashing
Hashing
 
Hashing
HashingHashing
Hashing
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptx
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptxPPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptx
PPT 2 wirha DSA hasings dvd ho gi of DJ of ch huu Raj of DJ.pptx
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashing
 
Quadratic probing
Quadratic probingQuadratic probing
Quadratic probing
 
11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm
 
Hash tables
Hash tablesHash tables
Hash tables
 
Maps&hash tables
Maps&hash tablesMaps&hash tables
Maps&hash tables
 
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
 
HASHING IS NOT YASH IT IS HASH.pptx
HASHING IS NOT YASH IT IS HASH.pptxHASHING IS NOT YASH IT IS HASH.pptx
HASHING IS NOT YASH IT IS HASH.pptx
 
Lec5
Lec5Lec5
Lec5
 
Hashing
HashingHashing
Hashing
 
Hash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptxHash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptx
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
Hashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniquesHashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniques
 

Recently uploaded

Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 

Recently uploaded (20)

Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 

Hashing using a different methods of technic

  • 2. Hashing When implementing a hash table using arrays, the nodes/elements are not stored consecutively, instead the location of an element/node to be inserted/deleted/searched is computed using the key and a hash function.
  • 3. Hash Table • Hash table is a data structure which is designed specifically with the objective of providing efficient insertion and searching operations (deletion is not a primary objective). • Hash Table achieves constant time performance O(1) time • To achieve constant time performance objective, the implementation must be based in some way on an array rather than a linked list.
  • 4. Hash Table • Hash table is a container which will be used to hold some number of items of a given set K. We call the elements of the set K keys and K is called the key space • In the general case, we expect the size of the set of keys, denoted as |K|, to be relatively large in comparison with the number of items stored in the table of size M • The general approach is to store the keys in an array. The position (also called location or index) i of a key k in the array is given by a function hash(k) or f(k), called a hash function • Hashing – Hash function determines the position of a given key directly from that key (i.e., i = f(k) is called the hash code of k) Example :For a Student Record USN is the key Example USN= 1234 Value = Hash(1234) or value =f(1234) where value is the hash value of the Student Record with key(USN) 1234.
  • 5. Hashing : Hash Function & Hash Collision • Hash Function f f: K → {0, 1, ..., M-1}. • Hash function f maps (or transforms) the set of key values(K) to subscripts (indexes) in an array(Table) of size M. • In general, since |K| >> M, the mapping defined by a hash function will be a many-to-one mapping. • Hash Collision : Hash Collision occurs when hash function maps distinct keys x and y to the same index, i.e. When x not equal to y f(x) = f(y) . • Collisions can be reduced with a selection of a good hash function
  • 6. Hashing: Hash Function Example Example : A record with key value 37 f(37) : 37%10 = 7 A record with key value 37 is mapped to index 7 Example : A record with key value 87 f(87) : 87%10 = 7 A record with key value 37 is mapped to index 7 Two records/items with different key values ie 37,87 mapped to same index ie 7 of the hash table/array results in collision
  • 7. Hashing: Hash Function Example Example: •K = {0, 1, ..., 199}, ie Number of different key values =200 •Hash Table Size (Array Size) M =10 •Hash Function -for each key k in K, Hash function Defintion f(k) : k % M i.e. f(k) : k % Table Size i.e. f(k) : k % 10 Example : A record with key value 25 f(25) : 25%10 = 5 A record with key value 25 is mapped to index 5
  • 9. Insert 36,48,90,12 using key%10 hash function Index HT[index] 0 90 1 12 2 3 4 5 6 36 7 8 48 9 10
  • 10. Hash Function The characteristics of a good hash function are as follows. • It avoids collisions. • It tends to spread keys evenly in the array. • It is easy to compute (i.e., computational time of a hash function should be O(1)
  • 11. Collision Resolution Techniques Collision Resolution Techniques • Open Hashing : In open hashing, keys are stored in linked lists attached to cells of a hash table  Chaining( Separate Chaining) • Closed Hashing(Open Addressing):In closed hashing, all keys are stored in the hash table itself without the use of linked lists.  Linear Probing Method  Quadratic Probing Method  Double Hashing Method
  • 12. Separate Chaining • When an element needs to be searched on the hash table, the hash function f(k) = k % M will specify the address i within the range [0, M -1]corresponding the singly linked list i that may contain the node. • Searching an element on the hash table turns out the problem of searching an element on the singly linked list • The hash table is implemented by using singly linked list. • Elements on the hash table are hashed into M (e.g., M = 10) singly linked lists (from list 0 to list M-1). • Elements conflicted at the address i are directly connected in the list i, where 0 ≤i ≤M-1. • When an element with the key k is added into the hash table, the hash function f(k) = k % M will identify the address i between 0 and M -1 corresponding the singly linked list i where this node will be added
  • 13. Separate Chaining(Table size 10 hash function f(k)= k%10)
  • 14. Linear Probing Method • All elements are stored in the hash table itself • The hash table is initialized, all M locations assigned to -1 (i.e., empty or vacant). • When a node with the key k needs to be added into the hash table, the hash function f(k) = k % M will specify the address/index i = f(k) (i.e., an index of an array) within the range [0, M-1]. • If there is no conflict (i.e., the cell i is unoccupied), then this node is added into the hash table at the address i. • If a collision takes place, then the hash function rehashes first time to consider the next address (i.e., i + 1). • If conflict occurs again, then the hash function rehashes second time to examine the next address (i.e., i + 2). • This process repeats until the available address found then this node will be added at this address.
  • 15. 15 Linear probing: Inserting a key • Idea: when there is a collision, check the next available position in the table (i.e., probing) h(k,i) = (h1(k) + i) mod m i=0,1,2,... • First slot probed: h1(k) • Second slot probed: h1(k) + 1 • Third slot probed: h1(k)+2, and so on • Can generate m probe sequences maximum, why? probe sequence: < h1(k), h1(k)+1 , h1(k)+2 , ....> wrap around
  • 16. 16 Linear probing: Deleting a key • Problems – Cannot mark the slot as empty – Impossible to retrieve keys inserted after that slot was occupied • Solution – Mark the slot with a sentinel value DELETED • The deleted slot can later be used for insertion • Searching will be able to find all the keys 0 m - 1
  • 18. Linear Probing • Hash Table Size : 7 , So f(k) = k%7 Probe Sequence is for Insert(47): {5,6,0,1,2,3,4} Table[4]= 47 //Insert
  • 26. 26 Double Hashing (1) Use one hash function to determine the first slot (2) Use a second hash function to determine the increment for the probe sequence h(k,i) = (h1(k) + i h2(k) ) mod m, i=0,1,... • Initial probe: h1(k) • Second probe is offset by h2(k) mod m, so on ... • Advantage: avoids clustering • Disadvantage: harder to delete an element • Can generate m2 probe sequences maximum
  • 27. 27 Double Hashing: Example h1(k) = k mod 13 h2(k) = 1+ (k mod 11) h(k,i) = (h1(k) + i h2(k) ) mod 13 • Insert key 14: h1(14,0) = 14 mod 13 = 1 h(14,1) = (h1(14) + h2(14)) mod 13 = (1 + 4) mod 13 = 5 h(14,2) = (h1(14) + 2 h2(14)) mod 13 = (1 + 8) mod 13 = 9 79 69 98 72 50 0 9 4 2 3 1 5 6 7 8 10 11 12 14
  • 29. For the input 30, 20, 56, 75, 31, 19 and hash function h(K) = K mod 11 construct the open hash table. • find the largest number of key comparisons in a successful search in this table. • find the average number of key comparisons in a successful search in this table. For the input 30, 20, 56, 75, 31, 19 and hash function h(K) = K mod 11 construct the closed hash table. • find the largest number of key comparisons in a successful search in this table • find the average number of key comparisons in a successful search in this table.