![]() |
VOOZH | about |
A skip list is a data structure that allows for efficient search, insertion and deletion of elements in a sorted list. It is a probabilistic data structure, meaning that its average time complexity is determined through a probabilistic analysis.
In a skip list, elements are organized in layers, with each layer having a smaller number of elements than the one below it. The bottom layer is a regular linked list, while the layers above it contain "skipping" links that allow for fast navigation to elements that are far apart in the bottom layer. The idea behind this is to allow for quick traversal to the desired element, reducing the average number of steps needed to reach it.
Skip lists are implemented using a technique called "coin flipping." In this technique, a random number is generated for each insertion to determine the number of layers the new element will occupy. This means that, on average, each element will be in log(n) layers, where n is the number of elements in the bottom layer.
Skip lists have an average time complexity of O(log n) for search, insertion and deletion, which is similar to that of balanced trees, such as AVL trees and red-black trees, but with the advantage of simpler implementation and lower overhead.
In summary, skip lists provide a simple and efficient alternative to balanced trees for certain use cases, particularly when the average number of elements in the list is large.
Can we search in a sorted linked list better than O(n) time? The worst-case search time for a sorted linked list is O(n) as we can only linearly traverse the list and cannot skip nodes while searching. For a Balanced Binary Search Tree, we skip almost half of the nodes after one comparison with the root. For a sorted array, we have random access and we can apply Binary Search on arrays. Can we augment sorted linked lists to search faster? The answer is Skip List. The idea is simple, we create multiple layers so that we can skip some nodes. See the following example list with 16 nodes and two layers. The upper layer works as an "express lane" that connects only the main outer stations, and the lower layer works as a "normal lane" that connects every station. Suppose we want to search for 50, we start from the first node of the "express lane" and keep moving on the "express lane" till we find a node whose next is greater than 50. Once we find such a node (30 is the node in the following example) on "express lane", we move to "normal lane" using a pointer from this node, and linearly search for 50 on "normal lane". In the following example, we start from 30 on the "normal lane" and with linear search, we find 50. 👁 Image
What is the time complexity with two layers? worst-case time complexity is several nodes on the "express lane" plus several nodes in a segment (A segment is several "normal lane" nodes between two "express lane" nodes) of the "normal lane". So if we have n nodes on "normal lane", ?n (square root of n) nodes on "express lane" and we equally divide the "normal lane", then there will be ?n nodes in every segment of "normal lane". ?n is an optimal division with two layers. With this arrangement, the number of nodes traversed for a search will be O(?n). Therefore, with O(?n) extra space, we can reduce the time complexity to O(?n).
Advantages of Skip List:
Disadvantages of Skip List:
Can we do better? The time complexity of skip lists can be reduced further by adding more layers. The time complexity of the search, the insert, and delete can become O(Logn) in an average cases with O(n) extra space. We will soon be publishing more posts on Skip Lists. ReferencesMIT Video Lecture on Skip Listshttps://en.wikipedia.org/wiki/Skip_list