hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · back to...

163
Advanced Algorithms – COMS31900 Hashing part three Cuckoo Hashing Rapha ¨ el Clifford Slides by Benjamin Sach

Upload: others

Post on 11-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Advanced Algorithms – COMS31900

Hashing part three

Cuckoo Hashing

Raphael Clifford

Slides by Benjamin Sach

Page 2: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Back to the start (again)

� A dynamic dictionary stores (key, value)-pairs and supports:

Universe U of u keys.Hash table T of size m > n.

Collisions are fixed by chaining

A hash function maps

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)

a key x to position h(x)

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)

Page 3: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Back to the start (again)

� A dynamic dictionary stores (key, value)-pairs and supports:

Universe U of u keys.Hash table T of size m > n.

Collisions are fixed by chaining

A hash function maps

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)

in fact this result can be generalised . . .

a key x to position h(x)

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)

Page 4: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Back to the start (again)

� A dynamic dictionary stores (key, value)-pairs and supports:

Universe U of u keys.Hash table T of size m > n.

Collisions are fixed by chaining

A hash function maps

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)

in fact this result can be generalised . . .

a key x to position h(x)

bucketing

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)

Page 5: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Back to the start (again)

� A dynamic dictionary stores (key, value)-pairs and supports:

Universe U of u keys.Hash table T of size m > n.

Collisions are fixed by chaining

A hash function maps

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)

in fact this result can be generalised . . .

a key x to position h(x)

bucketing

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)

Page 6: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Back to the start (again)

� A dynamic dictionary stores (key, value)-pairs and supports:

Universe U of u keys.Hash table T of size m > n.

Collisions are fixed by chaining

A hash function maps

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)

in fact this result can be generalised . . .

a key x to position h(x)

bucketing

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)

Page 7: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Back to the start (again)

� A dynamic dictionary stores (key, value)-pairs and supports:

Universe U of u keys.Hash table T of size m > n.

Collisions are fixed by chaining

A hash function maps

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)

in fact this result can be generalised . . .

a key x to position h(x)

bucketing

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)

Page 8: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Back to the start (again)

� A dynamic dictionary stores (key, value)-pairs and supports:

Universe U of u keys.Hash table T of size m > n.

Collisions are fixed by chaining

A hash function maps

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)

in fact this result can be generalised . . .

a key x to position h(x)

bucketing

We require that we can recover

any key from its bucket in O(s) time

where s is the number of keys in the bucket

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)

Page 9: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Back to the start (again)

� A dynamic dictionary stores (key, value)-pairs and supports:

Universe U of u keys.Hash table T of size m > n.

Collisions are fixed by chaining

A hash function maps

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)

in fact this result can be generalised . . .

a key x to position h(x)

bucketing

We require that we can recover

any key from its bucket in O(s) time

where s is the number of keys in the bucket

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)

Page 10: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Back to the start (again)

� A dynamic dictionary stores (key, value)-pairs and supports:

Universe U of u keys.Hash table T of size m > n.

Collisions are fixed by chaining

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)

in fact this result can be generalised . . .

bucketing

We require that we can recover

any key from its bucket in O(s) time

where s is the number of keys in the bucket

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)

Page 11: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Back to the start (again)

� A dynamic dictionary stores (key, value)-pairs and supports:

Universe U of u keys.Hash table T of size m > n.

Collisions are fixed by chaining

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)

in fact this result can be generalised . . .

bucketing

We require that we can recover

any key from its bucket in O(s) time

where s is the number of keys in the bucket

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)

Locating the bucket containing

a given key takes O(1) time

Page 12: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Back to the start (again)

� A dynamic dictionary stores (key, value)-pairs and supports:

Universe U of u keys.Hash table T of size m > n.

Collisions are fixed by chaining

n arbitrary operations arrive online, one at a time.

add(key, value), lookup(key) (which returns value) and delete(key)

bucketing

We require that we can recover

any key from its bucket in O(s) time

where s is the number of keys in the bucketLocating the bucket containing

a given key takes O(1) time

Page 13: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Back to the start (again)

� A dynamic dictionary stores (key, value)-pairs and supports:

Universe U of u keys.Hash table T of size m > n.

Collisions are fixed by chaining

n arbitrary operations arrive online, one at a time.

add(key, value), lookup(key) (which returns value) and delete(key)

bucketing

We require that we can recover

any key from its bucket in O(s) time

where s is the number of keys in the bucket

If our construction has the property that,for any two keys x, y ∈ U (with x 6= y),

the probability that x and y are in the same bucket is O(

1m

)

Locating the bucket containing

a given key takes O(1) time

Page 14: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Back to the start (again)

� A dynamic dictionary stores (key, value)-pairs and supports:

Universe U of u keys.Hash table T of size m > n.

Collisions are fixed by chaining

n arbitrary operations arrive online, one at a time.

add(key, value), lookup(key) (which returns value) and delete(key)

bucketing

We require that we can recover

any key from its bucket in O(s) time

where s is the number of keys in the bucket

If our construction has the property that,for any two keys x, y ∈ U (with x 6= y),

the probability that x and y are in the same bucket is O(

1m

)

Locating the bucket containing

a given key takes O(1) time

Page 15: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Back to the start (again)

� A dynamic dictionary stores (key, value)-pairs and supports:

Universe U of u keys.Hash table T of size m > n.

Collisions are fixed by chaining

n arbitrary operations arrive online, one at a time.

add(key, value), lookup(key) (which returns value) and delete(key)

bucketing

We require that we can recover

any key from its bucket in O(s) time

where s is the number of keys in the bucket

If our construction has the property that,for any two keys x, y ∈ U (with x 6= y),

the probability that x and y are in the same bucket is O(

1m

)For any n operations, the expected run-time is O(1) per operation.

Locating the bucket containing

a given key takes O(1) time

Page 16: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

Page 17: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

What does amortised expected O(1) time mean?!

Page 18: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

What does amortised expected O(1) time mean?!let’s build it up. . .

Page 19: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

What does amortised expected O(1) time mean?!let’s build it up. . .

“O(1) worst-case time per operation”means every operation takes constant time

Page 20: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

What does amortised expected O(1) time mean?!let’s build it up. . .

“O(1) worst-case time per operation”means every operation takes constant time

“The total worst-case time complexity of performing any n operations is O(n)”

Page 21: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

What does amortised expected O(1) time mean?!let’s build it up. . .

“O(1) worst-case time per operation”means every operation takes constant time

“The total worst-case time complexity of performing any n operations is O(n)”this does not imply that every operation takes constant time

Page 22: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

What does amortised expected O(1) time mean?!let’s build it up. . .

“O(1) worst-case time per operation”means every operation takes constant time

“The total worst-case time complexity of performing any n operations is O(n)”this does not imply that every operation takes constant time

However, it does mean that the amortised worst-case time complexity of an operation is O(1)

Page 23: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

What does amortised expected O(1) time mean?!let’s build it up. . .

“O(1) worst-case time per operation”means every operation takes constant time in expectation

“The total worst-case time complexity of performing any n operations is O(n)”this does not imply that every operation takes constant time in expectation

However, it does mean that the amortised worst-case time complexity of an operation is O(1)

expected

expected

expected

Page 24: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

In Cuckoo hashing there is a single hash table but two hash functions: h1 and h2.

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

Page 25: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

In Cuckoo hashing there is a single hash table but two hash functions: h1 and h2.

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

Each key in the table is either stored at position h1(x) or h2(x).

Page 26: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

In Cuckoo hashing there is a single hash table but two hash functions: h1 and h2.

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

Each key in the table is either stored at position h1(x) or h2(x).

h1(x) h2(x)

Page 27: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

In Cuckoo hashing there is a single hash table but two hash functions: h1 and h2.

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

Each key in the table is either stored at position h1(x) or h2(x).

h1(x)

x

h2(x)

Page 28: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

In Cuckoo hashing there is a single hash table but two hash functions: h1 and h2.

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

Each key in the table is either stored at position h1(x) or h2(x).

h1(x) h2(x)

x

Page 29: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

In Cuckoo hashing there is a single hash table but two hash functions: h1 and h2.

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

Each key in the table is either stored at position h1(x) or h2(x).

h1(x)

x

h2(x)

Page 30: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

In Cuckoo hashing there is a single hash table but two hash functions: h1 and h2.

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

Each key in the table is either stored at position h1(x) or h2(x).

h1(x) h2(x)

x

Page 31: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

In Cuckoo hashing there is a single hash table but two hash functions: h1 and h2.

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

Each key in the table is either stored at position h1(x) or h2(x).

h1(x) h2(x)

x

Important: We never store multiple keys at the same position

Page 32: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

In Cuckoo hashing there is a single hash table but two hash functions: h1 and h2.

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

Each key in the table is either stored at position h1(x) or h2(x).

h1(x) h2(x)

x

Important: We never store multiple keys at the same position

Therefore, as claimed, lookup takes O(1) time. . .

Page 33: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Dynamic perfect hashing

In Cuckoo hashing there is a single hash table but two hash functions: h1 and h2.

� A dynamic dictionary stores (key, value)-pairs and supports:

add(key, value), lookup(key) (which returns value) and delete(key)

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

Each key in the table is either stored at position h1(x) or h2(x).

h1(x) h2(x)

x

Important: We never store multiple keys at the same position

Therefore, as claimed, lookup takes O(1) time. . . but how do we do inserts?

Page 34: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)

xx

Page 35: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop (and congratulate yourself on a job well done)

x

Page 36: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop

x

Page 37: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop

xx

Page 38: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop

y

xx

Page 39: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop

Step 2: Let y be the key currently in position h1(x)

y

xx

Page 40: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop

Step 2: Let y be the key currently in position h1(x)

evict key y and replace it with key x

y

xx

Page 41: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop

Step 2: Let y be the key currently in position h1(x)

evict key y and replace it with key x

y

x

Page 42: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop

Step 2: Let y be the key currently in position h1(x)

evict key y and replace it with key x

where should we put key y?

y

x

Page 43: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop

Step 2: Let y be the key currently in position h1(x)

evict key y and replace it with key x

where should we put key y?in the other position it’s allowed in

y

x

Page 44: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop

Step 2: Let y be the key currently in position h1(x)

evict key y and replace it with key x

where should we put key y?in the other position it’s allowed in

y

x

h1(y) h2(y)

Page 45: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop

Step 2: Let y be the key currently in position h1(x)

evict key y and replace it with key x

Step 3: Let pos be the other position y is allowed to be ini.e pos = h2(y) if h1(x) = h1(y) and pos = h1(y) otherwise

y

x

h1(y) h2(y)

Page 46: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop

Step 2: Let y be the key currently in position h1(x)

evict key y and replace it with key x

Step 3: Let pos be the other position y is allowed to be ini.e pos = h2(y) if h1(x) = h1(y) and pos = h1(y) otherwise

Step 4: Attempt to put y in position posif that position is empty, stop

y

x

h1(y) h2(y)

Page 47: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop

Step 2: Let y be the key currently in position h1(x)

evict key y and replace it with key x

Step 3: Let pos be the other position y is allowed to be ini.e pos = h2(y) if h1(x) = h1(y) and pos = h1(y) otherwise

Step 4: Attempt to put y in position posif that position is empty, stop

h1(y) h2(y)

yx

Page 48: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop

Step 2: Let y be the key currently in position h1(x)

evict key y and replace it with key x

Step 3: Let pos be the other position y is allowed to be ini.e pos = h2(y) if h1(x) = h1(y) and pos = h1(y) otherwise

Step 4: Attempt to put y in position posif that position is empty, stop

z

y

x

h1(y) h2(y)

h1(z)h2(z)

Page 49: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop

Step 2: Let y be the key currently in position h1(x)

evict key y and replace it with key x

Step 3: Let pos be the other position y is allowed to be ini.e pos = h2(y) if h1(x) = h1(y) and pos = h1(y) otherwise

Step 4: Attempt to put y in position posif that position is empty, stop

Step 5: Let z be the key currently in position pos

evict key z and replace it with key y

z

y

x

h1(y) h2(y)

h1(z)h2(z)

Page 50: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop

Step 2: Let y be the key currently in position h1(x)

evict key y and replace it with key x

Step 3: Let pos be the other position y is allowed to be ini.e pos = h2(y) if h1(x) = h1(y) and pos = h1(y) otherwise

Step 4: Attempt to put y in position posif that position is empty, stop

Step 5: Let z be the key currently in position pos

evict key z and replace it with key y

h1(y) h2(y)

yx

z

h1(z)h2(z)

Page 51: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop

Step 2: Let y be the key currently in position h1(x)

evict key y and replace it with key x

Step 3: Let pos be the other position y is allowed to be ini.e pos = h2(y) if h1(x) = h1(y) and pos = h1(y) otherwise

Step 4: Attempt to put y in position posif that position is empty, stop

Step 5: Let z be the key currently in position pos

evict key z and replace it with key y

h1(y) h2(y)

yx

z

h1(z)h2(z)

Page 52: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Inserts in Cuckoo hashing

h1(x) h2(x)

Step 1: Attempt to put x in position h1(x)if that position is empty, stop

Step 2: Let y be the key currently in position h1(x)

evict key y and replace it with key x

Step 3: Let pos be the other position y is allowed to be ini.e pos = h2(y) if h1(x) = h1(y) and pos = h1(y) otherwise

Step 4: Attempt to put y in position posif that position is empty, stop

Step 5: Let z be the key currently in position pos

evict key z and replace it with key y and so on. . .

h1(y) h2(y)

yx

z

h1(z)h2(z)

Page 53: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Pseudocode

h1(x) h2(x)

h1(y) h2(y)

yx

z

h1(z)h2(z)

add(x):

� pos← h1(x)

� Repeat at most n times:

� If T [pos] is empty then T [pos]← x.

� Otherwise,

y ← T [pos],

T [pos]← x,

pos← the other possible location for y.

(i.e. if y was evicted from h1(y) then pos← h2(y), otherwise pos← h1(y).)

x← y.

Repeat

� Give up and rehash the whole table.i.e. empty the table, pick two new hash functions and reinsert every key

Page 54: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

If we fail to insert a new key x,(i.e. we still have an “evicted” key after moving around keys n times)

then we declare the table “rubbish” and rehash.

Page 55: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

If we fail to insert a new key x,

� Now try to insert x again.

� If we fail, rehash and try to insert x again. Repeat until it succeeds.

(i.e. we still have an “evicted” key after moving around keys n times)then we declare the table “rubbish” and rehash.

What does rehashing involve?

Page 56: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

If we fail to insert a new key x,

� Now try to insert x again.

� If we fail, rehash and try to insert x again. Repeat until it succeeds.

(i.e. we still have an “evicted” key after moving around keys n times)then we declare the table “rubbish” and rehash.

What does rehashing involve?

Suppose that the table contains the k keys x1, . . . , xkat the time of we fail to insert key x.

Page 57: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

If we fail to insert a new key x,

� Now try to insert x again.

� If we fail, rehash and try to insert x again. Repeat until it succeeds.

To rehash we:

(i.e. we still have an “evicted” key after moving around keys n times)then we declare the table “rubbish” and rehash.

What does rehashing involve?

Suppose that the table contains the k keys x1, . . . , xkat the time of we fail to insert key x.

Page 58: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

If we fail to insert a new key x,

� Now try to insert x again.

� If we fail, rehash and try to insert x again. Repeat until it succeeds.

To rehash we:

(i.e. we still have an “evicted” key after moving around keys n times)then we declare the table “rubbish” and rehash.

What does rehashing involve?

Suppose that the table contains the k keys x1, . . . , xkat the time of we fail to insert key x.

Randomly pick two new hash functions h1 and h2. (More about this in a minute.)

Page 59: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

If we fail to insert a new key x,

� Now try to insert x again.

� If we fail, rehash and try to insert x again. Repeat until it succeeds.

To rehash we:

(i.e. we still have an “evicted” key after moving around keys n times)then we declare the table “rubbish” and rehash.

What does rehashing involve?

Suppose that the table contains the k keys x1, . . . , xkat the time of we fail to insert key x.

Randomly pick two new hash functions h1 and h2. (More about this in a minute.)

Build a new empty hash table of the same size

Page 60: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

If we fail to insert a new key x,

� Now try to insert x again.

� If we fail, rehash and try to insert x again. Repeat until it succeeds.

Reinsert the keys x1, . . . , xk and then x,

To rehash we:

(i.e. we still have an “evicted” key after moving around keys n times)then we declare the table “rubbish” and rehash.

What does rehashing involve?

Suppose that the table contains the k keys x1, . . . , xkat the time of we fail to insert key x.

Randomly pick two new hash functions h1 and h2. (More about this in a minute.)

Build a new empty hash table of the same size

one by one, using the normal add operation.

Page 61: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

If we fail to insert a new key x,

� Now try to insert x again.

� If we fail, rehash and try to insert x again. Repeat until it succeeds.

Reinsert the keys x1, . . . , xk and then x,

To rehash we:

(i.e. we still have an “evicted” key after moving around keys n times)then we declare the table “rubbish” and rehash.

What does rehashing involve?

Suppose that the table contains the k keys x1, . . . , xkat the time of we fail to insert key x.

Randomly pick two new hash functions h1 and h2. (More about this in a minute.)

Build a new empty hash table of the same size

If we fail while rehashing. . . we start from the beginning again

one by one, using the normal add operation.

Page 62: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

If we fail to insert a new key x,

� Now try to insert x again.

� If we fail, rehash and try to insert x again. Repeat until it succeeds.

Reinsert the keys x1, . . . , xk and then x,

To rehash we:

(i.e. we still have an “evicted” key after moving around keys n times)then we declare the table “rubbish” and rehash.

What does rehashing involve?

Suppose that the table contains the k keys x1, . . . , xkat the time of we fail to insert key x.

Randomly pick two new hash functions h1 and h2. (More about this in a minute.)

Build a new empty hash table of the same size

If we fail while rehashing. . . we start from the beginning again

This is rather slow. . . but we will prove that it happens rarely

one by one, using the normal add operation.

Page 63: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Assumptions

We will follow the analysis in the paper Cuckoo hashing for undergraduates,

2006, by Rasmus Pagh (see the link on unit web page).

Page 64: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Assumptions

We will follow the analysis in the paper Cuckoo hashing for undergraduates,

2006, by Rasmus Pagh (see the link on unit web page).

We make the following assumptions:

Page 65: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Assumptions

We will follow the analysis in the paper Cuckoo hashing for undergraduates,

2006, by Rasmus Pagh (see the link on unit web page).

We make the following assumptions:

h1 and h2 are independenti.e. h1(x) says nothing about h2(x), and vice versa.

Page 66: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Assumptions

We will follow the analysis in the paper Cuckoo hashing for undergraduates,

2006, by Rasmus Pagh (see the link on unit web page).

We make the following assumptions:

h1 and h2 are truly randomi.e. each key is independently mapped to each of the m positions

in the hash table with probability 1m .

h1 and h2 are independenti.e. h1(x) says nothing about h2(x), and vice versa.

Page 67: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Assumptions

We will follow the analysis in the paper Cuckoo hashing for undergraduates,

2006, by Rasmus Pagh (see the link on unit web page).

We make the following assumptions:

h1 and h2 are truly random

Computing the value of h1(x) and h2(x) takes O(1) worst-case time

i.e. each key is independently mapped to each of the m positions

in the hash table with probability 1m .

h1 and h2 are independenti.e. h1(x) says nothing about h2(x), and vice versa.

Page 68: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Assumptions

We will follow the analysis in the paper Cuckoo hashing for undergraduates,

2006, by Rasmus Pagh (see the link on unit web page).

We make the following assumptions:

h1 and h2 are truly random

Computing the value of h1(x) and h2(x) takes O(1) worst-case time

i.e. each key is independently mapped to each of the m positions

in the hash table with probability 1m .

h1 and h2 are independenti.e. h1(x) says nothing about h2(x), and vice versa.

There are at most n keys in the hash table at any time.

Page 69: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Assumptions

We will follow the analysis in the paper Cuckoo hashing for undergraduates,

2006, by Rasmus Pagh (see the link on unit web page).

We make the following assumptions:

h1 and h2 are truly random

Computing the value of h1(x) and h2(x) takes O(1) worst-case time

i.e. each key is independently mapped to each of the m positions

in the hash table with probability 1m .

h1 and h2 are independenti.e. h1(x) says nothing about h2(x), and vice versa.

There are at most n keys in the hash table at any time.

REASONABLE

ASSUMPTION

Page 70: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Assumptions

We will follow the analysis in the paper Cuckoo hashing for undergraduates,

2006, by Rasmus Pagh (see the link on unit web page).

We make the following assumptions:

h1 and h2 are truly random

Computing the value of h1(x) and h2(x) takes O(1) worst-case time

i.e. each key is independently mapped to each of the m positions

in the hash table with probability 1m .

h1 and h2 are independenti.e. h1(x) says nothing about h2(x), and vice versa.

There are at most n keys in the hash table at any time.

UNREASONABLEASSUMPTION

REASONABLE

ASSUMPTION

Page 71: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Assumptions

We will follow the analysis in the paper Cuckoo hashing for undergraduates,

2006, by Rasmus Pagh (see the link on unit web page).

We make the following assumptions:

h1 and h2 are truly random

Computing the value of h1(x) and h2(x) takes O(1) worst-case time

i.e. each key is independently mapped to each of the m positions

in the hash table with probability 1m .

h1 and h2 are independenti.e. h1(x) says nothing about h2(x), and vice versa.

There are at most n keys in the hash table at any time.

UNREASONABLEASSUMPTION

REASONABLE

ASSUMPTION

QUESTIONABLE

ASSUMPTION

Page 72: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Assumptions

We will follow the analysis in the paper Cuckoo hashing for undergraduates,

2006, by Rasmus Pagh (see the link on unit web page).

We make the following assumptions:

h1 and h2 are truly random

Computing the value of h1(x) and h2(x) takes O(1) worst-case time

i.e. each key is independently mapped to each of the m positions

in the hash table with probability 1m .

h1 and h2 are independenti.e. h1(x) says nothing about h2(x), and vice versa.

There are at most n keys in the hash table at any time.

UNREASONABLEASSUMPTION

REASONABLE

ASSUMPTION

QUESTIONABLE

ASSUMPTION

NOT ACTUALLY ANASSUMPTION

Page 73: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

(size m)

Page 74: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

The cuckoo graph:(size m)

Page 75: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

The cuckoo graph:(size m)A vertex for each position of the table.

Page 76: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

The cuckoo graph:

m vertices

(size m)A vertex for each position of the table.

Page 77: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

The cuckoo graph:

For each key x there is an undirected edge

m vertices

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

Page 78: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x1

h2(x1)

h1(x1)

The cuckoo graph:

For each key x there is an undirected edge

m vertices

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

Page 79: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x1

h2(x1)

h1(x1)

x2

The cuckoo graph:

For each key x there is an undirected edge

m vertices

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

Page 80: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x1

h2(x1)

h1(x1)

x2

x3

The cuckoo graph:

For each key x there is an undirected edge

m vertices

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

Page 81: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x1

h2(x1)

h1(x1)

x2

x3

x4

The cuckoo graph:

For each key x there is an undirected edge

m vertices

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

Page 82: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x3

x4

The cuckoo graph:

For each key x there is an undirected edge

m vertices

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

x1

Page 83: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x3

x4

x5

The cuckoo graph:

For each key x there is an undirected edge

m vertices

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

x1

Page 84: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x3

x4

x5

The cuckoo graph:

For each key x there is an undirected edge

m vertices

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

h1(x5)

x1

h2(x5)

Page 85: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x3

x4

x5

The cuckoo graph:

For each key x there is an undirected edge

m vertices

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

h1(x5)

x1

h2(x5)There is no space for x5. . .

Page 86: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x3

x4

x5

The cuckoo graph:

For each key x there is an undirected edge

m vertices

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

h1(x5)

x1

h2(x5)There is no space for x5. . .

so we make spaceby moving x2 and then x3

Page 87: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x3

x4

x5

The cuckoo graph:

For each key x there is an undirected edge

m vertices

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

h1(x5)

x1

h2(x5)There is no space for x5. . .

so we make spaceby moving x2 and then x3

Page 88: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x3

x4

x5

The cuckoo graph:

For each key x there is an undirected edge

m vertices

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

h1(x5)

x1

h2(x5)There is no space for x5. . .

so we make spaceby moving x2 and then x3

Page 89: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x4

x5

The cuckoo graph:

For each key x there is an undirected edge

m vertices

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

x3

h1(x5)

x1

h2(x5)There is no space for x5. . .

so we make spaceby moving x2 and then x3

Page 90: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x4

x5

The cuckoo graph:

For each key x there is an undirected edge

m vertices

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

x3

h1(x5)

x1

h2(x5)There is no space for x5. . .

so we make spaceby moving x2 and then x3

The number of moves performed while adding a key isthe length of the corresponding path in the cuckoo graph

Page 91: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x4

x5

The cuckoo graph:

For each key x there is an undirected edge

m vertices

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

x3

x1

The number of moves performed while adding a key isthe length of the corresponding path in the cuckoo graph

Page 92: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x4

x5

The cuckoo graph:

For each key x there is an undirected edge

m vertices

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

x3

x1The number of moves performed while adding a key is

the length of the corresponding path in the cuckoo graph

Page 93: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x4

x5

x6

The cuckoo graph:

For each key x there is an undirected edge

m vertices

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

x3

x1The number of moves performed while adding a key is

the length of the corresponding path in the cuckoo graph

Page 94: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x4

x5

x6

The cuckoo graph:

For each key x there is an undirected edge

m vertices

Inserting key x6 creates a cycle.

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

x3

x1The number of moves performed while adding a key is

the length of the corresponding path in the cuckoo graph

Page 95: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x4

x5

x6

The cuckoo graph:

For each key x there is an undirected edge

m vertices

Inserting key x6 creates a cycle.

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

Cycles are dangerous. . .x3

x1The number of moves performed while adding a key is

the length of the corresponding path in the cuckoo graph

Page 96: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x4

x5

x6

The cuckoo graph:

For each key x there is an undirected edge

m vertices

Inserting key x6 creates a cycle.

x7

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

Cycles are dangerous. . .x3

x1The number of moves performed while adding a key is

the length of the corresponding path in the cuckoo graph

Page 97: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x4

x5

x6

The cuckoo graph:

For each key x there is an undirected edge

m vertices

Inserting key x6 creates a cycle.

x7When key x7 is inserted where does it go?

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

Cycles are dangerous. . .x3

x1The number of moves performed while adding a key is

the length of the corresponding path in the cuckoo graph

Page 98: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x4

x5

x6

The cuckoo graph:

For each key x there is an undirected edge

m vertices

Inserting key x6 creates a cycle.

x7When key x7 is inserted where does it go?

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

Cycles are dangerous. . .x3

x1

there are 6 keys but only 5 spaces

The number of moves performed while adding a key isthe length of the corresponding path in the cuckoo graph

Page 99: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x4

x5

x6

The cuckoo graph:

For each key x there is an undirected edge

m vertices

Inserting key x6 creates a cycle.

x7When key x7 is inserted where does it go?

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

Cycles are dangerous. . .

The keys would be moved around in an infinite loop

x3

but we stop and rehash after n moves. . .

x1

there are 6 keys but only 5 spaces

The number of moves performed while adding a key isthe length of the corresponding path in the cuckoo graph

Page 100: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x4

x5

x6

The cuckoo graph:

For each key x there is an undirected edge

m vertices

Inserting key x6 creates a cycle.

x7When key x7 is inserted where does it go?

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

Cycles are dangerous. . .

The keys would be moved around in an infinite loop

x3

but we stop and rehash after n moves. . .

x1

there are 6 keys but only 5 spaces

The number of moves performed while adding a key isthe length of the corresponding path in the cuckoo graph

Inserting a key into a cycle always causes a rehash

Page 101: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x4

x5

x6

The cuckoo graph:

For each key x there is an undirected edge

m vertices

x7

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

x3

x1The number of moves performed while adding a key is

the length of the corresponding path in the cuckoo graph

Inserting a key into a cycle always causes a rehash

Page 102: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x4

x5

x6

The cuckoo graph:

For each key x there is an undirected edge

m vertices

x7

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

x3

x1The number of moves performed while adding a key is

the length of the corresponding path in the cuckoo graph

Inserting a key into a cycle always causes a rehash

Page 103: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x4

x5

x6

The cuckoo graph:

For each key x there is an undirected edge

m vertices

x7

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

x3

x1The number of moves performed while adding a key is

the length of the corresponding path in the cuckoo graph

Inserting a key into a cycle always causes a rehashThis is the only way a rehash can happen

Page 104: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Cuckoo graphHash table

x2

x4

x5

x6

The cuckoo graph:

For each key x there is an undirected edge

m vertices

x7

We will analyse the probability of either a cycle or a long path occuring in the graph

(size m)A vertex for each position of the table.

between h1(x) and h2(x).

x3

while inserting any n keys.

x1The number of moves performed while adding a key is

the length of the corresponding path in the cuckoo graph

Inserting a key into a cycle always causes a rehashThis is the only way a rehash can happen

Page 105: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

Page 106: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What does this say?

Page 107: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What does this say?(let c = 2 for simplicity)

Page 108: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What does this say?(let c = 2 for simplicity)

i j

Page 109: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What does this say?(let c = 2 for simplicity)

i j

Probability of a shortest path of length 1 is at most1

2·m

Page 110: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What does this say?(let c = 2 for simplicity)

i j

Probability of a shortest path of length 2 is at most1

4·m

Page 111: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What does this say?(let c = 2 for simplicity)

i j

Probability of a shortest path of length 3 is at most1

8·m

Page 112: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What does this say?(let c = 2 for simplicity)

i j

Probability of a shortest path of length 4 is at most1

16·m

Page 113: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What does this say?(let c = 2 for simplicity)

i j

Probability of a shortest path of length 4 is at most1

16·m

How likely is it that there even is a path?

Page 114: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What does this say?(let c = 2 for simplicity)

How likely is it that there even is a path?

Page 115: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What does this say?(let c = 2 for simplicity)

How likely is it that there even is a path?

Page 116: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What does this say?(let c = 2 for simplicity)

How likely is it that there even is a path?

If a path exists from i to j, there must be a shortest path (from i to j)

Page 117: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What does this say?(let c = 2 for simplicity)

How likely is it that there even is a path?

If a path exists from i to j, there must be a shortest path (from i to j)

Therefore the probability of a path from i to j existing is at most. . .∑∞`=1

1c`·m

(using the union bound over all possible path lengths.)

Page 118: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What does this say?(let c = 2 for simplicity)

How likely is it that there even is a path?

If a path exists from i to j, there must be a shortest path (from i to j)

Therefore the probability of a path from i to j existing is at most. . .∑∞`=1

1c`·m

(using the union bound over all possible path lengths.)

= 1m

∑∞`=1

1c`

Page 119: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What does this say?(let c = 2 for simplicity)

How likely is it that there even is a path?

If a path exists from i to j, there must be a shortest path (from i to j)

Therefore the probability of a path from i to j existing is at most. . .∑∞`=1

1c`·m

(using the union bound over all possible path lengths.)

= 1m

∑∞`=1

1c`

= 1m·(c−1) =

1m

Page 120: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What does this say?(let c = 2 for simplicity)

How likely is it that there even is a path?

If a path exists from i to j, there must be a shortest path (from i to j)

Therefore the probability of a path from i to j existing is at most. . .∑∞`=1

1c`·m

(using the union bound over all possible path lengths.)

= 1m

∑∞`=1

1c`

= 1m·(c−1) =

1m

So a path from i to j is rather unlikely to exist

Page 121: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What is the proof?

Page 122: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What is the proof?

The proof is in the directors cut of the slides (see notes)

Page 123: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What is the proof?

The proof is in the directors cut of the slides (see notes)

Can we at least see the pictures?

Page 124: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Paths in the cuckoo graph

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

table size is m n keys

What is the proof?

The proof is in the directors cut of the slides (see notes)

Can we at least see the pictures?

The proof is by induction on the length `:

Base case: ` = 1.

i j

key x

ki j

`−1

Inductive step:

Argue that each key has prob 2m2 to

create an edge (i, j)

Union bound over all n keys

Pick a third point k to split the path

very very unlikelyvery

unlikelyUnion bound over all k then all keys

Page 125: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Back to the start (again) (again)

� A dynamic dictionary stores (key, value)-pairs and supports:

Universe U of u keys.Hash table T of size m > n.

Collisions are fixed by chaining

n arbitrary operations arrive online, one at a time.

add(key, value), lookup(key) (which returns value) and delete(key)

bucketing

We require that we can recover

any key from its bucket in O(s) time

where s is the number of keys in the bucket

If our construction has the property that,for any two keys x, y ∈ U (with x 6= y),

the probability that x and y are in the same bucket is O(

1m

)For any n operations, the expected run-time is O(1) per operation.

Locating the bucket containing

a given key takes O(1) time

table size is m n keys

Page 126: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Don’t put all your eggs in one bucket

Hash tableWe say that two keys x, y are in the same bucket (conceptually)

x

y

z

w

iff there is a path between h1(x) and h1(y)in the cuckoo graph.

table size is m n keys

Page 127: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Don’t put all your eggs in one bucket

Hash tableWe say that two keys x, y are in the same bucket (conceptually)

For two distinct keys x, y, the probability

∞∑`=1

4

c` ·m=

4

m·∞∑`=1

1

c`=

4

m(c− 1)= O

(1

m

)x

y

z

where c > 1 is a constant.

w

iff there is a path between h1(x) and h1(y)in the cuckoo graph.

that they are in the same bucket is at most

(another union bound over all possible path lengths.)

table size is m n keys

Page 128: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Don’t put all your eggs in one bucket

Hash tableWe say that two keys x, y are in the same bucket (conceptually)

For two distinct keys x, y, the probability

∞∑`=1

4

c` ·m=

4

m·∞∑`=1

1

c`=

4

m(c− 1)= O

(1

m

)x

y

z

where c > 1 is a constant.

w

iff there is a path between h1(x) and h1(y)in the cuckoo graph.

that they are in the same bucket is at most

(another union bound over all possible path lengths.)

table size is m n keys

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

Page 129: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Don’t put all your eggs in one bucket

Hash tableWe say that two keys x, y are in the same bucket (conceptually)

For two distinct keys x, y, the probability

∞∑`=1

4

c` ·m=

4

m·∞∑`=1

1

c`=

4

m(c− 1)= O

(1

m

)x

y

z

where c > 1 is a constant.

w

iff there is a path between h1(x) and h1(y)in the cuckoo graph.

that they are in the same bucket is at most

(another union bound over all possible path lengths.)

table size is m n keys

Page 130: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Don’t put all your eggs in one bucket

Hash tableWe say that two keys x, y are in the same bucket (conceptually)

For two distinct keys x, y, the probability

∞∑`=1

4

c` ·m=

4

m·∞∑`=1

1

c`=

4

m(c− 1)= O

(1

m

)x

y

z

where c > 1 is a constant.

The time for an operation on x is bounded byw

iff there is a path between h1(x) and h1(y)in the cuckoo graph.

that they are in the same bucket is at most

(another union bound over all possible path lengths.)

the number of items in the bucket. (assuming there are no cycles.)

table size is m n keys

Page 131: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Don’t put all your eggs in one bucket

Hash tableWe say that two keys x, y are in the same bucket (conceptually)

For two distinct keys x, y, the probability

∞∑`=1

4

c` ·m=

4

m·∞∑`=1

1

c`=

4

m(c− 1)= O

(1

m

)x

y

z

where c > 1 is a constant.

The time for an operation on x is bounded byw

So we have that the expected time per operation is O(1)

iff there is a path between h1(x) and h1(y)in the cuckoo graph.

that they are in the same bucket is at most

(another union bound over all possible path lengths.)

the number of items in the bucket. (assuming there are no cycles.)

(assuming that m > 2cn and there are no cycles).

table size is m n keys

Page 132: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Don’t put all your eggs in one bucket

Hash tableWe say that two keys x, y are in the same bucket (conceptually)

For two distinct keys x, y, the probability

∞∑`=1

4

c` ·m=

4

m·∞∑`=1

1

c`=

4

m(c− 1)= O

(1

m

)x

y

z

where c > 1 is a constant.

The time for an operation on x is bounded byw

So we have that the expected time per operation is O(1)

iff there is a path between h1(x) and h1(y)in the cuckoo graph.

that they are in the same bucket is at most

(another union bound over all possible path lengths.)

the number of items in the bucket. (assuming there are no cycles.)

Further, lookups take O(1) time in the worst case.

(assuming that m > 2cn and there are no cycles).

table size is m n keys

Page 133: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

The previous analysis on the expected running time holds when there are no cycles.

Page 134: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

The previous analysis on the expected running time holds when there are no cycles.

However, we would expect there to be cycles every now and then, causing a rehash.

Page 135: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

The previous analysis on the expected running time holds when there are no cycles.

However, we would expect there to be cycles every now and then, causing a rehash.

How often does this happen? (sketch proof)

Page 136: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

The previous analysis on the expected running time holds when there are no cycles.

However, we would expect there to be cycles every now and then, causing a rehash.

How often does this happen? (sketch proof)

Consider inserting n keys into the table. . .

Page 137: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

The previous analysis on the expected running time holds when there are no cycles.

However, we would expect there to be cycles every now and then, causing a rehash.

A cycle is a path from a vertex i back to itself.

How often does this happen? (sketch proof)

Consider inserting n keys into the table. . . i

Page 138: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

The previous analysis on the expected running time holds when there are no cycles.

However, we would expect there to be cycles every now and then, causing a rehash.

A cycle is a path from a vertex i back to itself.

How often does this happen? (sketch proof)

Consider inserting n keys into the table. . .

so use previous result with i = j.. . .

i

Page 139: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

The previous analysis on the expected running time holds when there are no cycles.

However, we would expect there to be cycles every now and then, causing a rehash.

A cycle is a path from a vertex i back to itself.

How often does this happen? (sketch proof)

Consider inserting n keys into the table. . .

so use previous result with i = j.. . .

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

i

Page 140: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

The previous analysis on the expected running time holds when there are no cycles.

However, we would expect there to be cycles every now and then, causing a rehash.

A cycle is a path from a vertex i back to itself.

How often does this happen? (sketch proof)

Consider inserting n keys into the table. . .

so use previous result with i = j.. . .

For any positions i and j, and any constant c > 1, if m > 2cn then the probability that there exists a

shortest path in the cuckoo graph from i to j with length ` > 1, is at most 1c`·m .

LEMMA

The probability that a position i is involved in a cycle is at most

∞∑`=1

1

c` ·m=

1

m(c− 1).

(another union bound over all possible path lengths.)

i

Page 141: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

The probability that a position i is involved in a cycle is at most

∞∑`=1

1

c` ·m=

1

m(c− 1).

(another union bound over all possible path lengths.)

Page 142: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

The probability that a position i is involved in a cycle is at most

∞∑`=1

1

c` ·m=

1

m(c− 1).

The probability that there is at least one cycle is at most

m ·1

m(c− 1)=

1

c− 1.

(another union bound over all possible path lengths.)

Page 143: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

The probability that a position i is involved in a cycle is at most

∞∑`=1

1

c` ·m=

1

m(c− 1).

The probability that there is at least one cycle is at most

m ·1

m(c− 1)=

1

c− 1.

(another union bound over all possible path lengths.)

(union bound over all m positions in the table.)

Page 144: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

The probability that a position i is involved in a cycle is at most

∞∑`=1

1

c` ·m=

1

m(c− 1).

The probability that there is at least one cycle is at most

m ·1

m(c− 1)=

1

c− 1.

If we set c = 3, the probability is at most 12 that a cycle occurs

(another union bound over all possible path lengths.)

(union bound over all m positions in the table.)

(that there is a rehash) during the n insertions.

Page 145: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

The probability that a position i is involved in a cycle is at most

∞∑`=1

1

c` ·m=

1

m(c− 1).

The probability that there is at least one cycle is at most

m ·1

m(c− 1)=

1

c− 1.

If we set c = 3, the probability is at most 12 that a cycle occurs

The probability that there are two rehashes is 14 , and so on.

(another union bound over all possible path lengths.)

(union bound over all m positions in the table.)

(that there is a rehash) during the n insertions.

Page 146: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

The probability that a position i is involved in a cycle is at most

∞∑`=1

1

c` ·m=

1

m(c− 1).

The probability that there is at least one cycle is at most

m ·1

m(c− 1)=

1

c− 1.

If we set c = 3, the probability is at most 12 that a cycle occurs

The probability that there are two rehashes is 14 , and so on.

So the expected number of rehashes during n insertions is at most∑∞

i=1

( 12

)i= 1.

(another union bound over all possible path lengths.)

(union bound over all m positions in the table.)

(that there is a rehash) during the n insertions.

Page 147: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

If the expected time for one rehash is O(n) then

the expected time for all rehashes is also O(n)

(this is because we only expect there to be one rehash).

Page 148: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

If the expected time for one rehash is O(n) then

Therefore the amortised expected time for the rehashes over the n insertions is

the expected time for all rehashes is also O(n)

(this is because we only expect there to be one rehash).

O(1) per insertion (i.e. divide the total cost with n).

Page 149: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

If the expected time for one rehash is O(n) then

Therefore the amortised expected time for the rehashes over the n insertions is

Why is the expected time per rehash O(n)?

the expected time for all rehashes is also O(n)

(this is because we only expect there to be one rehash).

O(1) per insertion (i.e. divide the total cost with n).

Page 150: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

If the expected time for one rehash is O(n) then

Therefore the amortised expected time for the rehashes over the n insertions is

Why is the expected time per rehash O(n)?

First pick a new random h1 and h2 and construct the cuckoo graph

the expected time for all rehashes is also O(n)

(this is because we only expect there to be one rehash).

O(1) per insertion (i.e. divide the total cost with n).

using the at most n keys.

Page 151: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

If the expected time for one rehash is O(n) then

Therefore the amortised expected time for the rehashes over the n insertions is

Why is the expected time per rehash O(n)?

First pick a new random h1 and h2 and construct the cuckoo graph

the expected time for all rehashes is also O(n)

(this is because we only expect there to be one rehash).

O(1) per insertion (i.e. divide the total cost with n).

using the at most n keys.

Check for a cycle in the graph in O(n) time (and start again if you find one)

Page 152: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

If the expected time for one rehash is O(n) then

Therefore the amortised expected time for the rehashes over the n insertions is

Why is the expected time per rehash O(n)?

First pick a new random h1 and h2 and construct the cuckoo graph

the expected time for all rehashes is also O(n)

(this is because we only expect there to be one rehash).

O(1) per insertion (i.e. divide the total cost with n).

using the at most n keys.

Check for a cycle in the graph in O(n) time (and start again if you find one)

(you can do this using breadth-first search)

Page 153: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

Rehashing

If the expected time for one rehash is O(n) then

Therefore the amortised expected time for the rehashes over the n insertions is

Why is the expected time per rehash O(n)?

If there is no cycle, insert all the elements,

First pick a new random h1 and h2 and construct the cuckoo graph

the expected time for all rehashes is also O(n)

(this is because we only expect there to be one rehash).

O(1) per insertion (i.e. divide the total cost with n).

using the at most n keys.

Check for a cycle in the graph in O(n) time (and start again if you find one)

(you can do this using breadth-first search)

this takes O(n) time in expectation (as we have seen).

Page 154: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

A word about the assumptions

We have assumed true randomness. As we have discussed, this is not realistic.

Page 155: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

A word about the assumptions

We have assumed true randomness. As we have discussed, this is not realistic.

We have seen that weakly universal hash families are realisticwhere any two keys x, y are independent

Page 156: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

A word about the assumptions

We have assumed true randomness. As we have discussed, this is not realistic.

We have seen that weakly universal hash families are realisticwhere any two keys x, y are independent

A set H of hash functions is weakly universal if for any two distinct keys x, y ∈ U ,

Pr(h(x) = h(y)

)6 1

m (where h is picked uniformly at random from H)

Page 157: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

A word about the assumptions

We have assumed true randomness. As we have discussed, this is not realistic.

We have seen that weakly universal hash families are realistic

We can define a stronger hash families with k-wise independence.

here the hash values of any choice of k keys are independent.

where any two keys x, y are independent

A set H of hash functions is weakly universal if for any two distinct keys x, y ∈ U ,

Pr(h(x) = h(y)

)6 1

m (where h is picked uniformly at random from H)

Page 158: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

A word about the assumptions

We have assumed true randomness. As we have discussed, this is not realistic.

We have seen that weakly universal hash families are realistic

We can define a stronger hash families with k-wise independence.

here the hash values of any choice of k keys are independent.

where any two keys x, y are independent

A set H of hash functions is weakly universal if for any two distinct keys x, y ∈ U ,

Pr(h(x) = h(y)

)6 1

m (where h is picked uniformly at random from H)

A set H of hash functions is k-wise independent if

for any k distinct keys x1, x2 . . . xk ∈ U and k values v1, v2, . . . vk ∈ {0, 1, 2 . . .m− 1},

Pr

⋂i

h(xi) = vi

=1

mk

(where h is picked uniformly at random from H)

Page 159: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

A word about the assumptions

We have assumed true randomness. As we have discussed, this is not realistic.

We have seen that weakly universal hash families are realistic

We can define a stronger hash families with k-wise independence.

here the hash values of any choice of k keys are independent.

where any two keys x, y are independent

Page 160: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

A word about the assumptions

We have assumed true randomness. As we have discussed, this is not realistic.

We have seen that weakly universal hash families are realistic

We can define a stronger hash families with k-wise independence.

here the hash values of any choice of k keys are independent.

where any two keys x, y are independent

Page 161: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

A word about the assumptions

We have assumed true randomness. As we have discussed, this is not realistic.

We have seen that weakly universal hash families are realistic

We can define a stronger hash families with k-wise independence.

here the hash values of any choice of k keys are independent.

where any two keys x, y are independent

It is feasible to construct a (logn)-wise independent family of hash functions

such that h(x) can be computed in O(1) time

Page 162: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

A word about the assumptions

We have assumed true randomness. As we have discussed, this is not realistic.

We have seen that weakly universal hash families are realistic

We can define a stronger hash families with k-wise independence.

here the hash values of any choice of k keys are independent.

where any two keys x, y are independent

It is feasible to construct a (logn)-wise independent family of hash functions

such that h(x) can be computed in O(1) time

By changing the cuckoo hashing algorithm to perform a rehash after logn movesit can be shown (via a similar but harder proof) that the results still hold

Page 163: Hashing part threepeople.cs.bris.ac.uk/~clifford/coms31900-2020/slides/cuckoo-hashin… · Back to the start (again) A dynamic dictionary stores (key; value)-pairs and supports: Universe

A word about the assumptions

We have assumed true randomness. As we have discussed, this is not realistic.

We have seen that weakly universal hash families are realistic

We can define a stronger hash families with k-wise independence.

here the hash values of any choice of k keys are independent.

where any two keys x, y are independent

THEOREM

In the Cuckoo hashing scheme:

• Every lookup and every delete takes O(1) worst-case time,

• The space is O(n) where n is the number of keys stored

• An insert takes amortised expected O(1) time

It is feasible to construct a (logn)-wise independent family of hash functions

such that h(x) can be computed in O(1) time

By changing the cuckoo hashing algorithm to perform a rehash after logn movesit can be shown (via a similar but harder proof) that the results still hold