This repository has been archived on 2022-12-28. You can view files and clone it, but cannot push or open issues or pull requests.
site-posts/Skip lists.html

64 lines
2.5 KiB
HTML
Raw Normal View History

2022-12-27 00:24:02 +00:00
<div class="post-2-1">
<div>
<p>Skiplists are possibly one of the most useful data structures that I don't
see talked about all that often. They are a special type of list that is
probabilistic in nature, and allows for O(log n) indexing and searching, and
O(1) insertion when provided an iterator. They are always sorted, and therefore
have great ordered traversal. Its insane just how fast these things are.</p>
<p>The general concept starts with a normal linked list, usually singly linked
lists. From there we make it so that each node in the list has pointers to more
than just the next element. With this we can skip forward through the last much
faster. By randomly placing pointers forward when traversing normally, we can
build up a well distributed set of pointers, allowing us to skip forward large
amounts at first, then slowing getting more precise as we find the element we
want.</p>
</div>
<div class="col-sm-4">
<figure>
<img src="../img/Skip_list.svg" class="fig-img"></img>
<figcaption>Diagram of a skiplist, from <a
href="https://en.wikipedia.org/wiki/Skip_list">Wikipedia</a>
</figcaption>
</figure>
</div>
</div>
<div class="row no-gutters">
<div class="col-sm">
<h4>Complexities</h4>
<h5>Average case</h5>
<ul>
<li>Search - log n</li>
<li>Insert - log n</li>
<li>Delete - log n</li>
<li>Index - log n (indexable skiplist implementation)</li>
<li>Space usage - n log n</li>
</ul>
<h5>Use cases</h5>
<p>The use case that lead me to find this was implementing a cache for often
repeated malloc() operations. I have a use case where textures need to be
allocated with strings on those textures. The set of string will repeat itself
often, however the strings to be shown are somewhat unpredictable. Therefore, I
am keeping a hash table of pointers to the allocated textures, and a skip list
associating the age of the entry with the string on the texture. Using a
skiplist allows for log n lookup and removal of the entries so we can keep
accurate track of what entries are how old. Due to its fast access of its
elements, and having O(1) access time to the head element, there is a slight
performance advantage to using this versus a Heap.</p>
<h5>Implementations</h5>
<p>I am using <a href="https://github.com/Samyak2/skip-list">this</a>
implementation right now to benchmark this cache system in context, then I
plan to build my own skip list. I hope this helps in your own projects!</p>
</div>
</div>