ArticleslgStudy

computer science

Cuckoo hashing

Cuckoo hashing is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Cuckoo hashing rather than just read about it. In short: Cuckoo hashing is a scheme in computer programming for resolving hash collisions of values of hash functions in a table, with worst-case constant lookup time. The name derives from the behavior of some species of cuckoo, where the cuckoo chick pushes the other eggs or young out of the nest when it hatches in a variation of the behavior referred to as brood parasitism; analogously, inserting a new key into a cuckoo h…

Cuckoo hashing — main illustration
Cuckoo hashing — illustration

Key takeaways

  • Cuckoo hashing belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Cuckoo hashing to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Cuckoo hashing from memory before moving on to harder problems.

Reference excerpt

Cuckoo hashing is a scheme in computer programming for resolving hash collisions of values of hash functions in a table, with worst-case constant lookup time. The name derives from the behavior of some species of cuckoo, where the cuckoo chick pushes the other eggs or young out of the nest when it hatches in a variation of the behavior referred to as brood parasitism; analogously, inserting a new key into a cuckoo hashing table may push an older key to a different location in the table.

History Cuckoo hashing was first described by Rasmus Pagh and Flemming Friche Rodler in a 2001 conference paper. The paper was awarded the European Symposium on Algorithms Test-of-Time award in 2020.

Operations Cuckoo hashing is a form of open addressing in which each non-empty cell of a hash table contains a key or key–value pair. A hash function is used to determine the location for each key, and its presence in the table (or the value associated with it) can be found by examining that cell of the table. However, open addressing suffers from collisions, which happens when more than one key is mapped to the same cell. The basic idea of cuckoo hashing is to resolve collisions by using two hash functions instead of only one. This provides two possible locations in the hash table for each key. In one of the commonly used variants of the algorithm, the hash table is split into two smaller tables of equal size, and each hash function provides an index into one of these two tables. It is also possible for both hash functions to provide indexes into a single table.

Lookup Cuckoo hashing uses two hash tables, T1 and T2. Assuming r is the length of each table, the hash functions for the two tables is defined as, h1, h2 : S→{0,...,r−1} and ∀x∈S where x is the key and S is the set whose keys are stored in h1(x) of T1 or h2(x) of T2. The lookup operation is as follows:

The logical or (∨) denotes that, the value of the key x is found in either T1 or T2, which is O(1) in worst case.

Deletion Deletion is performed in O(1) time since probing is not involved. This ignores the cost of the shrinking operation if the table is too sparse.

Insertion When inserting a new item with key x, the first step involves examining if slot h1(x) of table T1 is occupied. If it is not, the item is inserted in that slot. However, if the slot is occupied, the existing item x′ is removed and x is inserted at T1[h1(x)]. Then, x′ is inserted into table T2 by following the same procedure. The process continues until an empty position is found to insert the key. To avoid an infinite loop, a threshold Max-Loop is specified. If the number of iterations exceeds this fixed threshold, both T1 and T2 are rehashed with new hash functions and the insertion procedure repeats. The following is pseudocode for insertion:

On lines 10 and 15, the "cuckoo approach" of kicking other keys which occupy T1,2[h1,2(x)] repeats until every key has its own "nest", i.e. item x is inserted into an empty slot in either of the two tables. The notation x↔y expresses swapping x and y.

Theory Insertions succeed in expected constant time, even considering the possibility of having to rebuild the table, as long as the number of keys is kept below half of the capacity of the hash table, i.e., the load factor is below 50%. One method of proving this uses the theory of random graphs: one may form an undirected graph called the "cuckoo graph" that has a vertex for each hash table location, and an edge for each hashed value, with the endpoints of the edge being the two possible locations of the value. Then, the greedy insertion algorithm for adding a set of values to a cuckoo hash table succeeds if and only if the cuckoo graph for this set of values is a pseudoforest, a graph with at most one cycle in each of its connected components. Any vertex-induced subgraph with more edges than vertices corresponds to a set of keys for which there are an insufficient number of slots in the hash table. When the hash function is chosen randomly, the cuckoo graph is a random graph in the Erdős–Rényi model. With high probability, for load factor less than 1/2 (corresponding to a random graph in which the ratio of the number of edges to the number of vertices is bounded below 1/2), the graph is a pseudoforest and the cuckoo hashing algorithm succeeds in placing all keys. The same theory also proves that the expected size of a connected component of the cuckoo graph is small, ensuring that each insertion takes constant expected time. However, also with high probability, a load factor greater than 1/2 will lead to a giant component with two or more cycles, causing the data structure to fail and need to be resized. Since a theoretical random hash function requires too much space for practical usage, an important theoretical question is which practical hash functions suffice for Cuckoo hashing. One approach is to use k-independent hashing. In 2009 it was shown that O(log⁡n)-independence suffices, and at least 6-independence is needed. Another approach is to use tabulation hashing, which is not 6-independent, but was shown in 2012 to have other properties sufficient for Cuckoo hashing. A third approach from 2014 is to slightly modify the cuckoo hashtable with a so-called stash, which makes it possible to use nothing more than 2-independent hash functions.

Practice In practice, cuckoo hashing is about 20–30% slower than linear probing, which is the fastest of the common approaches. The reason is that cuckoo hashing often causes two cache misses per search, to check the two locations where a key might be stored, while linear probing usually causes only one cache miss per search. However, because of its worst case guarantees on search time, cuckoo hashing can still be valuable when real-time response rates are required.

Example The following hash functions are given (the two least significant digits of k in base 11): h(k)=kmod11 h′(k)=⌊k11⌋mod11 The following two tables show the insertion of some example elements. Each column corresponds to the state of the two hash tables over time. The possible insertion locations for each new value are highlighted. The last column illustrates a failed insertion due to a cycle, details below.

Cycle If you attempt to insert the element 45, then you get into a cycle, and fail. In the last row of the table we find the same initial situation as at the beginning again. h(45)=45mod11=1 h′(45)=⌊4511⌋mod11=4

… excerpt ends here. Continue reading the full article.

Illustrations

Cuckoo hashing: Cuckoo hashing example. The arrows show the alternative location of each key. A new item would be inserted in the location of A by moving A to its alternative location, currently occupied by B, and moving B to its alternative location which is currently vacant. Insertion of a new item in the location of H would not succeed: Since H is part of a cycle (together with W), the new item would get kicked out again.
Cuckoo hashing example. The arrows show the alternative location of each key. A new item would be inserted in the location of A by moving A to its alternative location, currently occupied by B, and moving B to its alternative location which is currently vacant. Insertion of a new item in the location of H would not succeed: Since H is part of a cycle (together with W), the new item would get kicked out again.

Worked examples

Example 1 — a first encounter with Cuckoo hashing

Start with the simplest possible case. Write down what Cuckoo hashing claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Cuckoo hashing before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Cuckoo hashing ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Cuckoo hashing

In research
Cuckoo hashing appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Cuckoo hashing in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Cuckoo hashing is common in secondary-school and first-year university syllabi. It links to neighbouring topics Hashing, Search algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Cuckoo hashing outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Cuckoo hashing” →

How to study Cuckoo hashing in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Cuckoo hashing means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Cuckoo hashing out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Cuckoo hashing in simple terms?

Cuckoo hashing is a scheme in computer programming for resolving hash collisions of values of hash functions in a table, with worst-case constant lookup time. The name derives from the behavior of some species of cuckoo, where the cuckoo chick pushes the other eggs or young out of the nest when it…

Why does Cuckoo hashing matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Cuckoo hashing?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Cuckoo hashing.

Tags

  • Hashing
  • Search algorithms

Keep exploring