Docsity

Cuckoo Hashing, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Cuckoo Hashing and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Cuckoo Hashing

Problem Set 6 and Final Project Proposals are due in the box up front. Problem Set 6 and Final Project Proposals are due in the box up front.

Outline for Today

●

Review of Chained Hash Tables

●

A quick refresher on simple and efficient

hash tables.

●

Cuckoo Hashing

●

Hashing with worst-case O(1) lookups.

●

The Cuckoo Graph

●

A framework for analyzing cuckoo hashing.

●

Analysis of Cuckoo Hashing

●

Just how fast is cuckoo hashing?

Chained Hash Tables

● (^) A chained hash table is a hash table in which collisions are resolved by placing all colliding elements into the same bucket. ● (^) To determine whether an element is present, hash to its bucket and scan for it. ● (^) Insertions and deletions are generalizations of lookups. Calliope Clio Polyhymnia Urania Erato Thalia Terpsichore Euterpe Melpomene

Chained Hash Tables

● The load factor of a chained hash table is the ratio of the number of elements ( n ) to the number of buckets ( m ). ● Typically denoted α = n / m. ● By doubling the table size whenever α exceeds two, α can be kept low with amortized O(1) work per insertion. ● With universal hash functions, the expected, amortized cost of a lookup is O(1).

Worst-Case Efficient Hashing

●

Question: Is it possible to design a hash

table where lookups are worst-case O(1)?

●

This is challenging – we have to know

exactly where to look to find the element

we need, but there may be collisions!

●

Many techniques for this have been

developed over the years.

●

Check CLRS 11.5 for one such approach.

Cuckoo Hashing

● Cuckoo hashing is a simple hash table where ● Lookups are worst-case O(1). ● Deletions are worst-case O(1). ● Insertions are amortized, expected O(1). ● (^) Insertions are amortized O(1) with reasonably high probability. ● Today, we'll explore cuckoo hashing and work through the analysis.

Cuckoo Hashing

● (^) Lookups take time O(1) because only two locations must be checked. ● (^) Deletions take time O(1) because only two locations must be checked. 32 93 58 84 59 97 23 53 26 41 T ₁ T ₂

Cuckoo Hashing

● (^) To insert an element x , start by inserting it into table 1. ● (^) If h ₁( x ) is empty, place x there. ● Otherwise, place x there, evict the old element y , and try placing y into table 2. ● Repeat this process, bouncing between tables, until all elements stabilize. 53 10 58 6 75 97 23 84 26 32 93 T ₁ T ₂

A Note on Cycles

● It's possible for a successful insertion to revisit the same slot twice. ● Cycles only arise if we revisit the same slot with the same element to insert. 58 75 97 23 84 26 32 93 T ₁ T ₂ 53 10 6 91

Analyzing Cuckoo Hashing

●

Cuckoo hashing can be tricky to analyze

for a few reasons:

●

Elements move around and can be in one of

two different places.

●

The sequence of displacements can jump

chaotically over the table.

●

It turns out there's a beautiful framework

for analyzing cuckoo hashing.

The Cuckoo Graph

●

An insertion in a

cuckoo hash table

traces a path through

the cuckoo graph.

●

An insertion succeeds

iff the connected

component

containing the

inserted value

contains at most one

cycle.

91 10 75 19 26 32 6 T ₁ T ₂ 53 16 88 58 16 10 58 91 53 6 26 19 88

The Cuckoo Graph

●

An insertion in a

cuckoo hash table

traces a path through

the cuckoo graph.

●

An insertion succeeds

iff the connected

component

containing the

inserted value

contains at most one

cycle.

91 10 75 19 26 32 6 T ₁ T ₂ 53 16 88 58 16 10 91 53 6 26 19 88 4 4 58

The Cuckoo Graph

●

Claim 2: If x is inserted into a cuckoo hash

table, the insertion succeeds if the connected

component containing x contains either no

cycles or only one cycle.

Each edge is an element and each node is a bucket. The arrows show which bucket each element belongs to. Each edge is an element and each node is a bucket. The arrows show which bucket each element belongs to.

The Cuckoo Graph

●

Claim 2: If x is inserted into a cuckoo hash

table, the insertion succeeds if the connected

component containing x contains either no

cycles or only one cycle.

If there are no cycles, eventually the displacement chain comes to rest. If there are no cycles, eventually the displacement chain comes to rest.