Outline for Today. Towards Perfect Hashing. Cuckoo Hashing. The Cuckoo Graph. Analysis of Cuckoo Hashing. Reducing worst-case bounds

Similar documents
Outline for Today. A quick refresher on simple and efficient hash tables. Hashing with worst-case O(1) lookups.

Data and File Structures Laboratory

Outline for Today. How can we speed up operations that work on integer data? A simple data structure for ordered dictionaries.

Outline for Today. How can we speed up operations that work on integer data? A simple data structure for ordered dictionaries.

Outline for Today. Euler Tour Trees Revisited. The Key Idea. Dynamic Graphs. Implementation Details. Dynamic connectivity in forests.

x-fast and y-fast Tries

Cuckoo Hashing for Undergraduates

x-fast and y-fast Tries

Introduction. hashing performs basic operations, such as insertion, better than other ADTs we ve seen so far

Chapter 5 Hashing. Introduction. Hashing. Hashing Functions. hashing performs basic operations, such as insertion,

Hashing Based Dictionaries in Different Memory Models. Zhewei Wei

Module 5: Hashing. CS Data Structures and Data Management. Reza Dorrigiv, Daniel Roche. School of Computer Science, University of Waterloo

Range Minimum Queries Part Two

Hash-Based Indexing 1

Skill 1: Multiplying Polynomials

Balanced Trees Part Two

Worst-case running time for RANDOMIZED-SELECT

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/27/18

CS103 Spring 2018 Mathematical Vocabulary

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/18/14

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 16 Treaps; Augmented BSTs

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2

Hashing. Hashing Procedures

CS:3330 (22c:31) Algorithms

CSI33 Data Structures

9/24/ Hash functions

Fundamental Graph Algorithms Part Three

CS 3114 Data Structures and Algorithms READ THIS NOW!

Lecture 4. Hashing Methods

Bipartite Perfect Matching in O(n log n) Randomized Time. Nikhil Bhargava and Elliot Marx

Fast Lookup: Hash tables

CS161 Handout 07 Summer 2013 July 17, 2013 Guide to Reductions. Thanks to Julie Tibshirani for helping with this handout!

CSE373: Data Structures & Algorithms Lecture 17: Hash Collisions. Kevin Quinn Fall 2015

Outline for Today. Why could we construct them in time O(n)? Analyzing data structures over the long term.

Hashing Techniques. Material based on slides by George Bebis

Lecture 11: Perfect Hashing

Turing Machines Part Two

Open Addressing: Linear Probing (cont.)

Outline for Today. Analyzing data structures over the long term. Why could we construct them in time O(n)? A simple and elegant queue implementation.

Hash Tables. Hashing Probing Separate Chaining Hash Function

UNIT III BALANCED SEARCH TREES AND INDEXING

CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course

DATA STRUCTURES AND ALGORITHMS

Introduction hashing: a technique used for storing and retrieving information as quickly as possible.

Binary Search Trees. Carlos Moreno uwaterloo.ca EIT

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

III Data Structures. Dynamic sets

Solution READ THIS NOW! CS 3114 Data Structures and Algorithms

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Dynamic Dictionaries. Operations: create insert find remove max/ min write out in sorted order. Only defined for object classes that are Comparable

COMP3121/3821/9101/ s1 Assignment 1

Dartmouth Computer Science Technical Report TR Chain Match: An Algorithm for Finding a Perfect Matching of a Regular Bipartite Multigraph

CSCI Analysis of Algorithms I

Dictionary. Dictionary. stores key-value pairs. Find(k) Insert(k, v) Delete(k) List O(n) O(1) O(n) Sorted Array O(log n) O(n) O(n)

Unique Permutation Hashing

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

SFU CMPT Lecture: Week 8

Binary Search Trees. Carlos Moreno uwaterloo.ca EIT

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

Outline. hash tables hash functions open addressing chained hashing

Hash Tables. Gunnar Gotshalks. Maps 1

Symbol Table. Symbol table is used widely in many applications. dictionary is a kind of symbol table data dictionary is database management

The following content is provided under a Creative Commons license. Your support

Randomized Algorithms Part Four

Tables. The Table ADT is used when information needs to be stored and acessed via a key usually, but not always, a string. For example: Dictionaries

General Idea. Key could be an integer, a string, etc e.g. a name or Id that is a part of a large employee structure

Randomized Algorithms Part Four

Solutions to Exam Data structures (X and NV)

MITOCW watch?v=zm5mw5nkzjg

Lecture 11: Packet forwarding

Hash table basics mod 83 ate. ate. hashcode()

lecture23: Hash Tables

MITOCW watch?v=w_-sx4vr53m

MITOCW watch?v=yarwp7tntl4

Optimization I : Brute force and Greedy strategy

Data Structures. Topic #6

Intro. Speed V Growth

MITOCW watch?v=v3omvlzi0we

Data and File Structures Chapter 11. Hashing

HASH TABLES. Goal is to store elements k,v at index i = h k

CPSC W2 Midterm #2 Sample Solutions

CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS

Friday Four Square! 4:15PM, Outside Gates

Lecture 8 13 March, 2012

We assume uniform hashing (UH):

CSE100. Advanced Data Structures. Lecture 21. (Based on Paul Kube course materials)

Solutions. Suppose we insert all elements of U into the table, and let n(b) be the number of elements of U that hash to bucket b. Then.

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

CSC263 Week 5. Larry Zhang.

Lecture 17. Improving open-addressing hashing. Brent s method. Ordered hashing CSE 100, UCSD: LEC 17. Page 1 of 19

More on Hashing: Collisions. See Chapter 20 of the text.

Fundamentals of Database Systems Prof. Arnab Bhattacharya Department of Computer Science and Engineering Indian Institute of Technology, Kanpur

HashTable CISC5835, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Fall 2018

Hash table basics. ate à. à à mod à 83

Hi everyone. Starting this week I'm going to make a couple tweaks to how section is run. The first thing is that I'm going to go over all the slides

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Instructor: Craig Duckett. Lecture 04: Thursday, April 5, Relationships

CS 3114 Data Structures and Algorithms READ THIS NOW!

HASH TABLES. Hash Tables Page 1

Transcription:

Cuckoo Hashing

Outline for Today Towards Perfect Hashing Reducing worst-case bounds 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?

Perfect Hashing

Collision Resolution Last time, we mentioned three general strategies for resolving hash collisions: Closed addressing: Store all colliding elements in an auxiliary data structure like a linked list or BST. Open addressing: Allow elements to overflow out of their target bucket and into other spaces. Perfect hashing: Choose a hash function with no collisions. We have not spoken on this last topic yet.

Why Perfect Hashing is Hard The expected cost of a lookup in a chained hash table is O(1 + α) for a) for any load factor α) for a. For any fixed load factor α) for a, the expected cost of a lookup in linear probing is O(1), where the constant depends on α) for a. However, the expected cost of a lookup in these tables is not the same as the expected worst-case cost of a lookup in these tables.

Expected Worst-Case Bounds Theorem: Assuming truly random hash functions, the expected worst-case cost of a lookup in a chained hash table is Θ(log n / log log n). Theorem: Assuming truly random hash functions, the expected worst-case cost of a lookup in a linear probing hash table is Ω(log n). Proofs: Exercise 11-1 and 11-2 from CLRS.

Perfect Hashing A perfect hash table is one where lookups take worst-case time O(1). There's a pretty sizable gap between the expected worst-case bounds from chaining and linear probing and that's on expected worst-case, not worst-case. We're going to need to use some more creative techniques to close this gap.

Multiple-Choice Hashing

Second-Choice Hashing Suppose that we distribute n balls into m urns using the following strategy: For each ball, choose two urns totally at random. Place the ball into the urn with fewer balls in it. Theorem: The expected value of the maximum number of balls in any urn is Θ(log log n). Proof: Nontrivial; see Balanced Allocations by Azar et al. The math involved is tricky but interesting check it out if you're curious!

Second-Choice Hashing Imagine we build a chained hash table with two hash functions h₁ and h₂. To insert an element x, compute h₁(x) and h₂(x) and place x into whichever bucket is less full. To perform a lookup, compute h₁(x) and h₂(x) and search both buckets for x. Theorem: The expected cost of a lookup in such a hash table is O(1 + α) for a). This is certainly no worse than chained hashing. Theorem: Assuming truly random hash functions, the expected worst-case cost of a lookup in such a hash table is O(log log n). Open problem: What is the smallest k for which there are k-independent hash functions that match the bounds using truly random hash functions?

Hashing with Relocation

Robin Hood Hashing Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed. When an existing key is found during an insertion that's closer to its home location than the new key, it's displaced to make room for it. This dramatically decreases the variance in the expected number of lookups. It also makes it possible to terminate searches early. 14 14 12 13 12 12 11 10 15 9 0 0 13 8 1 7 1 2 6 1 3 4 5 2 4

Robin Hood Hashing Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed. When an existing key is found during an insertion that's closer to its home location than the new key, it's displaced to make room for it. This dramatically decreases the variance in the expected number of lookups. It also makes it possible to terminate searches early. 14 0 1 13 15 0 1 14 12 13 12 12 11 10 9 8 7 2 6 1 3 4 5 2 4

Robin Hood Hashing Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed. When an existing key is found during an insertion that's closer to its home location than the new key, it's displaced to make room for it. This dramatically decreases the variance in the expected number of lookups. It also makes it possible to terminate searches early. 14 0 1 13 15 0 1 14 12 13 12 12 15 11 10 9 8 7 2 6 1 3 4 5 2 4

Robin Hood Hashing Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed. When an existing key is found during an insertion that's closer to its home location than the new key, it's displaced to make room for it. This dramatically decreases the variance in the expected number of lookups. It also makes it possible to terminate searches early. 14 15 0 13 15 0 1 14 12 13 12 12 11 10 9 8 7 2 6 1 3 4 5 1 4 2

Robin Hood Hashing Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed. When an existing key is found during an insertion that's closer to its home location than the new key, it's displaced to make room for it. This dramatically decreases the variance in the expected number of lookups. It also makes it possible to terminate searches early. 14 0 0 13 15 0 1 14 12 13 12 12 15 11 10 9 8 7 2 6 1 3 4 5 2 4

Robin Hood Hashing Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed. When an existing key is found during an insertion that's closer to its home location than the new key, it's displaced to make room for it. This dramatically decreases the variance in the expected number of lookups. It also makes it possible to terminate searches early. 13 14 14 0 0 15 0 1 15 12 13 12 12 11 10 9 7 8 2 6 1 3 4 5 2 4

Robin Hood Hashing Robin Hood hashing is a variation of open addressing where keys can be moved after they're placed. When an existing key is found during an insertion that's closer to its home location than the new key, it's displaced to make room for it. This dramatically decreases the variance in the expected number of lookups. It also makes it possible to terminate searches early. 0 14 0 0 13 15 1 15 14 12 13 12 12 11 10 9 7 8 2 6 1 3 4 5 2 4

Robin Hood Hashing Theorem: The expected cost of a lookup in Robin Hood hashing, using 5-independent hashing, is O(1), assuming a constant load factor. Proof idea: Each element is hashed into the same run as it would have been hashed to in linear probing. We bounded the cost of a lookup in linear probing by looking at the cost of the run the element was in, so the analysis still upperbounds the cost. 14 0 0 13 15 0 1 14 12 13 12 12 11 10 9 8 7 2 6 1 3 4 5 2 4

Robin Hood Hashing Theorem: Assuming truly random hash functions, the variance of the expected number of probes required in Robin Hood hashing is O(log log n). Proof: Tricky; see Celis' Ph.D thesis. 14 0 0 13 15 0 1 14 12 13 12 12 11 10 9 8 7 2 6 1 3 4 5 2 4

Where We Stand We now have two interesting ideas that might be fun to combine: Second-choice hashing: Give each element in a hash table two choices of where to go, and put it at the leastloaded location. Relocation hashing: Allow objects in a hash table to move after being placed. Each idea, individually, exponentially decreases the worst-case cost of a lookup by decreasing the variance in the element distribution. What happens if we combine these ideas together?

Cuckoo Hashing

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); and insertions are amortized O(1) with reasonably high probability. Today, we'll explore cuckoo hashing and work through the analysis.

Cuckoo Hashing Maintain two tables, each of which has m elements. We choose two hash functions h₁ and h₂ from Uto [ to [ m]. Every element x Uto [ will either be at position h₁(x) in the first table or h₂(x) in the second. 32 84 59 93 58 T₁ 97 26 41 23 53 T₂

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 84 59 93 58 97 26 41 23 53 T₁ T₂

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 84 59 93 58 97 26 41 23 53 T₁ T₂

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 84 59 93 58 97 26 23 53 T₁ T₂

Cuckoo Hashing To insert an element x, start by inserting it into table 1. 32 97 If h₁(x) is empty, place x there. 84 26 Otherwise, place x there, evict the old element y, and try placing y into table 2. 93 58 75 23 Repeat this process, bouncing between tables, until all elements stabilize. T₁ 53 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. 32 84 75 93 58 T₁ 97 26 23 53 T₂

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

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

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

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

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

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

Cuckoo Hashing Insertions run into trouble if we run into a cycle. If that happens, perform a rehash by choosing a new h₁ and h₂ and inserting all elements back into the tables. Multiple rehashes might be necessary before this succeeds. 10 32 91 97 58 75 93 23 53 6 26 84 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. 10 32 91 59 97 75 18 93 23 53 6 26 T₁ 84 T₂

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 The cuckoo graph is a bipartite multigraph derived from a cuckoo hash table. Each table slot is a node. 19 16 32 10 19 16 91 58 10 91 Each element is an edge. 58 75 Edges link slots where each element can be. Each insertion introduces a new edge into the graph. 53 26 6 53 26 6 T₁ T₂

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. 19 16 32 58 75 53 26 10 19 16 91 58 6 10 91 6 53 26 88 T₁ T₂

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. 19 91 32 10 75 6 53 26 10 19 16 91 58 88 6 16 58 88 53 26 T₁ T₂

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. 19 91 32 10 75 6 53 26 10 19 16 91 58 4 88 6 16 58 88 53 26 4 T₁ T₂

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. 19 91 32 10 75 6 53 26 10 19 16 91 58 88 6 16 58 88 53 26 4 T₁ T₂ 4

The Cuckoo Graph Claim 1: If x is inserted into a cuckoo hash table, the insertion fails if the connected component containing x has two or more cycles. Proof: Each edge represents an element and needs to be placed in a bucket. If the number of nodes (buckets) in the CC is k, then there must be at least k + 1 elements (edges) in that CC to have two cycles. Therefore, there are too many nodes to place into the buckets.

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.

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 Each edge edge is is an an element element and and each each node node is is a a bucket. bucket. The The arrows arrows show show which which bucket bucket each each element element belongs belongs to. 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.

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.

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.

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 If there there are are no no cycles, cycles, eventually the the displacement chain chain comes comes to to rest. rest.

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.

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. When When we we arrive arrive back back at at this this node, node, we we can't can't follow follow the the edge edge back back into into the the cycle cycle because because it's it's flipped flipped the the wrong wrong way. way.

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. We We either either stabilize stabilize inside inside the the cycle, cycle, avoid avoid the the cycle, cycle, or or get get kicked kicked out out of of the the cycle. cycle.

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. Claim 3: If x is inserted in a connected component with k nodes, the insertion process does at most 2k displacements.

Terminology A tree is an undirected, connected component with no cycles. A unicyclic component is a connected component with exactly one cycle. A connected component is called complex if it's neither a tree nor unicyclic. Cuckoo hashing fails iff any of the connected components in the cuckoo graph are complex.

The Gameplan To analyze cuckoo hashing, we'll do the following. First, we'll analyze the probability that a connected component is complex. Next, under the assumption that no connected component is complex, we'll analyze the expected cost of an insertion. Finally, we'll put the two together to complete the analysis.

Time-Out for Announcements!

Problem Set Five Problem Set Five goes out today. It's due one week from today at 2:30PM. Play around with randomized data structures, both in theory and in practice! Problem Set Four was due at 2:30PM today.

Back to CS166!

Step One: Exploring the Graph Structure

Exploring the Graph Structure Cuckoo hashing will always succeed in the case where the cuckoo graph has no complex connected components. If there are no complex CC's, then we will not get into a loop and insertion time will depend only on the sizes of the CC's. It's reasonable to ask, therefore, how likely we are to not have complex components.

Tricky Combinatorics Question: What is the probability that a randomly-chosen bipartite multigraph with 2m nodes and n edges will contain a complex connected component? Answer: If m = (1 + ε))n, the answer is Θ(1 / m). Source: Bipartite Random Graphs and Cuckoo Hashing by Reinhard Kutzelnigg.

The Main Result Theorem: If m = (1 + ε))n for some ε) > 0, the probability that the cuckoo graph contains a complex connected component is O(1 / m). I have scoured the literature and cannot seem to find a simple proof of this result. Challenge Problem: Provide a simple proof of this result.

The Implications If m = (1 + ε))n, then the hash table will have a load factor of n / 2m = 1 / (2 + 2ε)). This means that roughly half of the table cells will be empty. Fact: There is an abrupt phase transition in the success probability when the tables get close to half full. The likelihood of getting a complex connected component is extremely high at that point. There are techniques for improving the space usage of cuckoo hashing; more on that later on.

Step Two: Analyzing Connected Components

Analyzing Connected Components The cost of inserting x into a cuckoo hash table is proportional to the size of the CC containing x. Question: What is the expected size of a CC in the cuckoo graph?

The Result Claim: If m (1 + ε))n for any ε) > 0, then on expectation, the cost of an insertion in a cuckoo hash table that does not trigger a rehash is O(1 + ε) -1 ). Proof idea: Show that the expected number of nodes in a connected component is at most 1 + ε) -1. Let's see how to do this!

Sizing a Connected Component Binom(n, 1/m) Binom(n, 1/m) Binom(n, 1/m)

Modeling the DFS Fix a start node v. The number of nodes incident to v is modeled by a Binom(n, 1 / m) variable. For each node u connected to v, we can upper-bound the number of nodes connected to u by a Binom(n, 1 / m) variable.

Subcritical Galton-Watson Processes The process modeled by this tree is called a subcritical Galton-Watson process. Models a tree where each node has a number of children given by i.i.d. copies of some variable ξ. Constraint: E[ξ] must be less than 1.

Subcritical Galton-Watson Processes Denote by Xₙ the number of nodes alive at depth n. This gives a series of random variables X₀, X₁, X₂,. These variables are defined by the following recurrence: X 0 =1 X X = n n+1 ξ i,n i=1 Here, each ξᵢ,ₙ is an i.i.d. copy of ξ.

Subcritical Galton-Watson Processes Lemma: E[Xₙ] = E[ξ] n. Proof: Conditional expectation.

Theorem: The expected size of a connected component in the cuckoo graph is at most 1 + ε) -1. Proof: We can upper-bound the number of nodes in a CC with the number of nodes in a subcritical Galton-Watson process where ξ ~ Binom(n, 1 / m). If we denote by X the total number of nodes in the CC, we see that X= i=0 Therefore, the expected value of X is given by E[ X ] = E[ X i ] = E[ X i ] = E[ξ ] i = i=0 i=0 i=0 Note that E[ξ] = n / m (1 + ε)) -1, so E[ X ] = (1 n 1 m ) (1 1 1 1+ε) ) = 1+ε) ε) = 1+ε) 1 Therefore, the expected size of a CC in the cuckoo graph is at most 1 + ε) -1. X i 1 1 E[ξ ]

Finishing Touches Lemma: The expected cost of a single rehash, assuming that it succeeds, is O(m + nε) -1 ). Proof: If the rehash succeeds, each insertion takes expected time O(1 + ε) -1 ). There are n insertions, so the time will be O(n + ε) -1 n). We also do O(m) work reinitializing the buckets, so the total time is O(m + nε) -1 ). Lemma: The expected cost of a rehash is O(m + nε) -1 ). Proof: Each rehash succeeds with probability 1 O(1 / m). Therefore, on expectation, only 1 / (1 O(1/m)) = O(1) rehashes are necessary. Since each one takes expected time O(m + nε) -1 ), the expected total time is O(m + nε) -1 ).

Planned Rehashing We need to rehash in two situations: A planned rehash, a rehash to ensure that m (1 + ε))n. This needs to happen periodically to ensure the table grows. An unplanned rehash, where we rehash because there is a complex CC in the table. If we repeatedly double the size of the table, the expected total work done in planned rehashing is O(m + nε) -1 ) across the lifetime of the table. Analysis similar to chained hashing. This amortizes out to expected O(1 + ε) + ε) -1) additional work per insertion.

Theorem: The expected, amortized cost of an insertion into a cuckoo hash table is O(1 + ε) + ε) -1 ). Proof: We have already shown that the amortized overhead of an insertion due to planned rehashing is O(1 + ε) + ε) -1 ), so all we need to do is analyze the expected cost ignoring planned rehashes. With probability 1 O(1 / m), the expected cost is O(1 + ε) + ε) -1 ). With probability O(1 / m), we have to rehash, which takes expected time O(m + nε) -1 ). Therefore, the expected cost of an insert is As required. = O(1 + ε) + ε) -1 ) + O(1 + nε) -1 / m) = O(1 + ε) + ε) -1 ) + O(1 + ε) -1 ) = O(1 + ε) + ε) -1 )

Some Technical Details

A Few Technical Details There are a few technical details we glossed over in this analysis. Stopping time: Typically, cuckoo hashing triggers a rehash as soon as C log n elements have been displaced, for some constant C. If a hash has been going on for that long, then it's almost certainly going to fail. Need to repeat the analysis to show that this addition doesn't cause rehashing with high frequency.

A Few Technical Details There are a few technical details we glossed over in this analysis. Hash function choice: The hash functions chosen need to have a high degree of independence for these results to hold. It's known that 6-independent hashing isn't sufficient, and that O(log n)-independence is. In practice, most simple hash functions will work, though some particular classes do not. See On the Risks of Using Cuckoo Hashing with Simple Universal Hash Classes by Dietzfelbinger et al. for more details.

A Few Technical Details There are a few technical details we glossed over in this analysis. Load Factor: Cuckoo hashing's performance is highly influenced by the load factor. With two tables each of size (1 + ε))n, cuckoo hashing performs quite well and rarely needs to rehash. This leads to an effective load factor of less than ½. At higher load factors, cuckoo hashing tends to perform dramatically worse and frequently needs to rehash. This is due to a phase transition in the structure of the cuckoo graph.

Variations on Cuckoo Hashing

Multiple Tables Cuckoo hashing works well with two tables. So why not 3, 4, 5,, or k tables? In practice, cuckoo hashing with k 3 tables tends to perform much better than cuckoo hashing with k = 2 tables: The load factor can increase substantially; with k=3, it's only around α) for a = 0.91 that you run into trouble with the cuckoo graph. Displacements are less likely to chain together; they only occur when all three hash locations are filled in.

Restricting Moves Insertions in cuckoo hashing only run into trouble when you encounter long chains of displacements during insertions. Idea: Cap the number of displacements at some fixed factor, then store overflowing elements in a secondary hash table. In practice, this works remarkably well, since the auxiliary table doesn't tend to get very large.

Increasing Bucket Sizes What if each slot in a cuckoo hash table can store multiple elements? When displacing an element, choose a random one to move and move it. This turns out to work remarkably well in practice, since it makes it really unlikely that you'll have long chains of displacements.

More to Explore There is another famous dynamic perfect hashing scheme called dynamic FKS hashing. It works by using closed addressing and resolving collisions at the top level with a secondary (static) perfect hash table. In practice, it's not as fast as these other approaches. However, it only requires 2- independent hash functions. Check CLRS for details!

An Interesting Fact Open Problem: Is there a hash table that supports amortized O(1) insertions, deletions, and lookups? You'd think that we'd know the answer to this question, but, sadly, we don't.

Next Time Integer Data Structures Data structures for storing and manipulating integers. x-fast and y-fast Tries Searching in o(log n) time for integers.