Cuckoo hashing is a scheme in
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
for resolving
hash collision
In computer science, a hash collision or hash clash is when two distinct pieces of data in a hash table share the same hash value. The hash value in this case is derived from a hash function which takes a data input and returns a fixed length of ...
s of values of
hash function
A hash function is any Function (mathematics), function that can be used to map data (computing), data of arbitrary size to fixed-size values, though there are some hash functions that support variable-length output. The values returned by a ...
s in a
table
Table may refer to:
* Table (database), how the table data arrangement is used within the databases
* Table (furniture), a piece of furniture with a flat surface and one or more legs
* Table (information), a data arrangement with rows and column ...
, with
worst-case constant lookup time. The name derives from the behavior of some species of
cuckoo
Cuckoos are birds in the Cuculidae ( ) family, the sole taxon in the order Cuculiformes ( ). The cuckoo family includes the common or European cuckoo, roadrunners, koels, malkohas, couas, coucals, and anis. The coucals and anis are somet ...
, 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
Brood parasitism is a subclass of parasitism and phenomenon and behavioural pattern of animals that rely on others to raise their young. The strategy appears among birds, insects and fish. The brood parasite manipulates a host, either of the ...
; 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
The European Symposium on Algorithms (ESA) is an international conference covering the field of algorithms. It has been held annually since 1993, typically in early Autumn in a different European location each year. Like most theoretical computer ...
Test-of-Time award in 2020.
Operations
Cuckoo hashing is a form of
open addressing
Open addressing, or closed hashing, is a method of Hash table#Collision resolution, collision resolution in hash tables. With this method a hash collision is resolved by probing, or searching through alternative locations in the array (the ''prob ...
in which each non-empty cell of a
hash table
In computer science, a hash table is a data structure that implements an associative array, also called a dictionary or simply map; an associative array is an abstract data type that maps Unique key, keys to Value (computer science), values. ...
contains a
key or
key–value pair. A
hash function
A hash function is any Function (mathematics), function that can be used to map data (computing), data of arbitrary size to fixed-size values, though there are some hash functions that support variable-length output. The values returned by a ...
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,
and
. Assuming
is the length of each table, the hash functions for the two tables is defined as,
and
where
is the key and
is the set whose keys are stored in
of
or
of
. The lookup operation is as follows:
The
logical or
In logic, disjunction (also known as logical disjunction, logical or, logical addition, or inclusive disjunction) is a logical connective typically notated as \lor and read aloud as "or". For instance, the English language, English language ...
(
) denotes that, the value of the key
is found in either
or
, which is
in worst case.
Deletion
Deletion is performed in
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
, the first step involves examining if slot
of table
is occupied. If it is not, the item is inserted in that slot. However, if the slot is occupied, the existing item
is removed and
is inserted at