23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

can go <strong>in</strong>to what is almost an <strong>in</strong>f<strong>in</strong>ite loop (it is not actually an <strong>in</strong>f<strong>in</strong>ite loop,<br />

however, s<strong>in</strong>ce the probability of hav<strong>in</strong>g a fair co<strong>in</strong> repeatedly come up heads<br />

forever is 0). Moreover, we cannot <strong>in</strong>f<strong>in</strong>itely add positions to a list without<br />

eventually runn<strong>in</strong>g out of memory. In any case, if we term<strong>in</strong>ate position <strong>in</strong>sertion at<br />

the highest level h, then the worst-case runn<strong>in</strong>g time for perform<strong>in</strong>g the f<strong>in</strong>d,<br />

<strong>in</strong>sert, <strong>and</strong> remove operations <strong>in</strong> a skip list S with n entries <strong>and</strong> height h is O(n<br />

+ h). This worst-case performance occurs when the tower of every entry reaches<br />

level h−1, where h is the height of S. However, this event has very low probability.<br />

Judg<strong>in</strong>g from this worst case, we might conclude that the skip list structure is<br />

strictly <strong>in</strong>ferior to the other dictionary implementations discussed earlier <strong>in</strong> this<br />

chapter. But this would not be a fair analysis, for this worst-case behavior is a gross<br />

overestimate.<br />

Bound<strong>in</strong>g the Height of a Skip List<br />

Because the <strong>in</strong>sertion step <strong>in</strong>volves r<strong>and</strong>omization, a more accurate analysis of<br />

skip lists <strong>in</strong>volves a bit of probability. At first, this might seem like a major<br />

undertak<strong>in</strong>g, for a complete <strong>and</strong> thorough probabilistic analysis could require<br />

deep mathematics (<strong>and</strong>, <strong>in</strong>deed, there are several such deep analyses that have<br />

appeared <strong>in</strong> data structures research literature). Fortunately, such an analysis is<br />

not necessary to underst<strong>and</strong> the expected asymptotic behavior of skip lists. The<br />

<strong>in</strong>formal <strong>and</strong> <strong>in</strong>tuitive probabilistic analysis we give below uses only basic<br />

concepts of probability theory.<br />

Let us beg<strong>in</strong> by determ<strong>in</strong><strong>in</strong>g the expected value of the height h of a skip list S with<br />

n entries (assum<strong>in</strong>g that we do not term<strong>in</strong>ate <strong>in</strong>sertions early). The probability that<br />

a given entry has a tower of height i ≥ 1 is equal to the probability of gett<strong>in</strong>g i<br />

consecutive heads when flipp<strong>in</strong>g a co<strong>in</strong>, that is, this probability is 1/2 i . Hence, the<br />

probability PPi that level i has at least one position is at most<br />

P i ≤ n/2 i ,<br />

for the probability that any one of n different events occurs is at most the sum of<br />

the probabilities that each occurs.<br />

The probability that the height h of S is larger than i is equal to the probability that<br />

level i has at least one position, that is, it is no more than P i This means that h is<br />

larger than, say, 3 log n with probability at most<br />

P 3 log n ≤ n/2 3 log n<br />

= n/n 3 = 1/n 2 .<br />

For example, if n = 1000, this probability is a one-<strong>in</strong>-a-million long shot. More<br />

generally, given a constant c > 1, h is larger than c log n with probability at most<br />

1/n c−1 . That is, the probability that h is smaller than c log n is at least 1 − 1/n c−1 .<br />

Thus, with high probability, the height h of S is O(logn).<br />

565

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!