Skip list
Skip list  

Type  List  
Invented  1989  
Invented by  W. Pugh  
Time complexity in big O notation  

Part of a series on 
Probabilistic data structures 

Random trees 
Related 
In computer science, a skip list (or skiplist) is a probabilistic data structure that allows [math]\displaystyle{ \mathcal{O}(\log n) }[/math] average complexity for search as well as [math]\displaystyle{ \mathcal{O}(\log n) }[/math] average complexity for insertion within an ordered sequence of [math]\displaystyle{ n }[/math] elements. Thus it can get the best features of a sorted array (for searching) while maintaining a linked listlike structure that allows insertion, which is not possible with a static array. Fast search is made possible by maintaining a linked hierarchy of subsequences, with each successive subsequence skipping over fewer elements than the previous one (see the picture below on the right). Searching starts in the sparsest subsequence until two consecutive elements have been found, one smaller and one larger than or equal to the element searched for. Via the linked hierarchy, these two elements link to elements of the next sparsest subsequence, where searching is continued until finally searching in the full sequence. The elements that are skipped over may be chosen probabilistically^{[2]} or deterministically,^{[3]} with the former being more common.
Description
A skip list is built in layers. The bottom layer [math]\displaystyle{ 1 }[/math] is an ordinary ordered linked list. Each higher layer acts as an "express lane" for the lists below, where an element in layer [math]\displaystyle{ i }[/math] appears in layer [math]\displaystyle{ i+1 }[/math] with some fixed probability [math]\displaystyle{ p }[/math] (two commonly used values for [math]\displaystyle{ p }[/math] are [math]\displaystyle{ 1/2 }[/math] or [math]\displaystyle{ 1/4 }[/math]). On average, each element appears in [math]\displaystyle{ 1/(1p) }[/math] lists, and the tallest element (usually a special head element at the front of the skip list) appears in all the lists. The skip list contains [math]\displaystyle{ \log_{1/p}n\, }[/math] (i.e. logarithm base [math]\displaystyle{ 1/p }[/math] of [math]\displaystyle{ n }[/math]) lists.
A search for a target element begins at the head element in the top list, and proceeds horizontally until the current element is greater than or equal to the target. If the current element is equal to the target, it has been found. If the current element is greater than the target, or the search reaches the end of the linked list, the procedure is repeated after returning to the previous element and dropping down vertically to the next lower list. The expected number of steps in each linked list is at most [math]\displaystyle{ 1/p }[/math], which can be seen by tracing the search path backwards from the target until reaching an element that appears in the next higher list or reaching the beginning of the current list. Therefore, the total expected cost of a search is [math]\displaystyle{ \tfrac{1}{p}\log_{1/p}n }[/math] which is [math]\displaystyle{ \mathcal{O}(\log n)\, }[/math], when [math]\displaystyle{ p }[/math] is a constant. By choosing different values of [math]\displaystyle{ p }[/math], it is possible to trade search costs against storage costs.
Implementation details
The elements used for a skip list can contain more than one pointer since they can participate in more than one list.
Insertions and deletions are implemented much like the corresponding linkedlist operations, except that "tall" elements must be inserted into or deleted from more than one linked list.
[math]\displaystyle{ \mathcal{O}(n) }[/math] operations, which force us to visit every node in ascending order (such as printing the entire list), provide the opportunity to perform a behindthescenes derandomization of the level structure of the skiplist in an optimal way, bringing the skip list to [math]\displaystyle{ \mathcal{O}(\log n) }[/math] search time. (Choose the level of the i'th finite node to be 1 plus the number of times it is possible to repeatedly divide i by 2 before it becomes odd. Also, i=0 for the negative infinity header as there is the usual special case of choosing the highest possible level for negative and/or positive infinite nodes.) However this also allows someone to know where all of the higherthanlevel 1 nodes are and delete them.
Alternatively, the level structure could be made quasirandom in the following way:
make all nodes level 1 j ← 1 while the number of nodes at level j > 1 do for each i'th node at level j do if i is odd and i is not the last node at level j randomly choose whether to promote it to level j+1 else if i is even and node i1 was not promoted promote it to level j+1 end if repeat j ← j + 1 repeat
Like the derandomized version, quasirandomization is only done when there is some other reason to be running an [math]\displaystyle{ \mathcal{O}(n) }[/math] operation (which visits every node).
The advantage of this quasirandomness is that it doesn't give away nearly as much levelstructure related information to an adversarial user as the derandomized one. This is desirable because an adversarial user who is able to tell which nodes are not at the lowest level can pessimize performance by simply deleting higherlevel nodes. (Bethea and Reiter however argue that nonetheless an adversary can use probabilistic and timing methods to force performance degradation.^{[4]}) The search performance is still guaranteed to be logarithmic.
It would be tempting to make the following "optimization": In the part which says "Next, for each ith...", forget about doing a coinflip for each evenodd pair. Just flip a coin once to decide whether to promote only the even ones or only the odd ones. Instead of [math]\displaystyle{ \mathcal{O}(n \log n) }[/math] coin flips, there would only be [math]\displaystyle{ \mathcal{O}(\log n) }[/math] of them. Unfortunately, this gives the adversarial user a 50/50 chance of being correct upon guessing that all of the even numbered nodes (among the ones at level 1 or higher) are higher than level one. This is despite the property that there is a very low probability of guessing that a particular node is at level N for some integer N.
A skip list does not provide the same absolute worstcase performance guarantees as more traditional balanced tree data structures, because it is always possible (though with very low probability^{[5]}) that the coinflips used to build the skip list will produce a badly balanced structure. However, they work well in practice, and the randomized balancing scheme has been argued to be easier to implement than the deterministic balancing schemes used in balanced binary search trees. Skip lists are also useful in parallel computing, where insertions can be done in different parts of the skip list in parallel without any global rebalancing of the data structure. Such parallelism can be especially advantageous for resource discovery in an adhoc wireless network because a randomized skip list can be made robust to the loss of any single node.^{[6]}
Indexable skiplist
As described above, a skip list is capable of fast [math]\displaystyle{ \mathcal{O}(\log n) }[/math] insertion and removal of values from a sorted sequence, but it has only slow [math]\displaystyle{ \mathcal{O}(n) }[/math] lookups of values at a given position in the sequence (i.e. return the 500th value); however, with a minor modification the speed of random access indexed lookups can be improved to [math]\displaystyle{ \mathcal{O}(\log n) }[/math].
For every link, also store the width of the link. The width is defined as the number of bottom layer links being traversed by each of the higher layer "express lane" links.
For example, here are the widths of the links in the example at the top of the page:
1 10 o> o> o Top level 1 3 2 5 o> o> o> o> o Level 3 1 2 1 2 3 2 o> o> o> o> o> o> o Level 2 1 1 1 1 1 1 1 1 1 1 1 o> o> o> o> o> o> o> o> o> o> o> o Bottom level Head 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th NIL Node Node Node Node Node Node Node Node Node Node
Notice that the width of a higher level link is the sum of the component links below it (i.e. the width 10 link spans the links of widths 3, 2 and 5 immediately below it). Consequently, the sum of all widths is the same on every level (10 + 1 = 1 + 3 + 2 + 5 = 1 + 2 + 1 + 2 + 3 + 2).
To index the skip list and find the i'th value, traverse the skip list while counting down the widths of each traversed link. Descend a level whenever the upcoming width would be too large.
For example, to find the node in the fifth position (Node 5), traverse a link of width 1 at the top level. Now four more steps are needed but the next width on this level is ten which is too large, so drop one level. Traverse one link of width 3. Since another step of width 2 would be too far, drop down to the bottom level. Now traverse the final link of width 1 to reach the target running total of 5 (1+3+1).
function lookupByPositionIndex(i) node ← head i ← i + 1 # don't count the head as a step for level from top to bottom do while i ≥ node.width[level] do # if next step is not too far i ← i  node.width[level] # subtract the current width node ← node.next[level] # traverse forward at the current level repeat repeat return node.value end function
This method of implementing indexing is detailed in "A skip list cookbook" by William Pugh^{[7]}
History
Skip lists were first described in 1989 by William Pugh.^{[8]}
To quote the author:
Skip lists are a probabilistic data structure that seem likely to supplant balanced trees as the implementation method of choice for many applications. Skip list algorithms have the same asymptotic expected time bounds as balanced trees and are simpler, faster and use less space.
Usages
List of applications and frameworks that use skip lists:
 Apache Portable Runtime implements skip lists.^{[9]}
 MemSQL uses lockfree skip lists as its prime indexing structure for its database technology.
 MuQSS built on skip lists as a cpu scheduler.^{[10]}
 Cyrus IMAP server offers a "skiplist" backend DB implementation^{[11]}
 Lucene uses skip lists to search deltaencoded posting lists in logarithmic time.
 The "QMap" key/value dictionary (up to Qt 4) template class of Qt is implemented with skip lists.^{[12]}
 Redis, an ANSIC opensource persistent key/value store for Posix systems, uses skip lists in its implementation of ordered sets.^{[13]}
 "nessDB", a very fast keyvalue embedded Database Storage Engine (Using logstructuredmerge (LSM) trees), uses skip lists for its memtable.^{[14]}
 "skipdb" is an opensource implementation of an ACID ordered key/value database, implemented with a skip list instead of a btree.^{[15]}
 ConcurrentSkipListSet and ConcurrentSkipListMap in the Java 1.6 API.^{[16]} Used by Apache HBase.
 Speed Tables are a fast keyvalue datastore for Tcl that use skiplists for indexes and lockless shared memory.
 "leveldb" is a fast keyvalue storage library written at Google that provides an ordered mapping from string keys to string values^{[17]}
 Con Kolivas' MuQSS^{[18]} Scheduler for the Linux kernel uses skip lists
 SkiMap uses skip lists as base data structure to build a more complex 3D Sparse Grid for Robot Mapping systems.^{[19]}
 "IOWOW" is a persistent C11 key/value storage library that uses skip lists as its main data structure.^{[20]}
Skip lists are used for efficient statistical computations of running medians (also known as moving medians).^{[21]}
Skip lists are also used in distributed applications (where the nodes represent physical computers, and pointers represent network connections) and for implementing highly scalable concurrent priority queues with less lock contention,^{[22]} or even without locking,^{[23]}^{[24]}^{[25]} as well as lockfree concurrent dictionaries.^{[26]} There are also several US patents for using skip lists to implement (lockless) priority queues and concurrent dictionaries.^{[27]}
See also
References
 ↑ ^{1.0} ^{1.1} Papadakis, Thomas (1993). Skip Lists and Probabilistic Analysis of Algorithms (PDF) (Ph.D.). University of Waterloo.
 ↑ Pugh, W. (1990). "Skip lists: A probabilistic alternative to balanced trees". Communications of the ACM 33 (6): 668–676. doi:10.1145/78973.78977. ftp://ftp.cs.umd.edu/pub/skipLists/skiplists.pdf.
 ↑ Munro, J. Ian; Papadakis, Thomas; Sedgewick, Robert (1992). "Deterministic skip lists". Orlando, Florida, USA: Society for Industrial and Applied Mathematics, Philadelphia, PA, USA. pp. 367–375. https://www.ic.unicamp.br/~celio/peer2peer/skipnetgraph/deterministicskiplistsmunro.pdf.
 ↑ Bethea, Darrell; Reiter, Michael K. (September 21–23, 2009). "Data Structures with Unpredictable Timing". ESORICS 2009, 14th European Symposium on Research in Computer Security. SaintMalo, France. pp. 456–471, §4 "Skip Lists". doi:10.1007/9783642044441_28. ISBN 9783642044434. https://www.cs.unc.edu/~djb/papers/2009ESORICS.pdf#page=5.
 ↑ Sen, Sandeep (1991). "Some observations on skip lists". Information Processing Letters 39 (4): 173–176. doi:10.1016/00200190(91)90175H.
 ↑ Shah, Gauri (2003). Distributed Data Structures for PeertoPeer Systems (PDF) (Ph.D. thesis). Yale University.
 ↑ William Pugh. "A skip list cookbook". 1990. Section 3.4 Linear List Operations .
 ↑ Pugh, William (April 1989). Concurrent Maintenance of Skip Lists (PS, PDF) (Technical report). Dept. of Computer Science, U. Maryland. CSTR2222.
 ↑ Apache Portable Runtime APR 1.6 documentation
 ↑ LWN's article
 ↑ Cyrus IMAP server. skiplist source file
 ↑ QMap
 ↑ "Redis ordered set implementation". https://github.com/antirez/redis/blob/unstable/src/t_zset.c.
 ↑ nessDB
 ↑ skipdb
 ↑ ConcurrentSkipListSet and ConcurrentSkipListMap
 ↑ leveldb
 ↑ "LKML: Con Kolivas: [ANNOUNCE Multiple Queue Skiplist Scheduler version 0.120"]. https://lkml.org/lkml/2016/10/29/4.
 ↑ Gregorio, Daniele De; Stefano, Luigi Di (2017). "SkiMap: An Efficient Mapping Framework for Robot Navigation". arXiv:1704.05832 [cs.CV].
 ↑ IOWOW
 ↑ Raymond Hettinger. "Efficient running median using an indexable skiplist (Python)". 2009.
 ↑ Shavit, N.; Lotan, I. (2000). "Skiplistbased concurrent priority queues". Proceedings 14th International Parallel and Distributed Processing Symposium. IPDPS 2000. pp. 263. doi:10.1109/IPDPS.2000.845994. ISBN 9780769505749. http://www.cs.tau.ac.il/~shanir/nirpubsweb/Papers/Priority_Queues.pdf.
 ↑ Sundell, H.; Tsigas, P. (2003). "Fast and lockfree concurrent priority queues for multithread systems". Proceedings International Parallel and Distributed Processing Symposium. pp. 11. doi:10.1109/IPDPS.2003.1213189. ISBN 9780769519265.
 ↑ Fomitchev, Mikhail; Ruppert, Eric (2004). "Lockfree linked lists and skip lists". Proc. Annual ACM Symp. on Principles of Distributed Computing (PODC). pp. 50–59. doi:10.1145/1011767.1011776. ISBN 1581138024. http://www.cse.yorku.ca/~ruppert/papers/lfll.pdf.
 ↑ Bajpai, R.; Dhara, K. K.; Krishnaswamy, V. (2008). "QPID: A Distributed Priority Queue with Item Locality". 2008 IEEE International Symposium on Parallel and Distributed Processing with Applications. pp. 215. doi:10.1109/ISPA.2008.90. ISBN 9780769534718.
 ↑ Sundell, H. K.; Tsigas, P. (2004). "Scalable and lockfree concurrent dictionaries". Proceedings of the 2004 ACM symposium on Applied computing  SAC '04. pp. 1438. doi:10.1145/967900.968188. ISBN 9781581138122. http://www.cse.chalmers.se/~tsigas/papers/LockFree_Dictionary.pdf.
 ↑ US patent 7937378
External links
 "Skip list" entry in the Dictionary of Algorithms and Data Structures
 Skip Lists: A Linked List with SelfBalancing BSTLike Properties on MSDN in C# 2.0
 Why Skip Lists?
 Skip Lists lecture (MIT OpenCourseWare: Introduction to Algorithms)
 Open Data Structures  Chapter 4  Skiplists, Pat Morin
 Skip trees, an alternative data structure to skip lists in a concurrent approach
 Skip tree graphs, a distributed version of skip trees
 More on skip tree graphs, a distributed version of skip trees
de:Liste (Datenstruktur)#SkipListe
Original source: https://en.wikipedia.org/wiki/Skip list.
Read more 