MCS-208 · Unit 3

Lists

Section 7 of 8

Block 1 · Unit 3Lists12 min · Section 7 of 8

3.7Skip Lists

Covers IGNOU MCS-208 §Programme guide topic "Skip lists" (not in the Unit 3 booklet)

Searching a sorted linked list is O(n)O(n), even though the data is sorted, because we cannot jump to the middle as binary search does in an array. A skip list adds “express lanes” on top of the linked list so that a search can skip over many nodes at once.

The idea

Each node stores its key and an array of forward links, one per level it belongs to. A dummy header node starts every level.

A skip list with 4 levels
level 3: header ------------------------------------> 19 ------------------------> NULL
level 2: header ----------------------> 12 ---------> 19 ------------------------> NULL
level 1: header ----------------------> 12 ---------> 19 --> 21 ---------> 26 --> NULL
level 0: header --> 3 --> 6 --> 7 --> 9 --> 12 --> 17 --> 19 --> 21 --> 25 --> 26 --> NULL

Searching

Start at the header on the top level. On each level, move right while the next key is smaller than the search key; then drop down one level. After level 0, the next node either holds the key or the key is not present.

Insertion

  1. Search for the key as above, remembering in update[i] the last node visited on each level ii.
  2. Choose the new node’s number of levels at random: start at 1 and keep adding a level while a coin flip comes up heads.
  3. On each of its levels ii, link the new node in after update[i] (as in an ordinary singly linked list insertion).

Deletion is the reverse: find update[i] on every level and bypass the node on each level it belongs to.

Cost

OperationSkip list (expected)Skip list (worst case)Sorted linked list
SearchO(log⁡n)O(\log n)O(n)O(n)O(n)O(n)
InsertO(log⁡n)O(\log n)O(n)O(n)O(n)O(n)
DeleteO(log⁡n)O(\log n)O(n)O(n)O(n)O(n)
SpaceO(n)O(n) (about 2n2n links)O(n⋅L)O(n \cdot L) for at most LL levelsO(n)O(n)

The worst case happens only if the coin flips are extremely unlucky, which is very unlikely for large nn. Skip lists give performance similar to balanced search trees (Unit 7) with much simpler code; they are used, for example, in the Redis database for sorted sets.

Key takeaways

  • A skip list = sorted linked list (level 0) + sparser express levels above it.
  • Search: go right while the next key is smaller, then go down a level.
  • A new node’s height is chosen by coin flips (probability 12\frac{1}{2} per extra level).
  • Expected O(log⁡n)O(\log n) search, insert and delete; O(n)O(n) expected space.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the expected time to search a skip list of nn keys?

Show answer

Answer: B. O(log⁡n)O(\log n)

Each level roughly halves the remaining distance, as binary search does. O(n)O(n) is the cost for a plain sorted linked list, and the skip list’s unlucky worst case.

Question 2 · multiple choice

In a randomised skip list, a node on level ii is also placed on level i+1i + 1 with probability:

Show answer

Answer: C. 12\frac{1}{2}

A coin flip decides. With probability 1, every node would be on every level and the express levels would not skip anything.

Question 3 · multiple choice

Using the skip list in the example, which nodes does the search for 21 visit?

Show answer

Answer: D. 19

On level 3 the search moves to 19 (19 < 21). On levels 2, 1 and 0 the next key after 19 is not smaller than 21, so it only drops down; the next node on level 0 is 21: found after 1 move.

Question 4 · multiple choice

What does the array update[] hold during insertion into a skip list?

Show answer

Answer: B. The last node before the new key on each level

The new node is linked in after update[i] on each of its levels, exactly like inserting after a node in a singly linked list.

Question 5 · multiple choice

Which statement about skip lists is true?

Show answer

Answer: C. Level 0 contains every key in sorted order.

Level 0 is the complete sorted linked list; the upper levels hold subsets. The O(log⁡n)O(\log n) bound is expected, not worst-case.

Question 6 · short answer

Explain how a key is searched for in a skip list, and trace the search for 9 in the example.

Show worked answer

Start at the header on the highest level. On the current level, move forward while the next node’s key is smaller than the search key; when the next key is larger or equal (or there is none), go down one level. After processing level 0, the next node on level 0 holds the key if it is present. Trace for 9: level 3: next is 19, not < 9, go down; level 2: next is 12, go down; level 1: next is 12, go down; level 0: move to 3, 6, 7 (all < 9); next is 9, not < 9, stop. The next node holds 9: found after 3 moves.

↑ ↓ move↵ openTab switch type