SlideShare a Scribd company logo
Advanced Algorithms – COMS31900
Hashing part two
Static Perfect Hashing
Benjamin Sach
Dictionaries and Hashing recap
A dynamic dictionary stores (key, value)-pairs and supports:
Universe U of u keys.
Hash table T of size m n.
Collisions were fixed by chaining
A hash function maps a key x to position h(x)
- i.e T[h(x)] = (key, value).
n arbitrary operations arrive online, one at a time.
add(key, value), lookup(key) (which returns value) and delete(key)
(building linked lists)
Dictionaries and Hashing recap
A dynamic dictionary stores (key, value)-pairs and supports:
Universe U of u keys.
Hash table T of size m n.
Collisions were fixed by chaining
A hash function maps a key x to position h(x)
- i.e T[h(x)] = (key, value).
A set H of hash functions is weakly universal if for any
two keys x, y ∈ U (with x = y),
Pr h(x) = h(y)
1
m
(h is picked uniformly at random from H)
n arbitrary operations arrive online, one at a time.
add(key, value), lookup(key) (which returns value) and delete(key)
(building linked lists)
Dictionaries and Hashing recap
A dynamic dictionary stores (key, value)-pairs and supports:
Universe U of u keys.
Hash table T of size m n.
Collisions were fixed by chaining
A hash function maps a key x to position h(x)
- i.e T[h(x)] = (key, value).
A set H of hash functions is weakly universal if for any
two keys x, y ∈ U (with x = y),
Pr h(x) = h(y)
1
m
(h is picked uniformly at random from H)
For any n operations, the expected
run-time is O(1) per operation.
Using weakly universal hashing:
n arbitrary operations arrive online, one at a time.
add(key, value), lookup(key) (which returns value) and delete(key)
(building linked lists)
Dictionaries and Hashing recap
A dynamic dictionary stores (key, value)-pairs and supports:
Universe U of u keys.
Hash table T of size m n.
Collisions were fixed by chaining
A hash function maps a key x to position h(x)
- i.e T[h(x)] = (key, value).
A set H of hash functions is weakly universal if for any
two keys x, y ∈ U (with x = y),
Pr h(x) = h(y)
1
m
(h is picked uniformly at random from H)
For any n operations, the expected
run-time is O(1) per operation.
But this doesn’t tell us much about the
worst-case behaviour
Using weakly universal hashing:
n arbitrary operations arrive online, one at a time.
add(key, value), lookup(key) (which returns value) and delete(key)
(building linked lists)
Static Dictionaries and Perfect hashing
A static dictionary stores (key, value)-pairs and supports:
Hash table T of size m n.
A hash function maps a key x to position h(x)
- i.e T[h(x)] = (key, value).
we are given n different (key, value)-pairs and want to pick a good h
lookup(key) (which returns value) - no inserts or deletes are allowed
Universe U of u keys.
Collisions were fixed by chaining
(building linked lists)
Static Dictionaries and Perfect hashing
A static dictionary stores (key, value)-pairs and supports:
Hash table T of size m n.
A hash function maps a key x to position h(x)
- i.e T[h(x)] = (key, value).
we are given n different (key, value)-pairs and want to pick a good h
lookup(key) (which returns value) - no inserts or deletes are allowed
THEOREM
The FKS hashing scheme:
• Has no collisions
• Every lookup takes O(1) worst-case time,
• Uses O(n) space,
• Can be built in O(n) expected time.
Universe U of u keys.
Collisions were fixed by chaining
(building linked lists)
Static Dictionaries and Perfect hashing
A static dictionary stores (key, value)-pairs and supports:
Hash table T of size m n.
A hash function maps a key x to position h(x)
- i.e T[h(x)] = (key, value).
we are given n different (key, value)-pairs and want to pick a good h
lookup(key) (which returns value) - no inserts or deletes are allowed
THEOREM
The FKS hashing scheme:
• Has no collisions
• Every lookup takes O(1) worst-case time,
• Uses O(n) space,
• Can be built in O(n) expected time.
The rest of this lecture is devoted to the
FKS scheme
Universe U of u keys.
Collisions were fixed by chaining
(building linked lists)
Static Dictionaries and Perfect hashing
A static dictionary stores (key, value)-pairs and supports:
Hash table T of size m n.
A hash function maps a key x to position h(x)
- i.e T[h(x)] = (key, value).
we are given n different (key, value)-pairs and want to pick a good h
lookup(key) (which returns value) - no inserts or deletes are allowed
THEOREM
The FKS hashing scheme:
• Has no collisions
• Every lookup takes O(1) worst-case time,
• Uses O(n) space,
• Can be built in O(n) expected time.
The rest of this lecture is devoted to the
FKS scheme
The construction is based on weak
universal hashing
Universe U of u keys.
Collisions were fixed by chaining
(building linked lists)
Static Dictionaries and Perfect hashing
A static dictionary stores (key, value)-pairs and supports:
Hash table T of size m n.
A hash function maps a key x to position h(x)
- i.e T[h(x)] = (key, value).
we are given n different (key, value)-pairs and want to pick a good h
lookup(key) (which returns value) - no inserts or deletes are allowed
THEOREM
The FKS hashing scheme:
• Has no collisions
• Every lookup takes O(1) worst-case time,
• Uses O(n) space,
• Can be built in O(n) expected time.
The rest of this lecture is devoted to the
FKS scheme
The construction is based on weak
universal hashing
(with an O(1) time hash function)
Universe U of u keys.
Collisions were fixed by chaining
(building linked lists)
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
n
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
n
(where any h(x) can be computed in O(1) time)
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
n
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
Step 2: Check for collisions
n
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Profit!
n
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
n
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
How many collisions do we get on average?
n
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
How many collisions do we get on average?
where indicator random variable Ix,y = 1 iff h(x) = h(y).
number of
collisions
n
E(C) = E
x,y∈T,x<y
Ix,y =
x,y∈T, x<y
E(Ix,y)
x,y∈T, x<y
1
m
=
n
2
·
1
m
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
How many collisions do we get on average?
where indicator random variable Ix,y = 1 iff h(x) = h(y).
number of
collisions
n
E(C) = E
x,y∈T,x<y
Ix,y =
x,y∈T, x<y
E(Ix,y)
x,y∈T, x<y
1
m
=
n
2
·
1
m
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
How many collisions do we get on average?
where indicator random variable Ix,y = 1 iff h(x) = h(y).
number of
collisions
n
Linearity of Expectation
Let Y1, Y2, . . . , Yk be k random variables. Then
E
k
i=1
Yi =
k
i=1
E(Yi)
E(C) = E
x,y∈T,x<y
Ix,y =
x,y∈T, x<y
E(Ix,y)
x,y∈T, x<y
1
m
=
n
2
·
1
m
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
How many collisions do we get on average?
where indicator random variable Ix,y = 1 iff h(x) = h(y).
number of
collisions
linearity of
expectation
n
E(C) = E
x,y∈T,x<y
Ix,y =
x,y∈T, x<y
E(Ix,y)
x,y∈T, x<y
1
m
=
n
2
·
1
m
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
How many collisions do we get on average?
where indicator random variable Ix,y = 1 iff h(x) = h(y).
number of
collisions
linearity of
expectation
n
E(C) = E
x,y∈T,x<y
Ix,y =
x,y∈T, x<y
E(Ix,y)
x,y∈T, x<y
1
m
=
n
2
·
1
m
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
How many collisions do we get on average?
where indicator random variable Ix,y = 1 iff h(x) = h(y).
number of
collisions
linearity of
expectation
definition of
expectation
n
E(C) = E
x,y∈T,x<y
Ix,y =
x,y∈T, x<y
E(Ix,y)
x,y∈T, x<y
1
m
=
n
2
·
1
m
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
How many collisions do we get on average?
where indicator random variable Ix,y = 1 iff h(x) = h(y).
number of
collisions
linearity of
expectation
E(Ix,y) = 1 · Pr(Ix,y = 1) + 0 · Pr(Ix,y = 0)
1
m
n
E(C) = E
x,y∈T,x<y
Ix,y =
x,y∈T, x<y
E(Ix,y)
x,y∈T, x<y
1
m
=
n
2
·
1
m
By the definition of expectation. . .
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
How many collisions do we get on average?
where indicator random variable Ix,y = 1 iff h(x) = h(y).
number of
collisions
linearity of
expectation
definition of
expectation
n
E(C) = E
x,y∈T,x<y
Ix,y =
x,y∈T, x<y
E(Ix,y)
x,y∈T, x<y
1
m
=
n
2
·
1
m
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
How many collisions do we get on average?
where indicator random variable Ix,y = 1 iff h(x) = h(y).
number of
collisions
linearity of
expectation
definition of
expectation
n
E(C) = E
x,y∈T,x<y
Ix,y =
x,y∈T, x<y
E(Ix,y)
x,y∈T, x<y
1
m
=
n
2
·
1
m
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
How many collisions do we get on average?
where indicator random variable Ix,y = 1 iff h(x) = h(y).
number of
collisions
linearity of
expectation
definition of
expectation
n
2
=
n(n − 1)
2
n
E(C) = E
x,y∈T,x<y
Ix,y =
x,y∈T, x<y
E(Ix,y)
x,y∈T, x<y
1
m
=
n
2
·
1
m
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
How many collisions do we get on average?
where indicator random variable Ix,y = 1 iff h(x) = h(y).
number of
collisions
linearity of
expectation
definition of
expectation n2/2
n
E(C) = E
x,y∈T,x<y
Ix,y =
x,y∈T, x<y
E(Ix,y)
x,y∈T, x<y
1
m
=
n
2
·
1
m
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
How many collisions do we get on average?
where indicator random variable Ix,y = 1 iff h(x) = h(y).
number of
collisions
linearity of
expectation
definition of
expectation n2/2
n
E(C) = E
x,y∈T,x<y
Ix,y =
x,y∈T, x<y
E(Ix,y)
x,y∈T, x<y
1
m
=
n
2
·
1
m
n2
2m
Perfect hashing - a first attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
Step 1: Insert everything into a hash table of size m = n
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
How many collisions do we get on average?
where indicator random variable Ix,y = 1 iff h(x) = h(y).
number of
collisions
linearity of
expectation
definition of
expectation n2/2
n
E(C) = E
x,y∈T,x<y
Ix,y =
x,y∈T, x<y
E(Ix,y)
x,y∈T, x<y
1
m
=
n
2
·
1
m
n2
2m
n
2
.
Perfect hashing - a second attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
n2
Step 1: Insert everything into a hash table of size m = n2
Perfect hashing - a second attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
n2
Step 1: Insert everything into a hash table of size m = n2
How many collisions do we get on average?
Perfect hashing - a second attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
n2
Step 1: Insert everything into a hash table of size m = n2
How many collisions do we get on average?
where indicator random variable Ix,y = 1 iff h(x) = h(y).
number of
collisions
linearity of
expectation
definition of
expectation n2/2
E(C) = E
x,y∈T,x<y
Ix,y =
x,y∈T, x<y
E(Ix,y)
x,y∈T, x<y
1
m
=
n
2
·
1
m
n2
2m
Perfect hashing - a second attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
n2
Step 1: Insert everything into a hash table of size m = n2
How many collisions do we get on average?
where indicator random variable Ix,y = 1 iff h(x) = h(y).
number of
collisions
linearity of
expectation
definition of
expectation n2/2
E(C) = E
x,y∈T,x<y
Ix,y =
x,y∈T, x<y
E(Ix,y)
x,y∈T, x<y
1
m
=
n
2
·
1
m
n2
2m
1
2
Perfect hashing - a second attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
n2
Step 1: Insert everything into a hash table of size m = n2
How many collisions do we get on average?
where indicator random variable Ix,y = 1 iff h(x) = h(y).
number of
collisions
linearity of
expectation
definition of
expectation n2/2
E(C) = E
x,y∈T,x<y
Ix,y =
x,y∈T, x<y
E(Ix,y)
x,y∈T, x<y
1
m
=
n
2
·
1
m
n2
2m
1
2
much
better!
Perfect hashing - a second attempt
A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y),
Pr h(x) = h(y)
1
m
where h is picked uniformly at random from H
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if necessary
n2
Step 1: Insert everything into a hash table of size m = n2
How many collisions do we get on average?
where indicator random variable Ix,y = 1 iff h(x) = h(y).
number of
collisions
linearity of
expectation
definition of
expectation n2/2
E(C) = E
x,y∈T,x<y
Ix,y =
x,y∈T, x<y
E(Ix,y)
x,y∈T, x<y
1
m
=
n
2
·
1
m
n2
2m
1
2
much
(except we cheated)
better!
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there was a collision
Step 1: Insert everything into a hash table of size m = n2
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there was a collision
Step 1: Insert everything into a hash table of size m = n2
How many times do we repeat on average?
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there was a collision
Step 1: Insert everything into a hash table of size m = n2
How many times do we repeat on average?
The expected number of collisions: E(C) 1
2
The probability of at least one collision: Pr(C 1) 1
2
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there was a collision
Step 1: Insert everything into a hash table of size m = n2
How many times do we repeat on average?
The expected number of collisions: E(C) 1
2
The probability of at least one collision: Pr(C 1) 1
2
Markov’s inequality
If X is a non-negative r.v., then for all a > 0,
Pr(X a)
E(X)
a
.
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there was a collision
Step 1: Insert everything into a hash table of size m = n2
How many times do we repeat on average?
The expected number of collisions: E(C) 1
2
The probability of at least one collision: Pr(C 1) 1
2
Markov’s inequality
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there was a collision
Step 1: Insert everything into a hash table of size m = n2
How many times do we repeat on average?
The expected number of collisions: E(C) 1
2
The probability of at least one collision: Pr(C 1) 1
2
The probability of zero collisions is at least 1
2
Markov’s inequality
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there was a collision
Step 1: Insert everything into a hash table of size m = n2
How many times do we repeat on average?
The expected number of collisions: E(C) 1
2
The probability of at least one collision: Pr(C 1) 1
2
The probability of zero collisions is at least 1
2
i.e. at least as good as tossing a heads on a fair coin
Markov’s inequality
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there was a collision
Step 1: Insert everything into a hash table of size m = n2
How many times do we repeat on average?
The expected number of collisions: E(C) 1
2
E(runs) E(coin tosses to get a heads) = 2
The probability of at least one collision: Pr(C 1) 1
2
The probability of zero collisions is at least 1
2
i.e. at least as good as tossing a heads on a fair coin
Markov’s inequality
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there was a collision
Step 1: Insert everything into a hash table of size m = n2
How many times do we repeat on average?
The expected number of collisions: E(C) 1
2
E(runs) E(coin tosses to get a heads) = 2
The probability of at least one collision: Pr(C 1) 1
2
The probability of zero collisions is at least 1
2
i.e. at least as good as tossing a heads on a fair coin
E(construction time) = O(m)·E(runs) = O(m) = O(n2)
Markov’s inequality
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there was a collision
Step 1: Insert everything into a hash table of size m = n2
How many times do we repeat on average?
The expected number of collisions: E(C) 1
2
E(runs) E(coin tosses to get a heads) = 2
The probability of at least one collision: Pr(C 1) 1
2
The probability of zero collisions is at least 1
2
i.e. at least as good as tossing a heads on a fair coin
E(construction time) = O(m)·E(runs) = O(m) = O(n2)
. . . and then the look-up time is always O(1)
Markov’s inequality
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there was a collision
Step 1: Insert everything into a hash table of size m = n2
How many times do we repeat on average?
The expected number of collisions: E(C) 1
2
E(runs) E(coin tosses to get a heads) = 2
The probability of at least one collision: Pr(C 1) 1
2
The probability of zero collisions is at least 1
2
i.e. at least as good as tossing a heads on a fair coin
E(construction time) = O(m)·E(runs) = O(m) = O(n2)
. . . and then the look-up time is always O(1)
(because any h(x) can be computed in O(1) time)
Markov’s inequality
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there are more than n collisions
Step 1: Insert everything into a hash table of size m = n
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there are more than n collisions
Step 1: Insert everything into a hash table of size m = n
This looks rubbish but
it will be useful in a bit!
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there are more than n collisions
Step 1: Insert everything into a hash table of size m = n
How many times do we repeat on average?
The expected number of collisions: E(C) n
2
The probability of at least n collisions: Pr(C n) 1
2
This looks rubbish but
it will be useful in a bit!
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there are more than n collisions
Step 1: Insert everything into a hash table of size m = n
How many times do we repeat on average?
The expected number of collisions: E(C) n
2
The probability of at least n collisions: Pr(C n) 1
2
This looks rubbish but
it will be useful in a bit!
(where a = n)
Markov’s inequality
If X is a non-negative r.v., then for all a > 0,
Pr(X a)
E(X)
a
.
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there are more than n collisions
Step 1: Insert everything into a hash table of size m = n
How many times do we repeat on average?
The expected number of collisions: E(C) n
2
The probability of at least n collisions: Pr(C n) 1
2
This looks rubbish but
it will be useful in a bit!
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there are more than n collisions
Step 1: Insert everything into a hash table of size m = n
How many times do we repeat on average?
The expected number of collisions: E(C) n
2
E(runs) E(coin tosses to get a heads) = 2
The probability of at least n collisions: Pr(C n) 1
2
i.e. at least as good as tossing a heads on a fair coin
E(construction time) = O(m)·E(runs) = O(m) = O(n)
This looks rubbish but
it will be useful in a bit!
The probability of at most n collisions is at least 1
2
Expected construction time
using a weakly universal hash function
Step 2: Check for collisions
Step 3: Repeat if there are more than n collisions
Step 1: Insert everything into a hash table of size m = n
How many times do we repeat on average?
The expected number of collisions: E(C) n
2
E(runs) E(coin tosses to get a heads) = 2
The probability of at least n collisions: Pr(C n) 1
2
i.e. at least as good as tossing a heads on a fair coin
E(construction time) = O(m)·E(runs) = O(m) = O(n)
. . . but the look-up time could be rubbish (lots of collisions)
This looks rubbish but
it will be useful in a bit!
The probability of at most n collisions is at least 1
2
Perfect hashing - attempt three
n
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal hash function, h
T
Perfect hashing - attempt three
n
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal hash function, h
Let ni be the number of items in T[i]
T
Perfect hashing - attempt three
n
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal hash function, h
Let ni be the number of items in T[i]
T
n1 = 2
n5 = 2
n8 = 3
Perfect hashing - attempt three
n
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal hash function, h
. . . but don’t use chaining
Step 2: The ni items in T[i] are inserted into
another hash table Ti of size n2
i
using another weakly universal hash function
Let ni be the number of items in T[i]
T
denoted hi (there is one for each i)
n2
i
Perfect hashing - attempt three
n
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal hash function, h
. . . but don’t use chaining
Step 2: The ni items in T[i] are inserted into
another hash table Ti of size n2
i
using another weakly universal hash function
Let ni be the number of items in T[i]
T
denoted hi (there is one for each i)
n2
i
Perfect hashing - attempt three
n
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal hash function, h
. . . but don’t use chaining
Step 2: The ni items in T[i] are inserted into
another hash table Ti of size n2
i
using another weakly universal hash function
Let ni be the number of items in T[i]
T
denoted hi (there is one for each i)
n2
i
(Step 3) Immediately repeat a step if either
a) T has more than n collisions
b) some Ti has a collision
Perfect hashing - attempt three
n
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal hash function, h
. . . but don’t use chaining
Step 2: The ni items in T[i] are inserted into
another hash table Ti of size n2
i
using another weakly universal hash function
Let ni be the number of items in T[i]
T
denoted hi (there is one for each i)
n2
i
(Step 3) Immediately repeat a step if either
a) T has more than n collisions
b) some Ti has a collision
i.e. check (and if necessary rebuild)
each table immediately after building it
Perfect hashing - attempt three
n
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal hash function, h
. . . but don’t use chaining
Step 2: The ni items in T[i] are inserted into
another hash table Ti of size n2
i
using another weakly universal hash function
Let ni be the number of items in T[i]
T
denoted hi (there is one for each i)
n2
i
(Step 3) Immediately repeat a step if either
a) T has more than n collisions
b) some Ti has a collision
Perfect hashing - attempt three
n
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal hash function, h
. . . but don’t use chaining
Step 2: The ni items in T[i] are inserted into
another hash table Ti of size n2
i
using another weakly universal hash function
Let ni be the number of items in T[i]
T
denoted hi (there is one for each i)
n2
i
(Step 3) Immediately repeat a step if either
a) T has more than n collisions
The look-up time is always O(1)
1. Compute i = h(x) (x is the key)
2. Compute j = hi(x)
3. The item is in Ti[j]
b) some Ti has a collision
Perfect hashing - attempt three
n
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal hash function, h
. . . but don’t use chaining
Step 2: The ni items in T[i] are inserted into
another hash table Ti of size n2
i
using another weakly universal hash function
Let ni be the number of items in T[i]
T
denoted hi (there is one for each i)
n2
i
(Step 3) Immediately repeat a step if either
a) T has more than n collisions
What is the expected construction time?
What is the space usage?
The look-up time is always O(1)
1. Compute i = h(x) (x is the key)
2. Compute j = hi(x)
3. The item is in Ti[j]
Two questions remain:
b) some Ti has a collision
Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
The size of Ti is O(ni
2)
Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
The size of Ti is O(ni
2)
Storing hi uses O(1) space
Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
The size of Ti is O(ni
2)
So the total space is. . .
Storing hi uses O(1) space
Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
The size of Ti is O(ni
2)
So the total space is. . .
O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i


Storing hi uses O(1) space
Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
The size of Ti is O(ni
2)
So the total space is. . .
Storing hi uses O(1) space
O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i


Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
The size of Ti is O(ni
2)
So the total space is. . .
Storing hi uses O(1) space
how big is this?
O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i


Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
The size of Ti is O(ni
2)
So the total space is. . .
Storing hi uses O(1) space
how big is this?
O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i


How big is i n2
i ?
Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
The size of Ti is O(ni
2)
So the total space is. . .
Storing hi uses O(1) space
how big is this?
O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i


How big is i n2
i ?
There are
ni
2 collisions in T[i]
Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
The size of Ti is O(ni
2)
So the total space is. . .
Storing hi uses O(1) space
how big is this?
O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i


How big is i n2
i ?
There are
ni
2 collisions in T[i] so there are i
ni
2 collisions in T
Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
The size of Ti is O(ni
2)
So the total space is. . .
Storing hi uses O(1) space
how big is this?
O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i


How big is i n2
i ?
There are
ni
2 collisions in T[i] so there are i
ni
2 collisions in T
but we know that there are at most n collisions in T . . .
Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
The size of Ti is O(ni
2)
So the total space is. . .
Storing hi uses O(1) space
how big is this?
O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i


How big is i n2
i ?
There are
ni
2 collisions in T[i] so there are i
ni
2 collisions in T
but we know that there are at most n collisions in T . . .
i
n2
i
4
i
ni
2
n
Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
The size of Ti is O(ni
2)
So the total space is. . .
Storing hi uses O(1) space
how big is this?
O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i


How big is i n2
i ?
There are
ni
2 collisions in T[i] so there are i
ni
2 collisions in T
but we know that there are at most n collisions in T . . .
i
n2
i
4
i
ni
2
n
Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
The size of Ti is O(ni
2)
So the total space is. . .
Storing hi uses O(1) space
how big is this?
O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i


How big is i n2
i ?
There are
ni
2 collisions in T[i] so there are i
ni
2 collisions in T
but we know that there are at most n collisions in T . . .
ni
2 =
ni(ni−1)
2
n2
i
4
i
n2
i
4
i
ni
2
n
Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
The size of Ti is O(ni
2)
So the total space is. . .
Storing hi uses O(1) space
how big is this?
O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i


How big is i n2
i ?
There are
ni
2 collisions in T[i] so there are i
ni
2 collisions in T
but we know that there are at most n collisions in T . . .
i
n2
i
4
i
ni
2
n
Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
The size of Ti is O(ni
2)
So the total space is. . .
Storing hi uses O(1) space
how big is this?
O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i


How big is i n2
i ?
There are
ni
2 collisions in T[i] so there are i
ni
2 collisions in T
but we know that there are at most n collisions in T . . .
i
n2
i
4
i
ni
2
n
i
n2
i 4nor
Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
The size of Ti is O(ni
2)
So the total space is. . .
Storing hi uses O(1) space
how big is this?
How big is i n2
i ?
There are
ni
2 collisions in T[i] so there are i
ni
2 collisions in T
but we know that there are at most n collisions in T . . .
i
n2
i
4
i
ni
2
n
i
n2
i 4nor
O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i

 = O(n)
Perfect Hashing - Space usage
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
How much space does this use?
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The size of T is O(n)
The size of Ti is O(ni
2)
So the total space is. . .
Storing hi uses O(1) space
O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i

 = O(n)
Perfect Hashing - Expected construction time
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
Perfect Hashing - Expected construction time
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The expected construction time for T is O(n)
(we considered this on a previous slide)
Perfect Hashing - Expected construction time
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The expected construction time for T is O(n)
(we considered this on a previous slide)
The expected construction time for each Ti is O(ni
2)
Perfect Hashing - Expected construction time
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The expected construction time for T is O(n)
(we considered this on a previous slide)
The expected construction time for each Ti is O(ni
2)
- we insert ni items into a table of size m = n2
i
- then repeat if there was a collision
Perfect Hashing - Expected construction time
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The expected construction time for T is O(n)
(we considered this on a previous slide)
The expected construction time for each Ti is O(ni
2)
- we insert ni items into a table of size m = n2
i
(we also considered this on a previous slide)
- then repeat if there was a collision
Perfect Hashing - Expected construction time
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The expected construction time for T is O(n)
(we considered this on a previous slide)
The expected construction time for each Ti is O(ni
2)
- we insert ni items into a table of size m = n2
i
(we also considered this on a previous slide)
- then repeat if there was a collision
The overall expected constuction time is therefore:
E(construction time) = E

construction time of T +
i
construction time of Ti


Perfect Hashing - Expected construction time
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The expected construction time for T is O(n)
The expected construction time for each Ti is O(ni
2)
The overall expected construction time is therefore:
E(construction time) = E

construction time of T +
i
construction time of Ti


Perfect Hashing - Expected construction time
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The expected construction time for T is O(n)
The expected construction time for each Ti is O(ni
2)
The overall expected construction time is therefore:
E(construction time) = E

construction time of T +
i
construction time of Ti


= E construction time of T)+
i
E(construction time of Ti
Perfect Hashing - Expected construction time
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The expected construction time for T is O(n)
The expected construction time for each Ti is O(ni
2)
The overall expected construction time is therefore:
E(construction time) = E

construction time of T +
i
construction time of Ti


= E construction time of T)+
i
E(construction time of Ti
= O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i


Perfect Hashing - Expected construction time
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The expected construction time for T is O(n)
The expected construction time for each Ti is O(ni
2)
The overall expected construction time is therefore:
E(construction time) = E

construction time of T +
i
construction time of Ti


= E construction time of T)+
i
E(construction time of Ti
= O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i

 = O(n)
Perfect Hashing - Expected construction time
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The expected construction time for T is O(n)
The expected construction time for each Ti is O(ni
2)
The overall expected construction time is therefore:
E(construction time) = E

construction time of T +
i
construction time of Ti


= E construction time of T)+
i
E(construction time of Ti
= O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i

 = O(n)
i
n2
i 4n
Perfect Hashing - Expected construction time
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
The expected construction time for T is O(n)
The expected construction time for each Ti is O(ni
2)
The overall expected construction time is therefore:
E(construction time) = E

construction time of T +
i
construction time of Ti


= E construction time of T)+
i
E(construction time of Ti
= O(n)+
i
O(n2
i ) = O(n)+O


i
n2
i

 = O(n)
Perfect Hashing - Summary
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
THEOREM
The FKS hashing scheme:
• Has no collisions
• Every lookup takes O(1) worst-case time,
• Uses O(n) space,
• Can be built in O(n) expected time.
The look-up time is always O(1)
1. Compute i = h(x) (x is the key)
2. Compute j = hi(x)
3. The item is in Ti[j]
Perfect Hashing - Summary
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
THEOREM
The FKS hashing scheme:
• Has no collisions
• Every lookup takes O(1) worst-case time,
• Uses O(n) space,
• Can be built in O(n) expected time.
The look-up time is always O(1)
1. Compute i = h(x) (x is the key)
2. Compute j = hi(x)
3. The item is in Ti[j]
Perfect Hashing - Summary
n n2
i
Step 1: Insert everything into a hash table, T, of size n
using a weakly universal (w.u.) hash function, h
Step 2: The ni items in T[i] are inserted into another hash table Ti
of size n2
i using w.u hash function hi
(Step 3) Immediately repeat if either
a) T has more than n collisions
b) some Ti has a collision
T
Ti
THEOREM
The FKS hashing scheme:
• Has no collisions
• Every lookup takes O(1) worst-case time,
• Uses O(n) space,
• Can be built in O(n) expected time.
The look-up time is always O(1)
1. Compute i = h(x) (x is the key)
2. Compute j = hi(x)
3. The item is in Ti[j]

More Related Content

What's hot

Hash table
Hash tableHash table
Hash table
Rajendran
 
Row major and column major in 2 d
Row major and column major in 2 dRow major and column major in 2 d
Row major and column major in 2 d
nikhilarora2211
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Kuntal Bhowmick
 
Python dictionary
Python dictionaryPython dictionary
Python dictionary
eman lotfy
 
Counting sort
Counting sortCounting sort
Counting sort
zahraa F.Muhsen
 
Java Linked List Tutorial | Edureka
Java Linked List Tutorial |  EdurekaJava Linked List Tutorial |  Edureka
Java Linked List Tutorial | Edureka
Edureka!
 
van Emde Boas trees
van Emde Boas treesvan Emde Boas trees
van Emde Boas trees
Benjamin Sach
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structureSajid Marwat
 
Heap tree
Heap treeHeap tree
Heap tree
JananiJ19
 
Turing machine
Turing machineTuring machine
Turing machine
MuhammadSamranTanvee
 
Hashing
HashingHashing
Hashing
LavanyaJ28
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
Rajendran
 
Hashing data
Hashing dataHashing data
Hashing data
umair khan
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
rajshreemuthiah
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1Kumar
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
Lemia Algmri
 
E-R diagram & SQL
E-R diagram & SQLE-R diagram & SQL
E-R diagram & SQL
Abdullah Almasud
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back trackingTech_MX
 

What's hot (20)

Hash table
Hash tableHash table
Hash table
 
Row major and column major in 2 d
Row major and column major in 2 dRow major and column major in 2 d
Row major and column major in 2 d
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
 
Python dictionary
Python dictionaryPython dictionary
Python dictionary
 
Counting sort
Counting sortCounting sort
Counting sort
 
Java Linked List Tutorial | Edureka
Java Linked List Tutorial |  EdurekaJava Linked List Tutorial |  Edureka
Java Linked List Tutorial | Edureka
 
van Emde Boas trees
van Emde Boas treesvan Emde Boas trees
van Emde Boas trees
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Heap tree
Heap treeHeap tree
Heap tree
 
Turing machine
Turing machineTuring machine
Turing machine
 
Hashing
HashingHashing
Hashing
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Heap
HeapHeap
Heap
 
Hashing data
Hashing dataHashing data
Hashing data
 
Hashing
HashingHashing
Hashing
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
E-R diagram & SQL
E-R diagram & SQLE-R diagram & SQL
E-R diagram & SQL
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 

Similar to Hashing Part Two: Static Perfect Hashing

Hashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo HashingHashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo Hashing
Benjamin Sach
 
Hashing Part One
Hashing Part OneHashing Part One
Hashing Part One
Benjamin Sach
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7chidabdu
 
Lec5
Lec5Lec5
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
Andres Mendez-Vazquez
 
13-hashing.ppt
13-hashing.ppt13-hashing.ppt
13-hashing.ppt
soniya555961
 
Skiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sortingSkiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sortingzukun
 
Randamization.pdf
Randamization.pdfRandamization.pdf
Randamization.pdf
Prashanth460337
 
Matrices ii
Matrices iiMatrices ii
Matrices ii
Solo Hermelin
 
Hash presentation
Hash presentationHash presentation
Hash presentation
omercode
 
Hashing using a different methods of technic
Hashing using a different methods of technicHashing using a different methods of technic
Hashing using a different methods of technic
lokaprasaadvs
 
Largedictionaries handout
Largedictionaries handoutLargedictionaries handout
Largedictionaries handoutcsedays
 
Iteration Techniques
Iteration TechniquesIteration Techniques
Iteration Techniques
Andrada Astefanoaie
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec IISajid Marwat
 
Hashing
HashingHashing
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set
Intro C# Book
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - Hashing
Sam Light
 
Hashing
HashingHashing
Hashing
debolina13
 

Similar to Hashing Part Two: Static Perfect Hashing (20)

Hashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo HashingHashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo Hashing
 
Hashing Part One
Hashing Part OneHashing Part One
Hashing Part One
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
 
Lec5
Lec5Lec5
Lec5
 
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
 
13-hashing.ppt
13-hashing.ppt13-hashing.ppt
13-hashing.ppt
 
Skiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sortingSkiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sorting
 
Lec5
Lec5Lec5
Lec5
 
Randamization.pdf
Randamization.pdfRandamization.pdf
Randamization.pdf
 
Matrices ii
Matrices iiMatrices ii
Matrices ii
 
Hashing
HashingHashing
Hashing
 
Hash presentation
Hash presentationHash presentation
Hash presentation
 
Hashing using a different methods of technic
Hashing using a different methods of technicHashing using a different methods of technic
Hashing using a different methods of technic
 
Largedictionaries handout
Largedictionaries handoutLargedictionaries handout
Largedictionaries handout
 
Iteration Techniques
Iteration TechniquesIteration Techniques
Iteration Techniques
 
Advance algorithm hashing lec II
Advance algorithm hashing lec IIAdvance algorithm hashing lec II
Advance algorithm hashing lec II
 
Hashing
HashingHashing
Hashing
 
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set
 
Analysis Of Algorithms - Hashing
Analysis Of Algorithms - HashingAnalysis Of Algorithms - Hashing
Analysis Of Algorithms - Hashing
 
Hashing
HashingHashing
Hashing
 

More from Benjamin Sach

Approximation Algorithms Part Four: APTAS
Approximation Algorithms Part Four: APTASApproximation Algorithms Part Four: APTAS
Approximation Algorithms Part Four: APTAS
Benjamin Sach
 
Approximation Algorithms Part Three: (F)PTAS
Approximation Algorithms Part Three: (F)PTASApproximation Algorithms Part Three: (F)PTAS
Approximation Algorithms Part Three: (F)PTAS
Benjamin Sach
 
Approximation Algorithms Part Two: More Constant factor approximations
Approximation Algorithms Part Two: More Constant factor approximationsApproximation Algorithms Part Two: More Constant factor approximations
Approximation Algorithms Part Two: More Constant factor approximations
Benjamin Sach
 
Approximation Algorithms Part One: Constant factor approximations
Approximation Algorithms Part One: Constant factor approximationsApproximation Algorithms Part One: Constant factor approximations
Approximation Algorithms Part One: Constant factor approximations
Benjamin Sach
 
Orthogonal Range Searching
Orthogonal Range SearchingOrthogonal Range Searching
Orthogonal Range Searching
Benjamin Sach
 
Pattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatchesPattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatches
Benjamin Sach
 
Pattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming DistancePattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming Distance
Benjamin Sach
 
Lowest Common Ancestor
Lowest Common AncestorLowest Common Ancestor
Lowest Common Ancestor
Benjamin Sach
 
Range Minimum Queries
Range Minimum QueriesRange Minimum Queries
Range Minimum Queries
Benjamin Sach
 
Pattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix ArraysPattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix Arrays
Benjamin Sach
 
Pattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix TreesPattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix Trees
Benjamin Sach
 
Probability Recap
Probability RecapProbability Recap
Probability Recap
Benjamin Sach
 
Bloom Filters
Bloom FiltersBloom Filters
Bloom Filters
Benjamin Sach
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
Benjamin Sach
 
Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)
Benjamin Sach
 
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's AlgorithmShortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Benjamin Sach
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First Search
Benjamin Sach
 
Shortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairsShortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairs
Benjamin Sach
 
Line Segment Intersections
Line Segment IntersectionsLine Segment Intersections
Line Segment Intersections
Benjamin Sach
 
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip ListsSelf-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Benjamin Sach
 

More from Benjamin Sach (20)

Approximation Algorithms Part Four: APTAS
Approximation Algorithms Part Four: APTASApproximation Algorithms Part Four: APTAS
Approximation Algorithms Part Four: APTAS
 
Approximation Algorithms Part Three: (F)PTAS
Approximation Algorithms Part Three: (F)PTASApproximation Algorithms Part Three: (F)PTAS
Approximation Algorithms Part Three: (F)PTAS
 
Approximation Algorithms Part Two: More Constant factor approximations
Approximation Algorithms Part Two: More Constant factor approximationsApproximation Algorithms Part Two: More Constant factor approximations
Approximation Algorithms Part Two: More Constant factor approximations
 
Approximation Algorithms Part One: Constant factor approximations
Approximation Algorithms Part One: Constant factor approximationsApproximation Algorithms Part One: Constant factor approximations
Approximation Algorithms Part One: Constant factor approximations
 
Orthogonal Range Searching
Orthogonal Range SearchingOrthogonal Range Searching
Orthogonal Range Searching
 
Pattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatchesPattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatches
 
Pattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming DistancePattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming Distance
 
Lowest Common Ancestor
Lowest Common AncestorLowest Common Ancestor
Lowest Common Ancestor
 
Range Minimum Queries
Range Minimum QueriesRange Minimum Queries
Range Minimum Queries
 
Pattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix ArraysPattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix Arrays
 
Pattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix TreesPattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix Trees
 
Probability Recap
Probability RecapProbability Recap
Probability Recap
 
Bloom Filters
Bloom FiltersBloom Filters
Bloom Filters
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)
 
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's AlgorithmShortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First Search
 
Shortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairsShortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairs
 
Line Segment Intersections
Line Segment IntersectionsLine Segment Intersections
Line Segment Intersections
 
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip ListsSelf-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
 

Recently uploaded

Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 

Recently uploaded (20)

Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 

Hashing Part Two: Static Perfect Hashing

  • 1. Advanced Algorithms – COMS31900 Hashing part two Static Perfect Hashing Benjamin Sach
  • 2. Dictionaries and Hashing recap A dynamic dictionary stores (key, value)-pairs and supports: Universe U of u keys. Hash table T of size m n. Collisions were fixed by chaining A hash function maps a key x to position h(x) - i.e T[h(x)] = (key, value). n arbitrary operations arrive online, one at a time. add(key, value), lookup(key) (which returns value) and delete(key) (building linked lists)
  • 3. Dictionaries and Hashing recap A dynamic dictionary stores (key, value)-pairs and supports: Universe U of u keys. Hash table T of size m n. Collisions were fixed by chaining A hash function maps a key x to position h(x) - i.e T[h(x)] = (key, value). A set H of hash functions is weakly universal if for any two keys x, y ∈ U (with x = y), Pr h(x) = h(y) 1 m (h is picked uniformly at random from H) n arbitrary operations arrive online, one at a time. add(key, value), lookup(key) (which returns value) and delete(key) (building linked lists)
  • 4. Dictionaries and Hashing recap A dynamic dictionary stores (key, value)-pairs and supports: Universe U of u keys. Hash table T of size m n. Collisions were fixed by chaining A hash function maps a key x to position h(x) - i.e T[h(x)] = (key, value). A set H of hash functions is weakly universal if for any two keys x, y ∈ U (with x = y), Pr h(x) = h(y) 1 m (h is picked uniformly at random from H) For any n operations, the expected run-time is O(1) per operation. Using weakly universal hashing: n arbitrary operations arrive online, one at a time. add(key, value), lookup(key) (which returns value) and delete(key) (building linked lists)
  • 5. Dictionaries and Hashing recap A dynamic dictionary stores (key, value)-pairs and supports: Universe U of u keys. Hash table T of size m n. Collisions were fixed by chaining A hash function maps a key x to position h(x) - i.e T[h(x)] = (key, value). A set H of hash functions is weakly universal if for any two keys x, y ∈ U (with x = y), Pr h(x) = h(y) 1 m (h is picked uniformly at random from H) For any n operations, the expected run-time is O(1) per operation. But this doesn’t tell us much about the worst-case behaviour Using weakly universal hashing: n arbitrary operations arrive online, one at a time. add(key, value), lookup(key) (which returns value) and delete(key) (building linked lists)
  • 6. Static Dictionaries and Perfect hashing A static dictionary stores (key, value)-pairs and supports: Hash table T of size m n. A hash function maps a key x to position h(x) - i.e T[h(x)] = (key, value). we are given n different (key, value)-pairs and want to pick a good h lookup(key) (which returns value) - no inserts or deletes are allowed Universe U of u keys. Collisions were fixed by chaining (building linked lists)
  • 7. Static Dictionaries and Perfect hashing A static dictionary stores (key, value)-pairs and supports: Hash table T of size m n. A hash function maps a key x to position h(x) - i.e T[h(x)] = (key, value). we are given n different (key, value)-pairs and want to pick a good h lookup(key) (which returns value) - no inserts or deletes are allowed THEOREM The FKS hashing scheme: • Has no collisions • Every lookup takes O(1) worst-case time, • Uses O(n) space, • Can be built in O(n) expected time. Universe U of u keys. Collisions were fixed by chaining (building linked lists)
  • 8. Static Dictionaries and Perfect hashing A static dictionary stores (key, value)-pairs and supports: Hash table T of size m n. A hash function maps a key x to position h(x) - i.e T[h(x)] = (key, value). we are given n different (key, value)-pairs and want to pick a good h lookup(key) (which returns value) - no inserts or deletes are allowed THEOREM The FKS hashing scheme: • Has no collisions • Every lookup takes O(1) worst-case time, • Uses O(n) space, • Can be built in O(n) expected time. The rest of this lecture is devoted to the FKS scheme Universe U of u keys. Collisions were fixed by chaining (building linked lists)
  • 9. Static Dictionaries and Perfect hashing A static dictionary stores (key, value)-pairs and supports: Hash table T of size m n. A hash function maps a key x to position h(x) - i.e T[h(x)] = (key, value). we are given n different (key, value)-pairs and want to pick a good h lookup(key) (which returns value) - no inserts or deletes are allowed THEOREM The FKS hashing scheme: • Has no collisions • Every lookup takes O(1) worst-case time, • Uses O(n) space, • Can be built in O(n) expected time. The rest of this lecture is devoted to the FKS scheme The construction is based on weak universal hashing Universe U of u keys. Collisions were fixed by chaining (building linked lists)
  • 10. Static Dictionaries and Perfect hashing A static dictionary stores (key, value)-pairs and supports: Hash table T of size m n. A hash function maps a key x to position h(x) - i.e T[h(x)] = (key, value). we are given n different (key, value)-pairs and want to pick a good h lookup(key) (which returns value) - no inserts or deletes are allowed THEOREM The FKS hashing scheme: • Has no collisions • Every lookup takes O(1) worst-case time, • Uses O(n) space, • Can be built in O(n) expected time. The rest of this lecture is devoted to the FKS scheme The construction is based on weak universal hashing (with an O(1) time hash function) Universe U of u keys. Collisions were fixed by chaining (building linked lists)
  • 11. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H
  • 12. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function n
  • 13. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function n (where any h(x) can be computed in O(1) time)
  • 14. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function n
  • 15. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function Step 2: Check for collisions n
  • 16. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function Step 2: Check for collisions Step 3: Profit! n
  • 17. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary n
  • 18. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary How many collisions do we get on average? n
  • 19. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary How many collisions do we get on average? where indicator random variable Ix,y = 1 iff h(x) = h(y). number of collisions n E(C) = E x,y∈T,x<y Ix,y = x,y∈T, x<y E(Ix,y) x,y∈T, x<y 1 m = n 2 · 1 m
  • 20. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary How many collisions do we get on average? where indicator random variable Ix,y = 1 iff h(x) = h(y). number of collisions n E(C) = E x,y∈T,x<y Ix,y = x,y∈T, x<y E(Ix,y) x,y∈T, x<y 1 m = n 2 · 1 m
  • 21. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary How many collisions do we get on average? where indicator random variable Ix,y = 1 iff h(x) = h(y). number of collisions n Linearity of Expectation Let Y1, Y2, . . . , Yk be k random variables. Then E k i=1 Yi = k i=1 E(Yi) E(C) = E x,y∈T,x<y Ix,y = x,y∈T, x<y E(Ix,y) x,y∈T, x<y 1 m = n 2 · 1 m
  • 22. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary How many collisions do we get on average? where indicator random variable Ix,y = 1 iff h(x) = h(y). number of collisions linearity of expectation n E(C) = E x,y∈T,x<y Ix,y = x,y∈T, x<y E(Ix,y) x,y∈T, x<y 1 m = n 2 · 1 m
  • 23. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary How many collisions do we get on average? where indicator random variable Ix,y = 1 iff h(x) = h(y). number of collisions linearity of expectation n E(C) = E x,y∈T,x<y Ix,y = x,y∈T, x<y E(Ix,y) x,y∈T, x<y 1 m = n 2 · 1 m
  • 24. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary How many collisions do we get on average? where indicator random variable Ix,y = 1 iff h(x) = h(y). number of collisions linearity of expectation definition of expectation n E(C) = E x,y∈T,x<y Ix,y = x,y∈T, x<y E(Ix,y) x,y∈T, x<y 1 m = n 2 · 1 m
  • 25. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary How many collisions do we get on average? where indicator random variable Ix,y = 1 iff h(x) = h(y). number of collisions linearity of expectation E(Ix,y) = 1 · Pr(Ix,y = 1) + 0 · Pr(Ix,y = 0) 1 m n E(C) = E x,y∈T,x<y Ix,y = x,y∈T, x<y E(Ix,y) x,y∈T, x<y 1 m = n 2 · 1 m By the definition of expectation. . .
  • 26. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary How many collisions do we get on average? where indicator random variable Ix,y = 1 iff h(x) = h(y). number of collisions linearity of expectation definition of expectation n E(C) = E x,y∈T,x<y Ix,y = x,y∈T, x<y E(Ix,y) x,y∈T, x<y 1 m = n 2 · 1 m
  • 27. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary How many collisions do we get on average? where indicator random variable Ix,y = 1 iff h(x) = h(y). number of collisions linearity of expectation definition of expectation n E(C) = E x,y∈T,x<y Ix,y = x,y∈T, x<y E(Ix,y) x,y∈T, x<y 1 m = n 2 · 1 m
  • 28. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary How many collisions do we get on average? where indicator random variable Ix,y = 1 iff h(x) = h(y). number of collisions linearity of expectation definition of expectation n 2 = n(n − 1) 2 n E(C) = E x,y∈T,x<y Ix,y = x,y∈T, x<y E(Ix,y) x,y∈T, x<y 1 m = n 2 · 1 m
  • 29. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary How many collisions do we get on average? where indicator random variable Ix,y = 1 iff h(x) = h(y). number of collisions linearity of expectation definition of expectation n2/2 n E(C) = E x,y∈T,x<y Ix,y = x,y∈T, x<y E(Ix,y) x,y∈T, x<y 1 m = n 2 · 1 m
  • 30. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary How many collisions do we get on average? where indicator random variable Ix,y = 1 iff h(x) = h(y). number of collisions linearity of expectation definition of expectation n2/2 n E(C) = E x,y∈T,x<y Ix,y = x,y∈T, x<y E(Ix,y) x,y∈T, x<y 1 m = n 2 · 1 m n2 2m
  • 31. Perfect hashing - a first attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H Step 1: Insert everything into a hash table of size m = n using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary How many collisions do we get on average? where indicator random variable Ix,y = 1 iff h(x) = h(y). number of collisions linearity of expectation definition of expectation n2/2 n E(C) = E x,y∈T,x<y Ix,y = x,y∈T, x<y E(Ix,y) x,y∈T, x<y 1 m = n 2 · 1 m n2 2m n 2 .
  • 32. Perfect hashing - a second attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary n2 Step 1: Insert everything into a hash table of size m = n2
  • 33. Perfect hashing - a second attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary n2 Step 1: Insert everything into a hash table of size m = n2 How many collisions do we get on average?
  • 34. Perfect hashing - a second attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary n2 Step 1: Insert everything into a hash table of size m = n2 How many collisions do we get on average? where indicator random variable Ix,y = 1 iff h(x) = h(y). number of collisions linearity of expectation definition of expectation n2/2 E(C) = E x,y∈T,x<y Ix,y = x,y∈T, x<y E(Ix,y) x,y∈T, x<y 1 m = n 2 · 1 m n2 2m
  • 35. Perfect hashing - a second attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary n2 Step 1: Insert everything into a hash table of size m = n2 How many collisions do we get on average? where indicator random variable Ix,y = 1 iff h(x) = h(y). number of collisions linearity of expectation definition of expectation n2/2 E(C) = E x,y∈T,x<y Ix,y = x,y∈T, x<y E(Ix,y) x,y∈T, x<y 1 m = n 2 · 1 m n2 2m 1 2
  • 36. Perfect hashing - a second attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary n2 Step 1: Insert everything into a hash table of size m = n2 How many collisions do we get on average? where indicator random variable Ix,y = 1 iff h(x) = h(y). number of collisions linearity of expectation definition of expectation n2/2 E(C) = E x,y∈T,x<y Ix,y = x,y∈T, x<y E(Ix,y) x,y∈T, x<y 1 m = n 2 · 1 m n2 2m 1 2 much better!
  • 37. Perfect hashing - a second attempt A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x = y), Pr h(x) = h(y) 1 m where h is picked uniformly at random from H using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if necessary n2 Step 1: Insert everything into a hash table of size m = n2 How many collisions do we get on average? where indicator random variable Ix,y = 1 iff h(x) = h(y). number of collisions linearity of expectation definition of expectation n2/2 E(C) = E x,y∈T,x<y Ix,y = x,y∈T, x<y E(Ix,y) x,y∈T, x<y 1 m = n 2 · 1 m n2 2m 1 2 much (except we cheated) better!
  • 38. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there was a collision Step 1: Insert everything into a hash table of size m = n2
  • 39. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there was a collision Step 1: Insert everything into a hash table of size m = n2 How many times do we repeat on average?
  • 40. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there was a collision Step 1: Insert everything into a hash table of size m = n2 How many times do we repeat on average? The expected number of collisions: E(C) 1 2 The probability of at least one collision: Pr(C 1) 1 2
  • 41. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there was a collision Step 1: Insert everything into a hash table of size m = n2 How many times do we repeat on average? The expected number of collisions: E(C) 1 2 The probability of at least one collision: Pr(C 1) 1 2 Markov’s inequality If X is a non-negative r.v., then for all a > 0, Pr(X a) E(X) a .
  • 42. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there was a collision Step 1: Insert everything into a hash table of size m = n2 How many times do we repeat on average? The expected number of collisions: E(C) 1 2 The probability of at least one collision: Pr(C 1) 1 2 Markov’s inequality
  • 43. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there was a collision Step 1: Insert everything into a hash table of size m = n2 How many times do we repeat on average? The expected number of collisions: E(C) 1 2 The probability of at least one collision: Pr(C 1) 1 2 The probability of zero collisions is at least 1 2 Markov’s inequality
  • 44. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there was a collision Step 1: Insert everything into a hash table of size m = n2 How many times do we repeat on average? The expected number of collisions: E(C) 1 2 The probability of at least one collision: Pr(C 1) 1 2 The probability of zero collisions is at least 1 2 i.e. at least as good as tossing a heads on a fair coin Markov’s inequality
  • 45. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there was a collision Step 1: Insert everything into a hash table of size m = n2 How many times do we repeat on average? The expected number of collisions: E(C) 1 2 E(runs) E(coin tosses to get a heads) = 2 The probability of at least one collision: Pr(C 1) 1 2 The probability of zero collisions is at least 1 2 i.e. at least as good as tossing a heads on a fair coin Markov’s inequality
  • 46. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there was a collision Step 1: Insert everything into a hash table of size m = n2 How many times do we repeat on average? The expected number of collisions: E(C) 1 2 E(runs) E(coin tosses to get a heads) = 2 The probability of at least one collision: Pr(C 1) 1 2 The probability of zero collisions is at least 1 2 i.e. at least as good as tossing a heads on a fair coin E(construction time) = O(m)·E(runs) = O(m) = O(n2) Markov’s inequality
  • 47. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there was a collision Step 1: Insert everything into a hash table of size m = n2 How many times do we repeat on average? The expected number of collisions: E(C) 1 2 E(runs) E(coin tosses to get a heads) = 2 The probability of at least one collision: Pr(C 1) 1 2 The probability of zero collisions is at least 1 2 i.e. at least as good as tossing a heads on a fair coin E(construction time) = O(m)·E(runs) = O(m) = O(n2) . . . and then the look-up time is always O(1) Markov’s inequality
  • 48. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there was a collision Step 1: Insert everything into a hash table of size m = n2 How many times do we repeat on average? The expected number of collisions: E(C) 1 2 E(runs) E(coin tosses to get a heads) = 2 The probability of at least one collision: Pr(C 1) 1 2 The probability of zero collisions is at least 1 2 i.e. at least as good as tossing a heads on a fair coin E(construction time) = O(m)·E(runs) = O(m) = O(n2) . . . and then the look-up time is always O(1) (because any h(x) can be computed in O(1) time) Markov’s inequality
  • 49. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there are more than n collisions Step 1: Insert everything into a hash table of size m = n
  • 50. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there are more than n collisions Step 1: Insert everything into a hash table of size m = n This looks rubbish but it will be useful in a bit!
  • 51. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there are more than n collisions Step 1: Insert everything into a hash table of size m = n How many times do we repeat on average? The expected number of collisions: E(C) n 2 The probability of at least n collisions: Pr(C n) 1 2 This looks rubbish but it will be useful in a bit!
  • 52. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there are more than n collisions Step 1: Insert everything into a hash table of size m = n How many times do we repeat on average? The expected number of collisions: E(C) n 2 The probability of at least n collisions: Pr(C n) 1 2 This looks rubbish but it will be useful in a bit! (where a = n) Markov’s inequality If X is a non-negative r.v., then for all a > 0, Pr(X a) E(X) a .
  • 53. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there are more than n collisions Step 1: Insert everything into a hash table of size m = n How many times do we repeat on average? The expected number of collisions: E(C) n 2 The probability of at least n collisions: Pr(C n) 1 2 This looks rubbish but it will be useful in a bit!
  • 54. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there are more than n collisions Step 1: Insert everything into a hash table of size m = n How many times do we repeat on average? The expected number of collisions: E(C) n 2 E(runs) E(coin tosses to get a heads) = 2 The probability of at least n collisions: Pr(C n) 1 2 i.e. at least as good as tossing a heads on a fair coin E(construction time) = O(m)·E(runs) = O(m) = O(n) This looks rubbish but it will be useful in a bit! The probability of at most n collisions is at least 1 2
  • 55. Expected construction time using a weakly universal hash function Step 2: Check for collisions Step 3: Repeat if there are more than n collisions Step 1: Insert everything into a hash table of size m = n How many times do we repeat on average? The expected number of collisions: E(C) n 2 E(runs) E(coin tosses to get a heads) = 2 The probability of at least n collisions: Pr(C n) 1 2 i.e. at least as good as tossing a heads on a fair coin E(construction time) = O(m)·E(runs) = O(m) = O(n) . . . but the look-up time could be rubbish (lots of collisions) This looks rubbish but it will be useful in a bit! The probability of at most n collisions is at least 1 2
  • 56. Perfect hashing - attempt three n Step 1: Insert everything into a hash table, T, of size n using a weakly universal hash function, h T
  • 57. Perfect hashing - attempt three n Step 1: Insert everything into a hash table, T, of size n using a weakly universal hash function, h Let ni be the number of items in T[i] T
  • 58. Perfect hashing - attempt three n Step 1: Insert everything into a hash table, T, of size n using a weakly universal hash function, h Let ni be the number of items in T[i] T n1 = 2 n5 = 2 n8 = 3
  • 59. Perfect hashing - attempt three n Step 1: Insert everything into a hash table, T, of size n using a weakly universal hash function, h . . . but don’t use chaining Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using another weakly universal hash function Let ni be the number of items in T[i] T denoted hi (there is one for each i) n2 i
  • 60. Perfect hashing - attempt three n Step 1: Insert everything into a hash table, T, of size n using a weakly universal hash function, h . . . but don’t use chaining Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using another weakly universal hash function Let ni be the number of items in T[i] T denoted hi (there is one for each i) n2 i
  • 61. Perfect hashing - attempt three n Step 1: Insert everything into a hash table, T, of size n using a weakly universal hash function, h . . . but don’t use chaining Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using another weakly universal hash function Let ni be the number of items in T[i] T denoted hi (there is one for each i) n2 i (Step 3) Immediately repeat a step if either a) T has more than n collisions b) some Ti has a collision
  • 62. Perfect hashing - attempt three n Step 1: Insert everything into a hash table, T, of size n using a weakly universal hash function, h . . . but don’t use chaining Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using another weakly universal hash function Let ni be the number of items in T[i] T denoted hi (there is one for each i) n2 i (Step 3) Immediately repeat a step if either a) T has more than n collisions b) some Ti has a collision i.e. check (and if necessary rebuild) each table immediately after building it
  • 63. Perfect hashing - attempt three n Step 1: Insert everything into a hash table, T, of size n using a weakly universal hash function, h . . . but don’t use chaining Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using another weakly universal hash function Let ni be the number of items in T[i] T denoted hi (there is one for each i) n2 i (Step 3) Immediately repeat a step if either a) T has more than n collisions b) some Ti has a collision
  • 64. Perfect hashing - attempt three n Step 1: Insert everything into a hash table, T, of size n using a weakly universal hash function, h . . . but don’t use chaining Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using another weakly universal hash function Let ni be the number of items in T[i] T denoted hi (there is one for each i) n2 i (Step 3) Immediately repeat a step if either a) T has more than n collisions The look-up time is always O(1) 1. Compute i = h(x) (x is the key) 2. Compute j = hi(x) 3. The item is in Ti[j] b) some Ti has a collision
  • 65. Perfect hashing - attempt three n Step 1: Insert everything into a hash table, T, of size n using a weakly universal hash function, h . . . but don’t use chaining Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using another weakly universal hash function Let ni be the number of items in T[i] T denoted hi (there is one for each i) n2 i (Step 3) Immediately repeat a step if either a) T has more than n collisions What is the expected construction time? What is the space usage? The look-up time is always O(1) 1. Compute i = h(x) (x is the key) 2. Compute j = hi(x) 3. The item is in Ti[j] Two questions remain: b) some Ti has a collision
  • 66. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti
  • 67. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n)
  • 68. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n) The size of Ti is O(ni 2)
  • 69. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n) The size of Ti is O(ni 2) Storing hi uses O(1) space
  • 70. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n) The size of Ti is O(ni 2) So the total space is. . . Storing hi uses O(1) space
  • 71. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n) The size of Ti is O(ni 2) So the total space is. . . O(n)+ i O(n2 i ) = O(n)+O   i n2 i   Storing hi uses O(1) space
  • 72. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n) The size of Ti is O(ni 2) So the total space is. . . Storing hi uses O(1) space O(n)+ i O(n2 i ) = O(n)+O   i n2 i  
  • 73. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n) The size of Ti is O(ni 2) So the total space is. . . Storing hi uses O(1) space how big is this? O(n)+ i O(n2 i ) = O(n)+O   i n2 i  
  • 74. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n) The size of Ti is O(ni 2) So the total space is. . . Storing hi uses O(1) space how big is this? O(n)+ i O(n2 i ) = O(n)+O   i n2 i   How big is i n2 i ?
  • 75. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n) The size of Ti is O(ni 2) So the total space is. . . Storing hi uses O(1) space how big is this? O(n)+ i O(n2 i ) = O(n)+O   i n2 i   How big is i n2 i ? There are ni 2 collisions in T[i]
  • 76. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n) The size of Ti is O(ni 2) So the total space is. . . Storing hi uses O(1) space how big is this? O(n)+ i O(n2 i ) = O(n)+O   i n2 i   How big is i n2 i ? There are ni 2 collisions in T[i] so there are i ni 2 collisions in T
  • 77. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n) The size of Ti is O(ni 2) So the total space is. . . Storing hi uses O(1) space how big is this? O(n)+ i O(n2 i ) = O(n)+O   i n2 i   How big is i n2 i ? There are ni 2 collisions in T[i] so there are i ni 2 collisions in T but we know that there are at most n collisions in T . . .
  • 78. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n) The size of Ti is O(ni 2) So the total space is. . . Storing hi uses O(1) space how big is this? O(n)+ i O(n2 i ) = O(n)+O   i n2 i   How big is i n2 i ? There are ni 2 collisions in T[i] so there are i ni 2 collisions in T but we know that there are at most n collisions in T . . . i n2 i 4 i ni 2 n
  • 79. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n) The size of Ti is O(ni 2) So the total space is. . . Storing hi uses O(1) space how big is this? O(n)+ i O(n2 i ) = O(n)+O   i n2 i   How big is i n2 i ? There are ni 2 collisions in T[i] so there are i ni 2 collisions in T but we know that there are at most n collisions in T . . . i n2 i 4 i ni 2 n
  • 80. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n) The size of Ti is O(ni 2) So the total space is. . . Storing hi uses O(1) space how big is this? O(n)+ i O(n2 i ) = O(n)+O   i n2 i   How big is i n2 i ? There are ni 2 collisions in T[i] so there are i ni 2 collisions in T but we know that there are at most n collisions in T . . . ni 2 = ni(ni−1) 2 n2 i 4 i n2 i 4 i ni 2 n
  • 81. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n) The size of Ti is O(ni 2) So the total space is. . . Storing hi uses O(1) space how big is this? O(n)+ i O(n2 i ) = O(n)+O   i n2 i   How big is i n2 i ? There are ni 2 collisions in T[i] so there are i ni 2 collisions in T but we know that there are at most n collisions in T . . . i n2 i 4 i ni 2 n
  • 82. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n) The size of Ti is O(ni 2) So the total space is. . . Storing hi uses O(1) space how big is this? O(n)+ i O(n2 i ) = O(n)+O   i n2 i   How big is i n2 i ? There are ni 2 collisions in T[i] so there are i ni 2 collisions in T but we know that there are at most n collisions in T . . . i n2 i 4 i ni 2 n i n2 i 4nor
  • 83. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n) The size of Ti is O(ni 2) So the total space is. . . Storing hi uses O(1) space how big is this? How big is i n2 i ? There are ni 2 collisions in T[i] so there are i ni 2 collisions in T but we know that there are at most n collisions in T . . . i n2 i 4 i ni 2 n i n2 i 4nor O(n)+ i O(n2 i ) = O(n)+O   i n2 i   = O(n)
  • 84. Perfect Hashing - Space usage n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi How much space does this use? (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The size of T is O(n) The size of Ti is O(ni 2) So the total space is. . . Storing hi uses O(1) space O(n)+ i O(n2 i ) = O(n)+O   i n2 i   = O(n)
  • 85. Perfect Hashing - Expected construction time n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti
  • 86. Perfect Hashing - Expected construction time n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The expected construction time for T is O(n) (we considered this on a previous slide)
  • 87. Perfect Hashing - Expected construction time n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The expected construction time for T is O(n) (we considered this on a previous slide) The expected construction time for each Ti is O(ni 2)
  • 88. Perfect Hashing - Expected construction time n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The expected construction time for T is O(n) (we considered this on a previous slide) The expected construction time for each Ti is O(ni 2) - we insert ni items into a table of size m = n2 i - then repeat if there was a collision
  • 89. Perfect Hashing - Expected construction time n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The expected construction time for T is O(n) (we considered this on a previous slide) The expected construction time for each Ti is O(ni 2) - we insert ni items into a table of size m = n2 i (we also considered this on a previous slide) - then repeat if there was a collision
  • 90. Perfect Hashing - Expected construction time n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The expected construction time for T is O(n) (we considered this on a previous slide) The expected construction time for each Ti is O(ni 2) - we insert ni items into a table of size m = n2 i (we also considered this on a previous slide) - then repeat if there was a collision The overall expected constuction time is therefore: E(construction time) = E  construction time of T + i construction time of Ti  
  • 91. Perfect Hashing - Expected construction time n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The expected construction time for T is O(n) The expected construction time for each Ti is O(ni 2) The overall expected construction time is therefore: E(construction time) = E  construction time of T + i construction time of Ti  
  • 92. Perfect Hashing - Expected construction time n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The expected construction time for T is O(n) The expected construction time for each Ti is O(ni 2) The overall expected construction time is therefore: E(construction time) = E  construction time of T + i construction time of Ti   = E construction time of T)+ i E(construction time of Ti
  • 93. Perfect Hashing - Expected construction time n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The expected construction time for T is O(n) The expected construction time for each Ti is O(ni 2) The overall expected construction time is therefore: E(construction time) = E  construction time of T + i construction time of Ti   = E construction time of T)+ i E(construction time of Ti = O(n)+ i O(n2 i ) = O(n)+O   i n2 i  
  • 94. Perfect Hashing - Expected construction time n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The expected construction time for T is O(n) The expected construction time for each Ti is O(ni 2) The overall expected construction time is therefore: E(construction time) = E  construction time of T + i construction time of Ti   = E construction time of T)+ i E(construction time of Ti = O(n)+ i O(n2 i ) = O(n)+O   i n2 i   = O(n)
  • 95. Perfect Hashing - Expected construction time n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The expected construction time for T is O(n) The expected construction time for each Ti is O(ni 2) The overall expected construction time is therefore: E(construction time) = E  construction time of T + i construction time of Ti   = E construction time of T)+ i E(construction time of Ti = O(n)+ i O(n2 i ) = O(n)+O   i n2 i   = O(n) i n2 i 4n
  • 96. Perfect Hashing - Expected construction time n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti The expected construction time for T is O(n) The expected construction time for each Ti is O(ni 2) The overall expected construction time is therefore: E(construction time) = E  construction time of T + i construction time of Ti   = E construction time of T)+ i E(construction time of Ti = O(n)+ i O(n2 i ) = O(n)+O   i n2 i   = O(n)
  • 97. Perfect Hashing - Summary n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti THEOREM The FKS hashing scheme: • Has no collisions • Every lookup takes O(1) worst-case time, • Uses O(n) space, • Can be built in O(n) expected time. The look-up time is always O(1) 1. Compute i = h(x) (x is the key) 2. Compute j = hi(x) 3. The item is in Ti[j]
  • 98. Perfect Hashing - Summary n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti THEOREM The FKS hashing scheme: • Has no collisions • Every lookup takes O(1) worst-case time, • Uses O(n) space, • Can be built in O(n) expected time. The look-up time is always O(1) 1. Compute i = h(x) (x is the key) 2. Compute j = hi(x) 3. The item is in Ti[j]
  • 99. Perfect Hashing - Summary n n2 i Step 1: Insert everything into a hash table, T, of size n using a weakly universal (w.u.) hash function, h Step 2: The ni items in T[i] are inserted into another hash table Ti of size n2 i using w.u hash function hi (Step 3) Immediately repeat if either a) T has more than n collisions b) some Ti has a collision T Ti THEOREM The FKS hashing scheme: • Has no collisions • Every lookup takes O(1) worst-case time, • Uses O(n) space, • Can be built in O(n) expected time. The look-up time is always O(1) 1. Compute i = h(x) (x is the key) 2. Compute j = hi(x) 3. The item is in Ti[j]