CSE 163
Hashing
��Hunter Schafer
Data Structures
2
How can the set/dictionary have run-times that don’t depend on the number of elements in the data structure?
Set of Ints
3
nums = set()
nums.add(11) # 11 % 10 == 1
nums.add(49) # 49 % 10 == 9
nums.add(24) # 24 % 10 == 4
nums.add(7) # 7 % 10 == 7
print(49 in nums) # True
print(5 in nums) # False
0 | | 0 | 0 | | 0 | 0 | | 0 | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
0
0
0
11
0
49
24
7
Chained Hashing
4
Hash Functions
5
Hashing in Practice
6
class Example:
def __init__(self, a, b, c):
# Initialize _a, _b, _c
def __eq__(self, other):
return self._a == other._a \
and self._b = other._b
def __hash__(self):
return hash((self._a, self._b))
Count-Min Sketch
7
Group Work:
Best Practices
When you first working with this group:
Tips:
8