<?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; ruby</title>
	<atom:link href="http://www.staticmethod.net/tag/ruby/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 &#8211; Part 2</title>
		<link>http://www.staticmethod.net/2009/05/13/cuckoo-hash-part-2/</link>
		<comments>http://www.staticmethod.net/2009/05/13/cuckoo-hash-part-2/#comments</comments>
		<pubDate>Wed, 13 May 2009 19:26:13 +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[ruby]]></category>

		<guid isPermaLink="false">http://www.staticmethod.net/?p=70</guid>
		<description><![CDATA[Small update, the recursion was killing me, so I introduced some attempts to control how deeply the code recurses.  My approach was basically to introduce a depth count, and if the depth count reaches 0, I force a storage growth on the next rehash, thus reducing the number of collisions by increasing the storage size.
This [...]]]></description>
			<content:encoded><![CDATA[<p>Small update, the recursion was killing me, so I introduced some attempts to control how deeply the code recurses.  My approach was basically to introduce a depth count, and if the depth count reaches 0, I force a storage growth on the next rehash, thus reducing the number of collisions by increasing the storage size.</p>
<p>This approach is working pretty well, especially if the size of the storage is set to a larger size to start with.   The insertions and hashing and lookups are fast, it&#8217;s the rehashing and collisions that cause lots of problems.<a rel="attachment wp-att-71" href="http://www.staticmethod.net/2009/05/13/cuckoo-hash-part-2/cuckoo_hash1/"></a></p>
<p><a rel="attachment wp-att-71" href="http://www.staticmethod.net/2009/05/13/cuckoo-hash-part-2/cuckoo_hash1/">Cuckoo Hash Part 2</a> &#8211; Second &#8216;version&#8217; of my cuckoo hash implementation, including the changes above.  It now uses a list of 499 commonly used english words, and inserts 6 permutations of the list (pattern: &#8220;word-<em>i</em>&#8220;, where i is 1-6), for a total insertion count of 2994.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.staticmethod.net/2009/05/13/cuckoo-hash-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>Skip Lists</title>
		<link>http://www.staticmethod.net/2009/05/05/skip-lists/</link>
		<comments>http://www.staticmethod.net/2009/05/05/skip-lists/#comments</comments>
		<pubDate>Wed, 06 May 2009 04:13:25 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Data and Algorithms]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[lists]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[skip list]]></category>

		<guid isPermaLink="false">http://www.staticmethod.net/?p=38</guid>
		<description><![CDATA[Skip lists are a variant of regular linked links that can allow for O(log n) search time.  According to nist.gov&#8217;s Dictionary of Algorithms and Data Structures entry for Skip Lists, they are:
A randomized variant of an  ordered linked list with additional, parallel lists. Parallel lists at higher levels skip geometrically more items. Searching begins [...]]]></description>
			<content:encoded><![CDATA[<p>Skip lists are a variant of regular linked links that can allow for <em>O(log n)</em> search time.  According to <em>nist.gov</em>&#8217;s <a href="http://www.itl.nist.gov/div897/sqg/dads/HTML/skiplist.html">Dictionary of Algorithms and Data Structures</a> entry for Skip Lists, they are:</p>
<blockquote><p>A <a href="http://www.itl.nist.gov/div897/sqg/dads/HTML/randomizedAlgo.html"><em>randomized</em></a> variant of an  <a href="http://www.itl.nist.gov/div897/sqg/dads/HTML/orderedLinkedList.html"><em>ordered linked list</em></a> with additional, parallel lists. Parallel lists at higher levels skip geometrically more items. Searching begins at the highest level, to quickly get to the right part of the list, then uses progressively lower level lists. A new item is added by randomly selecting a level, then inserting it in order in the lists for that and all lower levels. With enough levels, searching is <a href="http://www.itl.nist.gov/div897/sqg/dads/HTML/bigOnotation.html"><em>O(log n)</em></a>.</p></blockquote>
<p>At a basic level, searching a skip list consists of starting at element <em>i</em> (where i = 0 to start) in level <em>j</em> in the list (where <em>j</em> is the highest level on the first iteration) and searching for your item.  If you encouter a value that is greater than the value you are looking for, you move back to element <em>i-1</em> in level <em>j-1</em> and continue searching level <em>j &#8211; 1</em>.  You continue this pattern until you find your element.</p>
<p>This process allows you to skip sections of the list that won&#8217;t contain your element (since skip lists are always sorted).  Here&#8217;s a visual example:</p>
<pre>1---------------5---------------*
1-------3-------5-------7-------*
1---2---3---4---5---6---7---8---*</pre>
<p>Given this example, if you were searching for 8 you would only evaluate 4 elements, rather than 8 in a normal linked list.  The sequence of evalution would go as follows:</p>
<p>1 -&gt; 5 -&gt; 7 -&gt; 8</p>
<p>I won&#8217;t go into a lot of detail on the implementation of a skip list, but I&#8217;ll give a quick rundown of the basic and provide a couple links I found helpful.<span id="more-38"></span></p>
<p>The structure of the list is can be comprised of two parts, the list itself and nodes.  I&#8217;ve taken the example from one of the sites listed below and kind of translated it into very rudimentary Ruby.  At some point in the future, I might get around to writing some better ruby code, this is written for the sake of example.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Node
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:height</span>, <span style="color:#ff3333; font-weight:bold;">:val</span>, <span style="color:#ff3333; font-weight:bold;">:next</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@height</span> = <span style="color:#006666;">0</span>
    <span style="color:#0066ff; font-weight:bold;">@val</span> = <span style="color:#006666;">0</span>
    <span style="color:#0066ff; font-weight:bold;">@next</span> = <span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> SkipList
&nbsp;
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:height</span>, <span style="color:#ff3333; font-weight:bold;">:head</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@height</span> = <span style="color:#006666;">0</span>
    <span style="color:#0066ff; font-weight:bold;">@head</span> = <span style="color:#0000FF; font-weight:bold;">nil</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> find<span style="color:#006600; font-weight:bold;">&#40;</span> toFind <span style="color:#006600; font-weight:bold;">&#41;</span>
    node = <span style="color:#0066ff; font-weight:bold;">@head</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># For each height, starting at the top...</span>
    <span style="color:#006600; font-weight:bold;">&#40;</span>@height..0<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>height<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#008000; font-style:italic;"># Traverse the list from left to right at this height,</span>
      <span style="color:#008000; font-style:italic;"># looking for the node who's next node is still smaller than</span>
      <span style="color:#008000; font-style:italic;"># the value we're looking for</span>
      <span style="color:#9966CC; font-weight:bold;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> !node.<span style="color:#9966CC; font-weight:bold;">next</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>? <span style="color:#006600; font-weight:bold;">&amp;</span>amp;<span style="color:#006600; font-weight:bold;">&amp;</span>amp; <span style="color:#006600; font-weight:bold;">&#40;</span>toFind <span style="color:#006600; font-weight:bold;">&amp;</span>gt; node.<span style="color:#9966CC; font-weight:bold;">next</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">val</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
        node = node.<span style="color:#9966CC; font-weight:bold;">next</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># By now, the next node should be the node we're looking for, nil, or</span>
    <span style="color:#008000; font-style:italic;"># possibly greater than our node, but where our value should be</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> node.<span style="color:#9966CC; font-weight:bold;">next</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">val</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This only covers basic searching.  There are algorithms for insertion, deletion and re-balancing the skip list as well.  The page I&#8217;ve linked to below on Eternally Confuzzled covers some of these topics, and I hope to get to some of them in the future.</p>
<p>I found the following pages to be quite useful, I&#8217;m still reading up on Skip Lists so maybe I&#8217;ll have a nicer implementation available in the future:</p>
<ul>
<li><a href="http://www.itl.nist.gov/div897/sqg/dads/HTML/skiplist.html">Skip List</a> at Dictionary of Algorithms and Data Structures</li>
<li><a href="http://eternallyconfuzzled.com/tuts/datastructures/jsw_tut_skip.aspx">Skip Lists</a> at Eternally Confuzzled, where I found sample implementations, a very nice write up</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.staticmethod.net/2009/05/05/skip-lists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
