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.

O(n)<br />

O(1) exp., O(n) worst-case<br />

O(n)<br />

9.4 Skip Lists<br />

An <strong>in</strong>terest<strong>in</strong>g data structure for efficiently realiz<strong>in</strong>g the dictionary ADT is the skip<br />

list. This data structure makes r<strong>and</strong>om choices <strong>in</strong> arrang<strong>in</strong>g the entries <strong>in</strong> such a way<br />

that search <strong>and</strong> update times are O(logn) on average, where n is the number of entries<br />

<strong>in</strong> the dictionary. Interest<strong>in</strong>gly, the notion of average time complexity used here does<br />

not depend on the probability distribution of the keys <strong>in</strong> the <strong>in</strong>put. Instead, it depends<br />

on the use of a r<strong>and</strong>om-number generator <strong>in</strong> the implementation of the <strong>in</strong>sertions to<br />

help decide where to place the new entry. The runn<strong>in</strong>g time is averaged over all<br />

possible outcomes of the r<strong>and</strong>om numbers used when <strong>in</strong>sert<strong>in</strong>g entries.<br />

Because they are used extensively <strong>in</strong> computer games, cryptography, <strong>and</strong> computer<br />

simulations, methods that generate numbers that can be viewed as r<strong>and</strong>om numbers<br />

are built <strong>in</strong>to most modern computers. Some methods, called pseudor<strong>and</strong>om number<br />

generators, generate r<strong>and</strong>om-like numbers determ<strong>in</strong>istically, start<strong>in</strong>g with an <strong>in</strong>itial<br />

number called a seed. Other methods use hardware devices to extract "true" r<strong>and</strong>om<br />

numbers from nature. In any case, we will assume that our computer has access to<br />

numbers that are sufficiently r<strong>and</strong>om for our analysis.<br />

The ma<strong>in</strong> advantage of us<strong>in</strong>g r<strong>and</strong>omization <strong>in</strong> data structure <strong>and</strong> algorithm design is<br />

that the structures <strong>and</strong> methods that result are usually simple <strong>and</strong> efficient. We can<br />

devise a simple r<strong>and</strong>omized data structure, called the skip list, which has the same<br />

logarithmic time bounds for search<strong>in</strong>g as is achieved by the b<strong>in</strong>ary search<strong>in</strong>g<br />

algorithm. Nevertheless, the bounds are expected for the skip list, while they are<br />

worst-case bounds for b<strong>in</strong>ary search<strong>in</strong>g <strong>in</strong> a look-up table. On the other h<strong>and</strong>, skip<br />

lists are much faster than look-up tables for dictionary updates.<br />

A skip list S for dictionary D consists of a series of lists {S 0 , S 1 , ..., S h }. Each list S i<br />

stores a subset of the entries of D sorted by a nondecreas<strong>in</strong>g key plus entries with two<br />

special keys, denoted −∞ <strong>and</strong> +∞, where −∞ is smaller than every possible key that<br />

can be <strong>in</strong>serted <strong>in</strong> D <strong>and</strong> +∞ is larger than every possible key that can be <strong>in</strong>serted <strong>in</strong><br />

D. In addition, the lists <strong>in</strong> S satisfy the follow<strong>in</strong>g:<br />

• List S 0 conta<strong>in</strong>s every entry of dictionary D (plus the special entries with keys −∞<br />

<strong>and</strong> +∞).<br />

• For i = 1, ..., h − 1, list S i conta<strong>in</strong>s (<strong>in</strong> addition to −∞ <strong>and</strong> +∞) a r<strong>and</strong>omly<br />

generated subset of the entries <strong>in</strong> list S i−1 .<br />

• List S h conta<strong>in</strong>s only −∞ <strong>and</strong> +∞.<br />

558

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

Saved successfully!

Ooh no, something went wrong!