0% found this document useful (0 votes)
24 views18 pages

L19 Hashing III

Uploaded by

Akash Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views18 pages

L19 Hashing III

Uploaded by

Akash Sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Hashing - III

Instructor: Ashok Singh Sairam


Lecture Plan
• Double Hashing (continued from previous lecture)
• #Probes required for
§ Unsuccessful Search
§ Insertion
§ Successful retrieval
• Perfect Hashing

MA512: Data Structures and Algorithms


2
Open addressing methods – contd.
• We will examine three common techniques used
to compute the probe sequences required for
open addressing
1. Linear probing
2. Quadratic probing
3. Double hashing

• Note: None of these methods can generate more


than m2 different probing sequences!

MA512: Data Structures and Algorithms


3
Double Hashing
• One of the best methods available for open addressing

where both h1 and h2 are auxiliary hash functions


• 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

MA512: Data Structures and Algorithms


4
Double Hashing: Example
0
h1(k) = k mod 13 1 79

h2(k) = 1+ (k mod 11) 2


3
h(k,i) = (h1(k) + i h2(k) ) mod 13 4 69

• Insert key 14: 5 98

6
h(14,0) = 14 mod 13 = 1 7 72

h(14,1) = (h1(14) + h2(14)) mod 13 8


9 14
= (1 + 4) mod 13 = 5 10
h(14,2) = (h1(14) + 2 h2(14)) mod 13 11 50
12
= (1 + 8) mod 13 = 9
MA512: Data Structures and Algorithms
5
#Probes for unsuccessful search

MA512: Data Structures and Algorithms


6
#Probes for insertion

MA512: Data Structures and Algorithms


7
#Probes for successful retrieval

MA512: Data Structures and Algorithms


8

MA512: Data Structures and Algorithms


9
Review

MA512: Data Structures and Algorithms


10
Ex: Linear Probing
• Keys = (10, 22, 31, 4, 15, 28, 17, 88, 59)
• h(k,i)=(h1(k)+i)mod m, given h1(k)=k; m = 11 0
1
2
3
4
5
6
7
8
9
10

MA512: Data Structures and Algorithms


11
Ex: Quadratic Probing
• Keys = (10, 22, 31, 4, 15, 28, 17, 88, 59)
• h(k,i)=(h1(k)+c1i+c2i2) mod m, 0
1
• h1(k)=k ; c1=1; c2=3; m = 11
2
3
4
5
6
7
8
9
10

MA512: Data Structures and Algorithms


12
Ex: Double Hashing
• Keys = (10, 22, 31, 4, 15, 28, 17, 88, 59)
• h(k,i)=(h1(k)+i h2(k)) mod m, m = 11 0
1
• h1(k)=k; h2(k)=(1 + k) mod 10
2
3
4
5
6
7
8
9
10

MA512: Data Structures and Algorithms


13
Perfect Hashing

MA512: Data Structures and Algorithms


14
Perfect Hashing: The Idea

j
U

Level 1 Level 2
MA512: Data Structures and Algorithms
15
Perfect Hashing: Example

MA512: Data Structures and Algorithms


16
Probability of collision

MA512: Data Structures and Algorithms


17
Exercise

MA512: Data Structures and Algorithms


18

You might also like