Hashing Part Two: Static Perfect Hashing

99
Advanced Algorithms – COMS31900 Hashing part two Static Perfect Hashing Benjamin Sach

Transcript of Hashing Part Two: Static Perfect Hashing

Page 1: Hashing Part Two: Static Perfect Hashing

Advanced Algorithms – COMS31900

Hashing part two

Static Perfect Hashing

Benjamin Sach

Page 2: Hashing Part Two: Static Perfect Hashing

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)

Page 3: Hashing Part Two: Static Perfect Hashing

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 6= y),

Pr(h(x) = h(y)

)6

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)

Page 4: Hashing Part Two: Static Perfect Hashing

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 6= y),

Pr(h(x) = h(y)

)6

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)

Page 5: Hashing Part Two: Static Perfect Hashing

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 6= y),

Pr(h(x) = h(y)

)6

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)

Page 6: Hashing Part Two: Static Perfect Hashing

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)

Page 7: Hashing Part Two: Static Perfect Hashing

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)

Page 8: Hashing Part Two: Static Perfect Hashing

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)

Page 9: Hashing Part Two: Static Perfect Hashing

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)

Page 10: Hashing Part Two: Static Perfect Hashing

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)

Page 11: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Page 12: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing a weakly universal hash function

n

Page 13: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing a weakly universal hash function

n(where any h(x) can be computed in O(1) time)

Page 14: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing a weakly universal hash function

n

Page 15: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing a weakly universal hash function

Step 2: Check for collisionsn

Page 16: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing a weakly universal hash function

Step 2: Check for collisions

Step 3: Profit!

n

Page 17: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing a weakly universal hash function

Step 2: Check for collisions

Step 3: Repeat if necessary

n

Page 18: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing a weakly universal hash function

Step 2: Check for collisions

Step 3: Repeat if necessary

How many collisions do we get on average?

n

Page 19: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing 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) 6∑

x,y∈T, x<y

1

m=(n2

)·1

m

Page 20: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing 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) 6∑

x,y∈T, x<y

1

m=(n2

)·1

m

Page 21: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing 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) 6∑

x,y∈T, x<y

1

m=(n2

)·1

m

Page 22: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing 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) 6∑

x,y∈T, x<y

1

m=(n2

)·1

m

Page 23: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing 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) 6∑

x,y∈T, x<y

1

m=(n2

)·1

m

Page 24: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing 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) 6∑

x,y∈T, x<y

1

m=(n2

)·1

m

Page 25: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing 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) 61

m

n

E(C) = E( ∑

x,y∈T,x<y

Ix,y)=

∑x,y∈T, x<y

E(Ix,y) 6∑

x,y∈T, x<y

1

m=(n2

)·1

m

By the definition of expectation. . .

Page 26: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing 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) 6∑

x,y∈T, x<y

1

m=(n2

)·1

m

Page 27: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing 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) 6∑

x,y∈T, x<y

1

m=(n2

)·1

m

Page 28: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing 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

)=

n(n− 1)

2

n

E(C) = E( ∑

x,y∈T,x<y

Ix,y)=

∑x,y∈T, x<y

E(Ix,y) 6∑

x,y∈T, x<y

1

m=(n2

)·1

m

Page 29: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing 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 6 n2/2

n

E(C) = E( ∑

x,y∈T,x<y

Ix,y)=

∑x,y∈T, x<y

E(Ix,y) 6∑

x,y∈T, x<y

1

m=(n2

)·1

m

Page 30: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing 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 6 n2/2

n

E(C) = E( ∑

x,y∈T,x<y

Ix,y)=

∑x,y∈T, x<y

E(Ix,y) 6∑

x,y∈T, x<y

1

m=(n2

)·1

m6

n2

2m

Page 31: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a first attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere h is picked uniformly at random from H

Step 1: Insert everything into a hash table of size m = nusing 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 6 n2/2

n

E(C) = E( ∑

x,y∈T,x<y

Ix,y)=

∑x,y∈T, x<y

E(Ix,y) 6∑

x,y∈T, x<y

1

m=(n2

)·1

m6

n2

2m6

n

2.

Page 32: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a second attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere 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

Page 33: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a second attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere 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?

Page 34: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a second attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere 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 6 n2/2

E(C) = E( ∑

x,y∈T,x<y

Ix,y)=

∑x,y∈T, x<y

E(Ix,y) 6∑

x,y∈T, x<y

1

m=(n2

)·1

m6

n2

2m

Page 35: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a second attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere 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 6 n2/2

E(C) = E( ∑

x,y∈T,x<y

Ix,y)=

∑x,y∈T, x<y

E(Ix,y) 6∑

x,y∈T, x<y

1

m=(n2

)·1

m6

n2

2m6

1

2

Page 36: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a second attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere 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 6 n2/2

E(C) = E( ∑

x,y∈T,x<y

Ix,y)=

∑x,y∈T, x<y

E(Ix,y) 6∑

x,y∈T, x<y

1

m=(n2

)·1

m6

n2

2m6

1

2

muchbetter!

Page 37: Hashing Part Two: Static Perfect Hashing

Perfect hashing - a second attempt

A set H of hash functions is weakly universal if for any two keys x, y ∈ U (x 6= y),

Pr(h(x) = h(y)

)6

1

mwhere 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 6 n2/2

E(C) = E( ∑

x,y∈T,x<y

Ix,y)=

∑x,y∈T, x<y

E(Ix,y) 6∑

x,y∈T, x<y

1

m=(n2

)·1

m6

n2

2m6

1

2

much

(except we cheated)

better!

Page 38: Hashing Part Two: Static Perfect Hashing

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

Page 39: Hashing Part Two: Static Perfect Hashing

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?

Page 40: Hashing Part Two: Static Perfect Hashing

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) 6 12

The probability of at least one collision: Pr(C > 1) 6 12

Page 41: Hashing Part Two: Static Perfect Hashing

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) 6 12

The probability of at least one collision: Pr(C > 1) 6 12

Markov’s inequality

If X is a non-negative r.v., then for all a > 0,

Pr(X > a) 6E(X)

a.

Page 42: Hashing Part Two: Static Perfect Hashing

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) 6 12

The probability of at least one collision: Pr(C > 1) 6 12

Markov’s inequality

Page 43: Hashing Part Two: Static Perfect Hashing

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) 6 12

The probability of at least one collision: Pr(C > 1) 6 12

The probability of zero collisions is at least 12

Markov’s inequality

Page 44: Hashing Part Two: Static Perfect Hashing

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) 6 12

The probability of at least one collision: Pr(C > 1) 6 12

The probability of zero collisions is at least 12

i.e. at least as good as tossing a heads on a fair coin

Markov’s inequality

Page 45: Hashing Part Two: Static Perfect Hashing

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) 6 12

E(runs) 6 E(coin tosses to get a heads) = 2

The probability of at least one collision: Pr(C > 1) 6 12

The probability of zero collisions is at least 12

i.e. at least as good as tossing a heads on a fair coin

Markov’s inequality

Page 46: Hashing Part Two: Static Perfect Hashing

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) 6 12

E(runs) 6 E(coin tosses to get a heads) = 2

The probability of at least one collision: Pr(C > 1) 6 12

The probability of zero collisions is at least 12

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

Page 47: Hashing Part Two: Static Perfect Hashing

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) 6 12

E(runs) 6 E(coin tosses to get a heads) = 2

The probability of at least one collision: Pr(C > 1) 6 12

The probability of zero collisions is at least 12

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

Page 48: Hashing Part Two: Static Perfect Hashing

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) 6 12

E(runs) 6 E(coin tosses to get a heads) = 2

The probability of at least one collision: Pr(C > 1) 6 12

The probability of zero collisions is at least 12

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

Page 49: Hashing Part Two: Static Perfect Hashing

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

Page 50: Hashing Part Two: Static Perfect Hashing

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!

Page 51: Hashing Part Two: Static Perfect Hashing

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) 6 n2

The probability of at least n collisions: Pr(C > n) 6 12

This looks rubbish but

it will be useful in a bit!

Page 52: Hashing Part Two: Static Perfect Hashing

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) 6 n2

The probability of at least n collisions: Pr(C > n) 6 12

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) 6E(X)

a.

Page 53: Hashing Part Two: Static Perfect Hashing

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) 6 n2

The probability of at least n collisions: Pr(C > n) 6 12

This looks rubbish but

it will be useful in a bit!

Page 54: Hashing Part Two: Static Perfect Hashing

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) 6 n2

E(runs) 6 E(coin tosses to get a heads) = 2

The probability of at least n collisions: Pr(C > n) 6 12

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 12

Page 55: Hashing Part Two: Static Perfect Hashing

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) 6 n2

E(runs) 6 E(coin tosses to get a heads) = 2

The probability of at least n collisions: Pr(C > n) 6 12

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 12

Page 56: Hashing Part Two: Static Perfect Hashing

Perfect hashing - attempt three

n

Step 1: Insert everything into a hash table, T , of size nusing a weakly universal hash function, h

T

Page 57: Hashing Part Two: Static Perfect Hashing

Perfect hashing - attempt three

n

Step 1: Insert everything into a hash table, T , of size nusing a weakly universal hash function, h

Let ni be the number of items in T [i]

T

Page 58: Hashing Part Two: Static Perfect Hashing

Perfect hashing - attempt three

n

Step 1: Insert everything into a hash table, T , of size nusing a weakly universal hash function, h

Let ni be the number of items in T [i]

T

n1 = 2

n5 = 2

n8 = 3

Page 59: Hashing Part Two: Static Perfect Hashing

Perfect hashing - attempt three

n

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i

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)

n2i

Page 60: Hashing Part Two: Static Perfect Hashing

Perfect hashing - attempt three

n

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i

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)n2i

Page 61: Hashing Part Two: Static Perfect Hashing

Perfect hashing - attempt three

n

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i

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)n2i

(Step 3) Immediately repeat a step if eithera) T has more than n collisionsb) some Ti has a collision

Page 62: Hashing Part Two: Static Perfect Hashing

Perfect hashing - attempt three

n

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i

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)n2i

(Step 3) Immediately repeat a step if eithera) T has more than n collisionsb) some Ti has a collision

i.e. check (and if necessary rebuild)

each table immediately after building it

Page 63: Hashing Part Two: Static Perfect Hashing

Perfect hashing - attempt three

n

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i

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)n2i

(Step 3) Immediately repeat a step if eithera) T has more than n collisionsb) some Ti has a collision

Page 64: Hashing Part Two: Static Perfect Hashing

Perfect hashing - attempt three

n

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i

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)n2i

(Step 3) Immediately repeat a step if eithera) 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

Page 65: Hashing Part Two: Static Perfect Hashing

Perfect hashing - attempt three

n

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i

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)n2i

(Step 3) Immediately repeat a step if eithera) 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

Page 66: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

Page 67: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

Page 68: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

The size of Ti is O(ni2)

Page 69: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

The size of Ti is O(ni2)

Storing hi uses O(1) space

Page 70: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

The size of Ti is O(ni2)

So the total space is. . .

Storing hi uses O(1) space

Page 71: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

The size of Ti is O(ni2)

So the total space is. . .

O(n)+∑i

O(n2i ) = O(n)+O

∑i

n2i

Storing hi uses O(1) space

Page 72: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

The size of Ti is O(ni2)

So the total space is. . .

Storing hi uses O(1) space

O(n)+∑i

O(n2i ) = O(n)+O

∑i

n2i

Page 73: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

The size of Ti is O(ni2)

So the total space is. . .

Storing hi uses O(1) spacehow big is this?

O(n)+∑i

O(n2i ) = O(n)+O

∑i

n2i

Page 74: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

The size of Ti is O(ni2)

So the total space is. . .

Storing hi uses O(1) spacehow big is this?

O(n)+∑i

O(n2i ) = O(n)+O

∑i

n2i

How big is∑

i n2i ?

Page 75: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

The size of Ti is O(ni2)

So the total space is. . .

Storing hi uses O(1) spacehow big is this?

O(n)+∑i

O(n2i ) = O(n)+O

∑i

n2i

How big is∑

i n2i ?

There are(ni2

)collisions in T [i]

Page 76: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

The size of Ti is O(ni2)

So the total space is. . .

Storing hi uses O(1) spacehow big is this?

O(n)+∑i

O(n2i ) = O(n)+O

∑i

n2i

How big is∑

i n2i ?

There are(ni2

)collisions in T [i] so there are

∑i

(ni2

)collisions in T

Page 77: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

The size of Ti is O(ni2)

So the total space is. . .

Storing hi uses O(1) spacehow big is this?

O(n)+∑i

O(n2i ) = O(n)+O

∑i

n2i

How big is∑

i n2i ?

There are(ni2

)collisions in T [i] so there are

∑i

(ni2

)collisions in T

but we know that there are at most n collisions in T . . .

Page 78: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

The size of Ti is O(ni2)

So the total space is. . .

Storing hi uses O(1) spacehow big is this?

O(n)+∑i

O(n2i ) = O(n)+O

∑i

n2i

How big is∑

i n2i ?

There are(ni2

)collisions in T [i] so there are

∑i

(ni2

)collisions in T

but we know that there are at most n collisions in T . . .∑i

n2i4

6∑i

(ni2

)6 n

Page 79: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

The size of Ti is O(ni2)

So the total space is. . .

Storing hi uses O(1) spacehow big is this?

O(n)+∑i

O(n2i ) = O(n)+O

∑i

n2i

How big is∑

i n2i ?

There are(ni2

)collisions in T [i] so there are

∑i

(ni2

)collisions in T

but we know that there are at most n collisions in T . . .∑i

n2i4

6∑i

(ni2

)6 n

Page 80: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

The size of Ti is O(ni2)

So the total space is. . .

Storing hi uses O(1) spacehow big is this?

O(n)+∑i

O(n2i ) = O(n)+O

∑i

n2i

How big is∑

i n2i ?

There are(ni2

)collisions in T [i] so there are

∑i

(ni2

)collisions in T

but we know that there are at most n collisions in T . . .

(ni2

)=

ni(ni−1)2 >

n2i4

∑i

n2i4

6∑i

(ni2

)6 n

Page 81: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

The size of Ti is O(ni2)

So the total space is. . .

Storing hi uses O(1) spacehow big is this?

O(n)+∑i

O(n2i ) = O(n)+O

∑i

n2i

How big is∑

i n2i ?

There are(ni2

)collisions in T [i] so there are

∑i

(ni2

)collisions in T

but we know that there are at most n collisions in T . . .∑i

n2i4

6∑i

(ni2

)6 n

Page 82: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

The size of Ti is O(ni2)

So the total space is. . .

Storing hi uses O(1) spacehow big is this?

O(n)+∑i

O(n2i ) = O(n)+O

∑i

n2i

How big is∑

i n2i ?

There are(ni2

)collisions in T [i] so there are

∑i

(ni2

)collisions in T

but we know that there are at most n collisions in T . . .∑i

n2i4

6∑i

(ni2

)6 n

∑i

n2i 6 4nor

Page 83: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

The size of Ti is O(ni2)

So the total space is. . .

Storing hi uses O(1) spacehow big is this?

How big is∑

i n2i ?

There are(ni2

)collisions in T [i] so there are

∑i

(ni2

)collisions in T

but we know that there are at most n collisions in T . . .∑i

n2i4

6∑i

(ni2

)6 n

∑i

n2i 6 4nor

O(n)+∑i

O(n2i ) = O(n)+O

∑i

n2i

= O(n)

Page 84: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Space usage

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

How much space does this use?

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The size of T is O(n)

The size of Ti is O(ni2)

So the total space is. . .

Storing hi uses O(1) space

O(n)+∑i

O(n2i ) = O(n)+O

∑i

n2i

= O(n)

Page 85: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Expected construction time

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

Page 86: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Expected construction time

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) some Ti has a collision

T

Ti

The expected construction time for T is O(n)

(we considered this on a previous slide)

Page 87: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Expected construction time

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) 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(ni2)

Page 88: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Expected construction time

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) 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(ni2)

- we insert ni items into a table of size m = n2i- then repeat if there was a collision

Page 89: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Expected construction time

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) 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(ni2)

- we insert ni items into a table of size m = n2i

(we also considered this on a previous slide)- then repeat if there was a collision

Page 90: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Expected construction time

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) 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(ni2)

- we insert ni items into a table of size m = n2i

(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

Page 91: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Expected construction time

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) 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(ni2)

The overall expected construction time is therefore:

E(construction time) = E

construction time of T +∑i

construction time of Ti

Page 92: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Expected construction time

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) 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(ni2)

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)

Page 93: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Expected construction time

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) 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(ni2)

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(n2i ) = O(n)+O

∑i

n2i

Page 94: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Expected construction time

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) 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(ni2)

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(n2i ) = O(n)+O

∑i

n2i

= O(n)

Page 95: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Expected construction time

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) 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(ni2)

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(n2i ) = O(n)+O

∑i

n2i

= O(n)

∑i

n2i 6 4n

Page 96: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Expected construction time

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) 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(ni2)

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(n2i ) = O(n)+O

∑i

n2i

= O(n)

Page 97: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Summary

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) 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]

Page 98: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Summary

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) 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]

Page 99: Hashing Part Two: Static Perfect Hashing

Perfect Hashing - Summary

n n2i

Step 1: Insert everything into a hash table, T , of size nusing 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 n2i using w.u hash function hi

(Step 3) Immediately repeat if eithera) T has more than n collisionsb) 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]