Recitation 8:
Hashing
CIT 5940
March 29, 2023
Attendance Code:
47638
Announcements
(Quick Recap) Open Hashing
Open Hashing
Relatively straight forward, right?
Closed Hashing
Confusingly also called “Open Addressing”
Closed Hashing
Load Factor & Rehashing
Load Factor: The ratio of entries to table size before an increment in size of the data structure is required
Rehashing: Technique where the table is resized (doubled)
Closed Hashing with Buckets
** Why is this??
Alternative to Buckets: Probing
Probe function: function used by a collision resolution method to calculate where to look next in the hash table
If a collision occurs, the probe function will traverse to the next slot as determined by the function starting from a home slot.
(home + p(key, i)) % M
home = hash(key)
p(key, i) = probe function
M = capacity of hash table
No Buckets: Linear Probing/Probing by Steps
Linear probing:
Linear probing by Steps:
No Buckets: Pseudo-Random probing and Quadratic
Pseudo-Random probing
Quadratic probing
(one variant)
* Easy to visualize *
* There’s a small problem with this one. What is it?
No Buckets: Double Hashing
Double Hashing Example
h1(k) = k % 11 (first hash function)
h2(k) = 8 - (k % 8) (second hash function)
Steps:
Step 1: K = 20, h1(20) = 20 mod 11 = 9 No collision occurs.
Step 2: K = 34, h1(34) = 34 mod 11 = 1, No collision occurs.
Step 3: K = 45, h1(45) = 45 mod 11 = 1, Collision occur because index 1 is already occupied by 34. Now we will use the second hash function to calculate the index for the key 45.
h2(45) = 8 - (45 mod 8) = 3
h(45, 1) = (1 + 1 * 3) mod 11 = 4
…
Let’s Play:
GUESS THAT COLLISION!
Which probe function is being used?
Insertions = {24, 19, 34, 9, 4}
34 | 9 | 4 | 24 | 19 |
Which probe function is being used?
Insertions = {24, 19, 34, 9, 4}
34 | 9 | 4 | 24 | 19 |
Linear Probing:
pos = (hash1(key) + i) % 5
Which probe function is being used?
Insertions = {24, 36, 34, 8, 30 }
8 | | 30 | | 24 | | 36 | | 34 | |
Which probe function is being used?
Insertions = {24, 36, 34, 8, 30 }
8 | | 30 | | 24 | | 36 | | 34 | |
Linear Probing with Steps = 2:
pos = (hash1(key) + 2i) % 10
Which probe function is being used?
30 | | | 23 | 24 | | 36 | | 33 | 34 |
Insertions = {24, 36, 34, 30, 23, 33. 43. 53 }
Which probe function is being used?
Insertions = {24, 36, 34, 30, 23, 33, 43, 53 }
30 | | | 23 | 24 | | 36 | | 33 | 34 |
Quadratic Probing with c1 = 5 and c2 = 10:
pos = (hash1(key) + c1i + c2i2) % 10
cluster 1
cluster 2
Not added
Which probe function is being used?
Insertions = {24, 23, 34, 22, 32, 62, 72}
| | 22 | 23 | 24 | 32 | 34 | 62 | 72 | |
Which probe function is being used?
| | 22 | 23 | 24 | 32 | 34 | 62 | 72 | |
Insertions = {24, 23, 34, 22, 32, 62, 72}
Double Hashing with prime 3:
pos = (hash1(key) + i * hash2(key)) % 10
hash2(key) = 3 - (hash1(key) % 3)
Recitation Activity:
Now you write the probe functions!
Harry’s Note:
Over the past few lectures, we studied various implementations of hash tables. While this analysis is useful to understanding how these data structures are implemented, our discussion focused solely on implementation details at the expense of any practice in using them. This activity will help remedy that and also serves as a preview for our upcoming discussion of Indexing. In completing this lab, you will develop a program that organizes data by two different keys. Reflect on how we adapt to the limitations of Maps by adding another level of indirection to our program.
Collision Resolution Policy: Strategy Pattern
<<interface>>
CollisionResolutionPolicy
—
probe() & setup()
LinearProbingCRP
LinearProbingStepsCRP
QuadraticProbingCRP
DoubleHashingCRP
Ignore (if present)
SUBMIT
SUBMIT
SUBMIT
SUBMIT
Important/ Runnable Files
Ignore (if present)
OpenAddressingBucket/HashTable
Experiment:
HashingTest:
SUBMIT
SUBMIT
SUBMIT
SUBMIT
TODOS:
QuadraticProbingCRP has been filled out for you to reference how make a probing function.
Fill out the probing functions in:
Run HashingTest to test if they work properly.
Ignore (if present)
SUBMIT
SUBMIT
SUBMIT
SUBMIT
Experiments and Observations
Attendance Code:
47638