Cuckoo hashing is a scheme in
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
for resolving
hash collision
In computer science, a hash collision or hash clash is when two 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 bits.
Al ...
s of values of
hash function
A hash function is any function that can be used to map data of arbitrary size to fixed-size values. The values returned by a hash function are called ''hash values'', ''hash codes'', ''digests'', or simply ''hashes''. The values are usually ...
s in a
table
Table may refer to:
* Table (furniture), a piece of furniture with a flat surface and one or more legs
* Table (landform), a flat area of land
* Table (information), a data arrangement with rows and columns
* Table (database), how the table data ...
, with
worst-case
In computer science, best, worst, and average cases of a given algorithm express what the resource usage is ''at least'', ''at most'' and ''on average'', respectively. Usually the resource being considered is running time, i.e. time complexity, b ...
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 sometimes separat ...
, 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 parasites are 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 same or of another species, to raise its young as if it were its ow ...
; 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
In computing, a hash table, also known as hash map, is a data structure that implements an associative array or dictionary. It is an abstract data type that maps keys to values. A hash table uses a hash function to compute an ''index'', ...
contains a
key or
key–value pair. A
hash function
A hash function is any function that can be used to map data of arbitrary size to fixed-size values. The values returned by a hash function are called ''hash values'', ''hash codes'', ''digests'', or simply ''hashes''. The values are usually ...
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
In physics, a collision is any event in which two or more bodies exert forces on each other in a relatively short time. Although the most common use of the word ''collision'' refers to incidents in which two or more objects collide with great fo ...
, 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
and assuming
be the length of each tables, the hash functions for the two tables is defined as,
and
where
be the key and
be the set whose keys are stored in
of
or
of
. The lookup operation is as follows:
The
logical or
In logic, disjunction is a logical connective typically notated as \lor and read aloud as "or". For instance, the English language sentence "it is raining or it is snowing" can be represented in logic using the disjunctive formula R \lor S ...
(
) denotes that, the value of the key
is found in either
or
, which is
in worst case.
Deletion
Deletion is performed in
since there isn't involvement of probing—not considering the cost of shrinking operation if table is too sparse.
Insertion
Insertion of a new
item
Item may refer to:
Organizations
* '' Instituto del Tercer Mundo'' (ITeM), the Third World Institute
* ITEM club, an economic forecasting group based in the United Kingdom
Newspapers
* ''The Item'', an American independent, morning newspape ...
, the first step involves examining if the slot
of the table
is occupied; if it is not, the item is inserted at that cell. However, if the slot is occupied, the preoccupied item gets removed—let it be
—and
is inserted at