Tyler Perkins

Junior Software Engineer - KE8TIZ

Skip lists

Tyler - 2021-08-11 13:16:46

Views - 92

Tyler Perkins - Skip lists

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.

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.

Diagram of a skiplist, from Wikipedia

Complexities

Average case
  • Search - log n
  • Insert - log n
  • Delete - log n
  • Index - log n (indexable skiplist implementation)
  • Space usage - n log n
Use cases

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.

Implementations

I am using this 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!