Sub topic 8
Expected O(log n) time and O(n) space
This chapter assumes you know Big-O notation, basic probability (coin flips and expected value), and the skip list code from the earlier chapters: Node, _random_level, _find_predecessors, insert and delete.
What "expected" means here
A skip list's running time depends on random node heights, not on the order of the input. Its cost is therefore a random variable. The claim is that for any sequence of operations, the expected cost of each operation is O(log n), where the average is taken over the coin flips made during insertion.
This differs from average-case analysis of a binary search tree. A plain BST is fast on random input and slow on sorted input. A skip list is fast in expectation on every input, because the adversary cannot see the coin flips.
Space: O(n) expected
Each node has height h with probability (1 - p) * p^(h-1). With p = 0.5, the expected height is:
E[height] = 1 / (1 - p) = 2So the total number of forward pointers is about 2n, plus the head's MAX_LEVEL pointers. That is O(n) expected space. In general the expected pointers per node is 1 / (1 - p).
The following code checks this against the height sampler.
import random
def random_level(p=0.5, max_level=16):
level = 1
while level < max_level and random.random() < p:
level += 1
return level
n = 100_000
total = sum(random_level() for _ in range(n))
print(total / n) # close to 2.0Time: why the search is O(log n)
The number of nodes that reach layer i or higher is about n * p^(i-1). The top occupied layer is where this count falls to about 1. Solving n * p^(i-1) = 1 gives i ≈ log₂ n for p = 0.5. So the list has about log₂ n layers.
To see the search cost, trace a search backwards, from the found node up to the top layer. At each step, you do one of two things:
- If the current node has a higher pointer, you go up one layer.
- If it does not, you go left one node on the current layer.
Each node has a higher pointer with probability p. So each backward step goes up with probability p and left with probability 1 - p.
You need to climb about log₂ n layers. The expected number of steps to make L upward moves is L / p. With p = 0.5 that is 2 log₂ n steps in total. Each step is one pointer follow or comparison, so search takes O(log n) expected time. Insert and delete do one search plus an O(height) splice, so they have the same bound.
The worst case is not guaranteed
The O(log n) bound is an expectation, so bad outcomes are possible. If every node got height 1, the list would be a plain linked list with O(n) search. If all nodes got the maximum height, space would be O(n log n) with no benefit.
These outcomes are extremely unlikely. The chance that search cost exceeds c log n falls off rapidly as c grows, so in practice the running time stays close to its expectation. The bound still holds only in expectation (and with high probability), never as a hard limit.
Common errors
- Reading "expected" as "average over inputs." The average is over the structure's own random choices. A fixed input sequence cannot make the list slow with any reliability.
- Using a weak or predictable random source. If an attacker can predict the heights, the independence argument fails. Use a proper generator when the keys come from untrusted sources.
- Setting
MAX_LEVELtoo low. WithMAX_LEVEL = 4and a million keys, the top layer holds aboutn / 8nodes. Search then degrades toward O(n). The next chapter covers how to choose it. - Forgetting the head's cost. The head holds
MAX_LEVELpointers. This is a constant, but it matters for very small lists.
| Operation | Expected time | Worst case | Expected space |
|---|---|---|---|
| Search | O(log n) | O(n) | - |
| Insert | O(log n) | O(n) | - |
| Delete | O(log n) | O(n) | - |
| Whole structure | - | - | O(n) |
Exercise
With p = 0.25, what is the expected number of forward pointers per node, and roughly how many layers does a list of 1,024 keys have?
Show answer
The expected pointers per node is 1 / (1 - p) = 1 / 0.75 ≈ 1.33. The number of layers is about log base 1/p of n, which is log₄ 1024 = 5. Lower p saves space, but the backward search takes about 1/p steps per layer, so each search does more work per layer.