Proofs: Skip list
These are the human proofs of Skip list, at full length.
The expected cost of a skip list search is O(log n) full
This proof shows: expected-time, space.
Fix a set of keys and a key . The levels of the nodes are independent, with for and a constant . Ignore the cap first; it only makes levels smaller. The cost of a search is the number of moves: the moves to the right in line L3 and the moves down in line L2. Insert and delete do one search and then or more work, and , so it is enough to bound the search.
Space. The node has pointers, and . So the expected number of pointers is .
Number of levels. Let be the number of nodes with level , so . The top level is at most for each , and .
The backward walk. Reverse the search path: start at the node where the search ends at level 1, and walk back to the header at level . At a node on level of the path, the previous step of the search was either a move down into level at , which happens exactly when or is the header, or a move right into along level . For a node, the backward walk at has seen only the fact that (and facts about other nodes), so, by independence, . So each backward step is a move up with probability at least (exactly at a node, 1 at the header), independent of the past.
Climbing to level . Let , so and . The backward steps taken while the walk is below level are dominated by the number of trials needed to get successes, each trial a success with probability . This number has expectation .
The levels from up. On level , the moves to the right are at most , since each visits a different node of level . The moves down from levels above are at most the number of levels above , which is at most . So the expected number of steps at levels is at most
Total. The expected cost of a search is at most
for constant . For this is . The cap can only lower the levels; if , the bound on the climb holds unchanged, and the steps at levels are bounded as before.
Remarks. The bound holds for each set of keys and each query, because the only randomness is in the levels. With the bound is , the same leading term as , with instead of pointers per node on average.
Skip list operations keep the level lists sorted and return the right answers full
This proof shows: correctness, invariant, termination.
Write for the list obtained by following from the header, and for the set of nodes with . The invariant is (S1) is strictly increasing and contains each node; (S2) for each , is in increasing key order; (S3) and $S.header.forward[i] = $ NIL for . (S1) is (S2) for . Treat the header as a node with key and level .
Lemma 1 (search). Assume (S1) to (S3). When the loop of line L2 finishes level (after line L3 for that ), is the last element of in key order.
Proof. By downward induction on . Before level , is the header, which is in every list. Before level , is the node of the claim for , which is in (or is the header), so it is on by (S2). Line L3 moves right along while the next key is less than . Since is sorted, it stops at the last node of with key , provided that starts at or before it, which holds because . The walk ends because the list is finite.
Search. By Lemma 1 for and (S1), is the last node with key , so in line L4 is the first node with key , or NIL. Key is present if and only if this node exists and has key , which is the test of line L5. The search writes no field of the structure.
Insert. If is present, line L7 changes one value and nothing else, so the invariant holds. Otherwise let be the new node with level . For , is the last node of with key by Lemma 1; for , line L9 sets to the header, which is the last element of since that list is empty by (S3). Lines L12 and L13 put between and its old successor in , whose key is (it is by the choice of , and is absent). So stays sorted and becomes for . For the list does not change, and . So (S2) holds. Line L9 raises to when needed, so (S3) holds.
Delete. If is absent, line L15 returns and nothing changes. Otherwise is the node with key . For , is on , and its predecessor there is the last node with key , that is by Lemma 1, because . Line L17 unlinks from . The lists for do not hold . So each becomes in order, and (S2) holds. Line L18 lowers while the top list is empty, which restores (S3).
Termination. Each walk of line L3 moves along a finite list, the loops of lines L2, L11 and L16 have at most rounds, and the loop of line L18 lowers , which is at least 1.