Tutorial 3 - Part1
Tutorial 3 - Part1
Maps
Tutorial 3
Ahmed Fahmy
ECE 250 @uWaterloo
Motivation
• Complexity John 1
• Problem: collisions! 3
• Later...
• If keys are objects: 4
• Strings
• User-defined class
• Solution: transform the object into some integer
Hashing
• We can use
• Insert
• 4, 10, 33, 2
• Chaining
33 10 4
0 1 2 3 4 5 6 7
Collision
unsigned int hash(type obj, unsigned int size) {
return obj.hash() & ((1 << m) – 1);
}
• Insert
• 4, 10, 33, 2
• Chaining
• Open-addressing
33 10 4
0 1 2 3 4 5 6 7
Linear-Probing
unsigned int hash(type obj, unsigned int size) {
return obj.hash() & ((1 << m) – 1);
}
• Insert
• 4, 10, 33, 2
• Check next location
• Search
• Stop when empty or full
33 10 4
0 1 2 3 4 5 6 7
Double Hashing
SDBM [1]
Quality of
Hashing
• Let us have an experiment:
• Pick a hash function
• Insert random numeric strings into
a hash map
• Draw the hash map as a picture:
• Each pixel is a cell
• Colored if cell is occupied
• White if cell is empty
DBJ2A [1]
Quality of
Hashing
• Let us have an experiment:
• Pick a hash function
• Insert random numeric strings into
a hash map
• Draw the hash map as a picture:
• Each pixel is a cell
• Colored if cell is occupied
• White if cell is empty
FNV1 [1]
Quality of
Hashing
• Let us have an experiment:
• Pick a hash function
• Insert random numeric strings into
a hash map
• Draw the hash map as a picture:
• Each pixel is a cell
• Colored if cell is occupied
• White if cell is empty
FNV1-A [1]
Quality of
Hashing
• Let us have an experiment:
• Pick a hash function
• Insert random numeric strings into
a hash map
• Draw the hash map as a picture:
• Each pixel is a cell
• Colored if cell is occupied
• White if cell is empty
Murmur2 [1]
Problem Solving
• Remove Duplicates
from (unsorted) vector void removeDubFast(vector<int>& v){ // un/sorted vector v
unordered_set<int> m;
for (int i:v)
• Complexity: m.insert(i);
v.clear();
for (int i:m)
v.push_back(i);
}
450000000
400000000
350000000
300000000
250000000
200000000
150000000
100000000
50000000
0
0 42000 84000 126000 168000 210000 252000 294000 336000 378000 420000 462000 504000 546000 588000 630000 672000 714000 756000 798000 840000 882000 924000 966000
Real Performance
Thank You
References
• [1]
https://softwareengineering.stackexchange.com/questions/49550/which-hashing-al
gorithm-is-best-for-uniqueness-and-speed