<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>StaticMethod &#187; hashing</title>
	<atom:link href="http://www.staticmethod.net/tag/hashing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.staticmethod.net</link>
	<description>Things programmers might find interesting</description>
	<lastBuildDate>Thu, 04 Mar 2010 20:05:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cuckoo Hash</title>
		<link>http://www.staticmethod.net/2009/05/12/cuckoo-hash/</link>
		<comments>http://www.staticmethod.net/2009/05/12/cuckoo-hash/#comments</comments>
		<pubDate>Tue, 12 May 2009 17:44:41 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Data and Algorithms]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Programming Problems]]></category>
		<category><![CDATA[cuckoo hash]]></category>
		<category><![CDATA[hashing]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.staticmethod.net/?p=65</guid>
		<description><![CDATA[From Wikipedia:
Cuckoo hashing is a scheme in computer programming for resolving hash collisions of values of hash functions in a table
Very basically, a Cuckoo hash is a table, where the position of an element can be one of two places.  On insertion, if there is another element (call it &#8216;y&#8217;) in the candidate spot, it [...]]]></description>
			<content:encoded><![CDATA[<p>From <a href="http://en.wikipedia.org/wiki/Cuckoo_hashing">Wikipedia</a>:</p>
<blockquote><p><strong>Cuckoo hashing</strong> is a scheme in computer programming for resolving <span class="mw-redirect">hash collisions</span> of values of hash functions in a table</p></blockquote>
<p>Very basically, a Cuckoo hash is a table, where the position of an element can be one of two places.  On insertion, if there is another element (call it &#8216;y&#8217;) in the candidate spot, it is replaced by the item you are inserting.  Then you re-add the &#8216;y&#8217; element to the hash, using the second hash method.  Doing this could cause another element (call it &#8216;z&#8217;) to be displaced, in which case the cycle repeats until all elements are in their new spots.</p>
<p>To look up if an element is in the table, you need to check both possible spots.</p>
<p>You can run into a problem with cycles when inserting values if, for example,  <em>h1(x) = h2(y)</em> and  <em>h2(x) = h1(y)</em>, but you can limit the number of re-insertions to avoid this.  If you reach this limit, you can change your hashing methods and re-insert everything again. This problem happens even more frequently if the load factor on your table is too high, so you can put checks in place to increase the size of the table if you detect that the load is getting high.  In my example, I set the top table load factor to 60%, at which point I double the size of the table and re-insert the elements.</p>
<p>The main problem I&#8217;m running into is that the <em>insert</em> method can call the <em>rehash</em> method if it loops too many times trying to place everything&#8230;but <em>rehash</em> calls <em>insert</em> to rebuild the table with new hashing methods.  This seems to lead to deeply recursive problems, even with a load set to 50%.  I&#8217;m not sure how to fix this right now, but I&#8217;ll keep working at it and take any suggestions.  One method might be to find better hashing algorithms, but I thought the one I was using was descent.</p>
<p>Anyway, since I&#8217;m still learning about it, here&#8217;s a few links to read:</p>
<ul>
<li><a class="external text" title="http://www.it-c.dk/people/pagh/papers/cuckoo-undergrad.pdf" rel="nofollow" href="http://www.it-c.dk/people/pagh/papers/cuckoo-undergrad.pdf">Cuckoo Hashing for Undergraduates, 2006</a>, R. Pagh, 2006. (PDF)</li>
<li><a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/142054">Some String hashing algorithms</a></li>
<li><a rel="attachment wp-att-67" href="http://www.staticmethod.net/2009/05/12/cuckoo-hash/cuckoo_hash/">Cuckoo Hash Attempt 1</a> &#8211; My first attempt at implementing a Cuckoo Hash in Ruby</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.staticmethod.net/2009/05/12/cuckoo-hash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
