{"id":1850,"date":"2019-01-01T12:00:00","date_gmt":"2019-01-01T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/cuckoo-hashing\/"},"modified":"2026-07-05T03:20:24","modified_gmt":"2026-07-05T01:20:24","slug":"cuckoo-hashing","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/cuckoo-hashing\/","title":{"rendered":"How Cuckoo Hashing Works"},"content":{"rendered":"<p>The Cuckoo hashing algorithm allows for lookup of items at constant time, O(1). We would cover the following in this article:<\/p>\n<ol>\n<li><a href=\"#t1\">How Cuckoo Hashing Works<\/a><\/li>\n<li><a href=\"#t2\">The Cuckoo Hashing Algorithm<\/a><\/li>\n<li><a href=\"#t3\">The Cuckoo Graph<\/a><\/li>\n<li><a href=\"#t4\">Cycles in the Cuckoo Graph<\/a><\/li>\n<li><a href=\"#t5\">Watch the Video<\/a><\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-345 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Introduction-to-Cuckoo-Hashing-Text-300x150.jpg\" alt=\"\" width=\"300\" height=\"150\" \/><br \/>\n<strong id=\"t1\">1. How the Cuckoo Hashing Works<\/strong><\/p>\n<p>In cuckoo hashing, we maintain two hash position (and hash functions) h1 and h2.\u00a0 So if we need to store a key x, we would compute one of two postions h1(x) and h2(x).<\/p>\n<p>This means that to retrieve an item, we simply need to lookup just two positions in the table. This is shown in Table 1.0.<\/p>\n<figure id=\"attachment_341\" aria-describedby=\"caption-attachment-341\" style=\"width: 552px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-341\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Cuckoo-Hash-Table.jpg\" alt=\"\" width=\"552\" height=\"239\" \/><figcaption id=\"caption-attachment-341\" class=\"wp-caption-text\">Figure 1.0: Cuckoo hash table. The arrow directions shown the alternative location of each of the items in the table. If a new item is to be inserted into position occupied by A, then A is ejected and moved to its second alternative position. Insertion in position of H would lead to a cycle and would require to rehash the entire table.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>To insert an Item, we first try to insert the item into position h1(x). If the position is empty, we insert the item. But if it happens that the position if occupied, we throw out the existing key in that location and insert x. (This is the behaviour of the Cuckoo). Assuming the ejected key is y, then y is place in the alternative position which is h2(y). If also this position is occupied by a key z, the y repeats the same treatment: throws out z to make room. This process continues until a given threshold is reached.<\/p>\n<p>In situation like this, if cycles are encountered, then we choose new has functions h1() and h2 and rehash or rebuild the entire table.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t2\">2. The Cuckoo Hashing Algorithm<\/strong><\/p>\n<p>The cuckoo hashing algorithm is given in Listing 1.0. The variable pos is used to maintain the current position where we are currently trying to insert an item.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">Procedure<\/span> insert (x)\n   <span style=\"color: #008800; font-weight: bold;\">if<\/span>(<span style=\"color: #333399; font-weight: bold;\">T<\/span>[h1(x) <span style=\"color: #000000; font-weight: bold;\">=<\/span> x or <span style=\"color: #333399; font-weight: bold;\">T<\/span>[h2(x) <span style=\"color: #000000; font-weight: bold;\">=<\/span> x \n\t   <span style=\"color: #008800; font-weight: bold;\">then<\/span> return\n   <span style=\"color: #008800; font-weight: bold;\">else<\/span>\n\t pos gets h1(x)\n\t <span style=\"color: #333399; font-weight: bold;\">Loop<\/span> n times {\n\t\t<span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333399; font-weight: bold;\">T<\/span>[pos] <span style=\"color: #000000; font-weight: bold;\">=<\/span> <span style=\"color: #333399; font-weight: bold;\">NULL<\/span>\n\t\t   <span style=\"color: #008800; font-weight: bold;\">then<\/span> <span style=\"color: #333399; font-weight: bold;\">T<\/span>[pos] gets x;\n\t\t   return\n\t\t<span style=\"color: #008800; font-weight: bold;\">else<\/span>\n\t\t   swap x and <span style=\"color: #333399; font-weight: bold;\">T<\/span>[pos]\n\t\t   <span style=\"color: #008800; font-weight: bold;\">if<\/span> pos <span style=\"color: #000000; font-weight: bold;\">=<\/span> h1(x) <span style=\"color: #008800; font-weight: bold;\">then<\/span> post gets h2(x) <span style=\"color: #008800; font-weight: bold;\">else<\/span> post gets h1(x)\n\t   rehash<span style=\"color: #007020;\">()<\/span>; insert(x)\n<span style=\"color: #333399; font-weight: bold;\">End<\/span>\n<\/pre>\n<\/div>\n<p>Listing 1.0: Cuckoo Hashing Algorithm<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t3\">3. The Cuckoo Graph<\/strong><\/p>\n<p>A cuckoo graph is formed from the movement of the keys between alternative positions in the hash table. This can be created from Figure 1.0. In the figure, the direction of the arrow on each of the edges indicate that the element can move along the edge to its alternative position<\/p>\n<p>When inserting an item x, the insertion algorithm will only visit positions to which there is a path in the cuckoo graph from either of the two positions h1(x) and h2(x). In the event of a loop, then we go ahead to rehash.<\/p>\n<p>Analysis of the cuckoo graph shows that insertion takes constant time in the absence\u00a0 of a rehashing.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t4\">4. Cycles in Cuckoo\u00a0<\/strong><b>Graph<\/b><\/p>\n<p>Assuming we want to insert a key Z. We would examine the\u00a0 two possible position h1(Z) and h2(Z) which are called buckets. Let&#8217;s also assume that h1(Z) hashes to W. Then then:<\/p>\n<ul>\n<li>W is moved to position of H<\/li>\n<li>H is moved to position of of H and this becomes a cycle which would require rehashing the table<\/li>\n<\/ul>\n<p>If however h1(Z) hashes to the position of W and W moves to the position of A. Then a can move to position of B and B moves to an empty slot. In this case, there is not cycles.<\/p>\n<p><strong id=\"t4\">5. Demo on the Cuckoo Hashing<\/strong><br \/>\n<iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/J12X51Q-Ocs\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<h5><strong>Check these Data Structure and Algorithms Tutorials<\/strong><\/h5>\n<ul>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/skip-lists-and-how-they-work\/\">Skip Lists and How They Work<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/perfect-hashing\/\">Perfect Hashing \u2013 How it Works<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/hashing\/\">Linear Probing, Quadratic Probing and Double Hashing<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/hashing-with-open-addressing\/\">Hashing With Open Addressing<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/universal-hashing\/\">Universal Hashing<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/search-time-under-simple-uniform-hashing\/\">Search Time Under Simple Uniform Hashing<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/hash-tables-hashing-with-chaining\/\">Hash Tables \u2013 Hashing With Chaining<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/introduction-to-hash-tables-and-direct-addressing\/\">Introduction to Hash Tables and Direct Addressing<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/recurrences-in-divide-and-conquer-algorithm\/\">Recurrences in Divide and Conquer Algorithm<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/how-bloom-filters-work\/\">How Bloom Filters Work<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/cuckoo-hashing\/\">How Cuckoo Hashing Works<\/a><\/strong><\/li>\n<li><strong><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/introduction-to-avl-trees-with-examples\/\">Introduction to AVL Trees (With Examples)<\/a><\/strong><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The Cuckoo hashing algorithm allows for lookup of items at constant time, O(1). We would cover the following in this article: How Cuckoo Hashing Works &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[35],"tags":[],"class_list":["post-1850","post","type-post","status-publish","format-standard","hentry","category-algorithms"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1850"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1850\/revisions"}],"predecessor-version":[{"id":2019,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1850\/revisions\/2019"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}