Sub topic 9
Choosing p and the maximum level
This chapter assumes you have read the earlier chapters on insertion and random levels. You should know Big-O notation and what an expected value is. The skip list code uses two constants, P and MAX_LEVEL. Both are tuning knobs, and this chapter explains how to pick them.
What p controls
P is the chance that a node is promoted from one layer to the next. _random_level keeps flipping a biased coin while random.random() < P. A node's height follows a geometric distribution, so the fraction of nodes that reach layer k is P raised to the power k-1.
A smaller P gives these effects:
- Fewer nodes appear in the upper layers, so each node stores fewer pointers.
- The express lanes are sparser, so each level skips further but gets crossed less precisely.
The expected number of forward pointers per node is 1 / (1 - p). With p = 0.5 that is 2. With p = 0.25 it is about 1.33.
The cost of a search
A search does two kinds of work. It descends about log base 1/p of n layers. On each layer it walks forward an expected 1/p nodes before dropping down. The expected number of steps is therefore about (1/p) * log_{1/p}(n).
Raising p shortens the descent but lengthens the horizontal walks. The product is smallest near p = 1/e, about 0.37. The curve is flat around its minimum. For p = 0.5 and p = 0.25 the expected search cost is the same, 2 log2 n, but p = 0.25 uses about a third less pointer memory.
This script computes the numbers for one million keys.
import math
def max_level(n, p):
return math.ceil(math.log(n, 1 / p))
def pointers_per_node(p):
return 1 / (1 - p)
def search_steps(n, p):
return math.log(n, 1 / p) / p
n = 1_000_000
print("p max level pointers/node search steps")
for p in (0.5, 0.25, 0.125):
print(f"{p:<6} {max_level(n, p):<10} {pointers_per_node(p):<14.2f} {search_steps(n, p):.1f}")It prints these values.
| p | Layers needed for 1,000,000 keys | Pointers per node | Expected search steps |
|---|---|---|---|
| 0.5 | 20 | 2.00 | 39.9 |
| 0.25 | 10 | 1.33 | 39.9 |
| 0.125 | 7 | 1.14 | 53.1 |
Past p = 0.25 you save little memory and the search gets slower.
Choosing the maximum level
MAX_LEVEL caps node height and sets the size of the head sentinel's pointer array. The formula is ceil(log_{1/p}(N)), where N is the largest number of keys you expect.
- With p = 0.5 and N around 65,000, you need 16 layers. This is the value used in the chapter's implementation.
- With p = 0.5 and N around four billion, you need 32 layers.
- With p = 0.25 and the same four billion keys, 16 layers are enough.
A cap that is too low does not break correctness. Every node still sits in the bottom layer, and searches still work. But the top layer fills up with more than a handful of nodes, and the search walks along it linearly. Our 16-layer list holding one million keys has about 30 nodes in its top layer. That adds roughly 30 extra steps per search, so performance degrades gradually.
A cap that is too high wastes little. The head holds some unused pointers, and self.level tracks the layers actually in use, so searches skip the empty ones.
A practical rule
- Use p = 0.5 when you want the simplest model, or when a small constant factor on search matters more than memory.
- Use p = 0.25 when memory per node matters. This is the usual choice in production.
- Compute
MAX_LEVELfrom your largest expected N, then add a few layers of headroom. - Avoid p above 0.5. Memory use grows and nothing is gained.
Exercise
A skip list with p = 0.25 must hold up to 2^20 keys (about one million). What maximum level should you choose, and how many forward pointers does the average node hold?
Show answer
The maximum level is ceil(log base 4 of 2^20) = 10, so 10 layers are enough, and a few extra give headroom. The average node holds 1 / (1 - 0.25), which is about 1.33 forward pointers.
Exercise
Why does lowering MAXLEVEL below log{1/p}(n) slow searches down without making them return wrong answers?
Show answer
Every key stays in the bottom layer, which is a complete sorted linked list, so searches still find every key. With fewer layers, the top layer holds many nodes, and the search has to walk along it one node at a time. That adds linear steps.