Open In App

Skip List | Set 1 (Introduction)

Last Updated : 26 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

INTRODUCTION:

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. 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:

  • The skip list is solid and trustworthy.
  • To add a new node to it, it will be inserted extremely quickly. 
  • Easy to implement compared to the hash table and binary search tree
  • The number of nodes in the skip list increases, and the possibility of the worst-case decreases
  • Requires only ?(logn) time in the average case for all operations.
  • Finding a node in the list is relatively straightforward.

Disadvantages of Skip List:

  • It needs a greater amount of memory than the balanced tree.
  • Reverse search is not permitted.
  • Searching is slower than a linked list
  • Skip lists are not cache-friendly because they don’t optimize the locality of reference

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. References MIT Video Lecture on Skip Lists http://en.wikipedia.org/wiki/Skip_list

Sure, here are some related subtopics with details:

  1. Coin flipping technique: The coin flipping technique is used to randomly determine the number of layers an element will occupy in a skip list. When a new element is inserted, a random number is generated and compared to a predetermined threshold. If the random number is less than the threshold, the element is inserted into the next layer. This process is repeated until the random number is greater than the threshold, at which point the element is inserted into the bottom layer. The coin flipping technique is what gives skip lists their probabilistic nature and allows for their efficient average time complexity.
  2. Search operation: The search operation in a skip list involves traversing the layers from the top to bottom, skipping over elements that are not of interest, until the desired element is found or it is determined that it does not exist in the list. The skipping links in each layer allow for quick navigation to the desired element, reducing the average number of steps needed to reach it. The average time complexity of search in a skip list is O(log n), where n is the number of elements in the bottom layer.
  3. Insertion operation: The insertion operation in a skip list involves generating a random number to determine the number of layers the new element will occupy, as described in the coin flipping technique, and then inserting the element into the appropriate layers. The insertion operation preserves the sorted order of the elements in the list. The average time complexity of insertion in a skip list is O(log n), where n is the number of elements in the bottom layer.
  4. Deletion operation: The deletion operation in a skip list involves finding the element to be deleted and then removing it from all layers in which it appears. The deletion operation preserves the sorted order of the elements in the list. The average time complexity of deletion in a skip list is O(log n), where n is the number of elements in the bottom layer.
  5. Complexity analysis: The average time complexity of search, insertion, and deletion in a skip list is O(log n), where n is the number of elements in the bottom layer. This is due to the use of the coin flipping technique, which allows for quick navigation to the desired element, and the probabilistic nature of skip lists, which ensures that the average number of steps needed to reach an element is logarithmic in the number of elements in the bottom layer. However, the worst-case time complexity of these operations can be O(n), which occurs in the rare case where all elements are inserted into only the bottom layer.

Previous Article
Next Article

Similar Reads

Skip List | Set 2 (Insertion)
We have already discussed the idea of Skip list and how they work in Skip List | Set 1 (Introduction). In this article, we will be discussing how to insert an element in Skip list. Deciding nodes level Each element in the list is represented by a node, the level of the node is chosen randomly while insertion in the list. Level does not depend on th
15+ min read
Skip List | Set 3 (Searching and Deletion)
In previous article Skip List | Set 2 (Insertion) we discussed the structure of skip nodes and how to insert an element in the skip list. In this article we will discuss how to search and delete an element from skip list. Searching an element in Skip list Searching an element is very similar to approach for searching a spot for inserting an element
15+ min read
Self Organizing List | Set 1 (Introduction)
The worst case search time for a sorted linked list is O(n). With a Balanced Binary Search Tree, we can skip almost half of the nodes after one comparison with root. For a sorted array, we have random access and we can apply Binary Search on arrays. One idea to make search faster for Linked Lists is Skip List. Another idea (which is discussed in th
3 min read
Unrolled Linked List | Set 1 (Introduction)
Like array and linked list, the unrolled Linked List is also a linear data structure and is a variant of a linked list. Why do we need unrolled linked list? One of the biggest advantages of linked lists over arrays is that inserting an element at any location takes only O(1). However, the catch here is that to search an element in a linked list tak
10 min read
Introduction to Multi Linked List
A multi-linked list is a special type of list that contains two or more logical key sequences. Before checking details about multi-linked list, see what is a linked list. A linked list is a data structure that is free from any size restriction until the heap memory is not full. We have seen different types of linked lists, such as Singly Linked Lis
6 min read
Introduction to Circular Doubly Linked List
A circular doubly linked list is defined as a circular linked list in which each node has two links connecting it to the previous node and the next node. Characteristics of Circular Doubly Linked List :A circular doubly linked list has the following properties: Circular: A circular doubly linked list's main feature is that it is circular in design.
4 min read
Introduction to Circular Linked List
What is Circular linked list?The circular linked list is a linked list where all nodes are connected to form a circle. In a circular linked list, the first node and the last node are connected to each other which forms a circle. There is no NULL at the end. There are generally two types of circular linked lists: Circular singly linked list: In a ci
15+ min read
Introduction to Singly Linked List
Learn more about Singly Linked List in DSA Self Paced CourseWhat is Singly Linked List?A singly linked list is a linear data structure in which the elements are not stored in contiguous memory locations and each element is connected only to its next element using a pointer. Introduction: Introduction to Linked ListSingly Linked List TutorialSingly
3 min read
XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
In the previous post, we discussed how a Doubly Linked can be created using only one space for the address field with every node. In this post, we will discuss the implementation of a memory-efficient doubly linked list. We will mainly discuss the following two simple functions. A function to insert a new node at the beginning.A function to travers
10 min read
XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
In this post, we're going to talk about how XOR linked lists are used to reduce the memory requirements of doubly-linked lists. We know that each node in a doubly-linked list has two pointer fields which contain the addresses of the previous and next node. On the other hand, each node of the XOR linked list requires only a single pointer field, whi
15+ min read
Article Tags :
Practice Tags :