4.4 Analysis of Skiplists

In this section, we analyze the expected height, size, and length of the search path in a skiplist. This section requires a background in basic probability. Several proofs are based on the following basic observation about coin tosses.

Lemma 4..2   Let $ T$ be the number of times a fair coin is tossed up to and including the first time the coin comes up heads. Then $ \mathrm{E}[T]=2$.

Proof. Suppose we stop tossing the coin the first time it comes up heads. Define the indicator variable

$\displaystyle I_{i} = \left\{\begin{array}{ll}
0 & \mbox{if the coin is tossed...
...\\
1 & \mbox{if the coin is tossed $i$\ or more times}
\end{array}\right.
$

Note that $ I_i=1$ if and only if the first $ i-1$ coin tosses are tails, so $ \mathrm{E}[I_i]=\Pr\{I_i=1\}=1/2^{i-1}$. Observe that $ T$, the total number of coin tosses, can be written as $ T=\sum_{i=1}^{\infty} I_i$. Therefore,

$\displaystyle \mathrm{E}[T]$ $\displaystyle = \mathrm{E}\left[\sum_{i=1}^\infty I_i\right]$    
  $\displaystyle = \sum_{i=1}^\infty \mathrm{E}\left[I_i\right]$    
  $\displaystyle = \sum_{i=1}^\infty 1/2^{i-1}$    
  $\displaystyle = 1 + 1/2 + 1/4 + 1/8 + \cdots$    
  $\displaystyle = 2 \enspace . \qedhere$    

$ \qedsymbol$

The next two lemmata tell us that skiplists have linear size:

Lemma 4..3   The expected number of nodes in a skiplist containing $ \ensuremath{\mathtt{n}}$ elements, not including occurrences of the sentinel, is $ 2\ensuremath{\mathtt{n}}$.

Proof. The probability that any particular element, $ \mathtt{x}$, is included in list $ L_{\ensuremath{\mathtt{r}}}$ is $ 1/2^{\ensuremath{\mathtt{r}}}$, so the expected number of nodes in $ L_{\ensuremath{\mathtt{r}}}$ is $ \ensuremath{\mathtt{n}}/2^{\ensuremath{\mathtt{r}}}$.4.2 Therefore, the total expected number of nodes in all lists is

$\displaystyle \sum_{\ensuremath{\mathtt{r}}=0}^\infty \ensuremath{\mathtt{n}}/2...
...athtt{n}}(1+1/2+1/4+1/8+\cdots) = 2\ensuremath{\mathtt{n}} \enspace . \qedhere $

$ \qedsymbol$

Lemma 4..4   The expected height of a skiplist containing $ \mathtt{n}$ elements is at most $ \log \ensuremath{\mathtt{n}} + 2$.

Proof. For each $ \ensuremath{\mathtt{r}}\in\{1,2,3,\ldots,\infty\}$, define the indicator random variable

$\displaystyle I_{\ensuremath{\mathtt{r}}} = \left\{\begin{array}{ll}
0 & \mbox...
...1 & \mbox{if $L_{\ensuremath{\mathtt{r}}}$\ is non-empty}
\end{array}\right.
$

The height, $ \mathtt{h}$, of the skiplist is then given by

$\displaystyle \ensuremath{\mathtt{h}} = \sum_{i=1}^\infty I_{\ensuremath{\mathtt{r}}} \enspace .
$

Note that $ I_{\ensuremath{\mathtt{r}}}$ is never more than the length, $ \vert L_{\ensuremath{\mathtt{r}}}\vert$, of $ L_{\ensuremath{\mathtt{r}}}$, so

$\displaystyle \mathrm{E}[I_{\ensuremath{\mathtt{r}}}] \le \mathrm{E}[\vert L_{\...
...t{r}}}\vert] = \ensuremath{\mathtt{n}}/2^{\ensuremath{\mathtt{r}}} \enspace .
$

Therefore, we have

$\displaystyle \mathrm{E}[\ensuremath{\mathtt{h}}]$ $\displaystyle = \mathrm{E}\left[\sum_{r=1}^\infty I_{\ensuremath{\mathtt{r}}}\right]$    
  $\displaystyle = \sum_{\ensuremath{\mathtt{r}}=1}^{\infty} E[I_{\ensuremath{\mathtt{r}}}]$    
  $\displaystyle = \sum_{\ensuremath{\mathtt{r}}=1}^{\lfloor\log \ensuremath{\math...
...r\log \ensuremath{\mathtt{n}}\rfloor+1}^{\infty} E[I_{\ensuremath{\mathtt{r}}}]$    
  $\displaystyle \le \sum_{\ensuremath{\mathtt{r}}=1}^{\lfloor\log \ensuremath{\ma...
...thtt{n}}\rfloor+1}^{\infty} \ensuremath{\mathtt{n}}/2^{\ensuremath{\mathtt{r}}}$    
  $\displaystyle \le \log \ensuremath{\mathtt{n}} + \sum_{\ensuremath{\mathtt{r}}=0}^\infty 1/2^{\ensuremath{\mathtt{r}}}$    
  $\displaystyle = \log \ensuremath{\mathtt{n}} + 2 \enspace . \qedhere$    

$ \qedsymbol$

Lemma 4..5   The expected number of nodes in a skiplist containing $ \ensuremath{\mathtt{n}}$ elements, including all occurrences of the sentinel, is $ 2\ensuremath{\mathtt{n}}+O(\log \ensuremath{\mathtt{n}})$.

Proof. By Lemma 4.3, the expected number of nodes, not including the sentinel, is $ 2\ensuremath{\mathtt{n}}$. The number of occurrences of the sentinel is equal to the height, $ \ensuremath{\mathtt{h}}$, of the skiplist so, by Lemma 4.4 the expected number of occurrences of the sentinel is at most $ \log \ensuremath{\mathtt{n}}+2 = O(\log \ensuremath{\mathtt{n}})$. $ \qedsymbol$

Lemma 4..6   The expected length of a search path in a skiplist is at most $ 2\log \ensuremath{\mathtt{n}} + O(1)$.

Proof. The easiest way to see this is to consider the reverse search path for a node, $ \mathtt{x}$. This path starts at the predecessor of $ \mathtt{x}$ in $ L_0$. At any point in time, if the path can go up a level, then it does. If it cannot go up a level then it goes left. Thinking about this for a few moments will convince us that the reverse search path for $ \mathtt{x}$ is identical to the search path for $ \mathtt{x}$, except that it is reversed.

The number of nodes that the reverse search path visits at a particular level, $ \mathtt{r}$, is related to the following experiment: Toss a coin. If the coin comes up heads then go up and stop, otherwise go left and repeat the experiment. The number of coin tosses before the heads then represents the number of steps to the left that a reverse search path takes at a particular level.4.3 Lemma 4.2 tells us that the expected number of coin tosses before the first heads is 1.

Let $ S_{\ensuremath{\mathtt{r}}}$ denote the number of steps the forward search path takes at level $ \ensuremath{\mathtt{r}}$ that go to the right. We have just argued that $ \mathrm{E}[S_{\ensuremath{\mathtt{r}}}]\le
1$. Furthermore, $ S_{\ensuremath{\mathtt{r}}}\le \vert L_{\ensuremath{\mathtt{r}}}\vert$, since we can't take more steps in $ L_{\ensuremath{\mathtt{r}}}$ than the length of $ L_{\ensuremath{\mathtt{r}}}$, so

$\displaystyle \mathrm{E}[S_{\ensuremath{\mathtt{r}}}] \le \mathrm{E}[\vert L_{\...
...t{r}}}\vert] = \ensuremath{\mathtt{n}}/2^{\ensuremath{\mathtt{r}}} \enspace .
$

We can now finish as in the proof of Lemma 4.4. Let $ S$ be the length of the search path for some node, $ \mathtt{u}$, in a skiplist, and let $ \ensuremath{\mathtt{h}}$ be the height of the skiplist. Then

$\displaystyle \mathrm{E}[S]$ $\displaystyle = \mathrm{E}\left[ \ensuremath{\mathtt{h}} + \sum_{\ensuremath{\mathtt{r}}=0}^\infty S_{\ensuremath{\mathtt{r}}} \right]$    
  $\displaystyle = \mathrm{E}[\ensuremath{\mathtt{h}}] + \sum_{\ensuremath{\mathtt{r}}=0}^\infty \mathrm{E}[S_{\ensuremath{\mathtt{r}}}]$    
  $\displaystyle = \mathrm{E}[\ensuremath{\mathtt{h}}] + \sum_{\ensuremath{\mathtt...
...ensuremath{\mathtt{n}}\rfloor+1}^\infty \mathrm{E}[S_{\ensuremath{\mathtt{r}}}]$    
  $\displaystyle \le \mathrm{E}[\ensuremath{\mathtt{h}}] + \sum_{\ensuremath{\math...
...mathtt{n}}\rfloor+1}^\infty \ensuremath{\mathtt{n}}/2^{\ensuremath{\mathtt{r}}}$    
  $\displaystyle \le \mathrm{E}[\ensuremath{\mathtt{h}}] + \sum_{\ensuremath{\math...
...or} 1 + \sum_{\ensuremath{\mathtt{r}}=0}^{\infty} 1/2^{\ensuremath{\mathtt{r}}}$    
  $\displaystyle \le \mathrm{E}[\ensuremath{\mathtt{h}}] + \sum_{\ensuremath{\math...
...or} 1 + \sum_{\ensuremath{\mathtt{r}}=0}^{\infty} 1/2^{\ensuremath{\mathtt{r}}}$    
  $\displaystyle \le \mathrm{E}[\ensuremath{\mathtt{h}}] + \log \ensuremath{\mathtt{n}} + 3$    
  $\displaystyle \le 2\log \ensuremath{\mathtt{n}} + 5 \enspace . \qedhere$    

$ \qedsymbol$

The following theorem summarizes the results in this section:

Theorem 4..3   A skiplist containing $ \ensuremath{\mathtt{n}}$ elements has expected size $ O(\ensuremath{\mathtt{n}})$ and the expected length of the search path for any particular element is at most $ 2\log \ensuremath{\mathtt{n}} + O(1)$.



Footnotes

....4.2
See Section 1.2.4 to see how this is derived using indicator variables and linearity of expectation.
... level.4.3
Note that this might overcount the number of steps to the left, since the experiment should end either at the first heads or when the search path reaches the sentinel, whichever comes first. This is not a problem since the lemma is only stating an upper bound.
opendatastructures.org