<?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; skip list</title>
	<atom:link href="http://www.staticmethod.net/tag/skip-list/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>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>
