Sei sulla pagina 1di 16

Skip Lists

Skip lists were invented by William Pugh in 1989. They were proposed by him in 1990 as an alternative to the BST (Binary Search Tree) and other balanced trees.

ADS

Definition
A skip list is a probabilistic data structure where elements are kept sorted by key. It allows quick search, insertions and deletions of elements with simple algorithms. It is basically a linked list with additional pointers such that intermediate nodes can be skipped. It uses a random number generator to make some decisions.

ADS

Use
Skip Lists are used as an alternative to balanced trees. A skip list is a practical data structure that gives good results while keeping the implementation simple.

ADS

Ideal case
A linked list is a relatively easy data structure to implement. It is simple to keep a linked list of n elements sorted. To perform a search n comparisons are required in the worst case. Now, suppose a second pointer pointing two nodes ahead is added to every other node, the number of comparisons required goes down to ceil(n/2)+1 (in the worst case). Adding one more pointer to every fourth node and making them point to the fourth node ahead reduces the number of comparisons to ceil(n/2)+2. If that strategy is continued so every node with i pointers points to 2i-1 nodes ahead, O(log n) performance is obtained and the number of pointers has only doubled (n + n/2 + n/4 + n/8 + n/16 + .... = 2n).
ADS 4

Ideal case illustrated

The data structure described above is great for searching but insertions and deletions would be really difficult to implement and inefficient as almost all the pointers must be modified. Maintaining perfect balancing is time consuming. This data structure can be made more flexible for simple insert and delete operations. This can be done quite easily while keeping a good (though not perfect) balancing as follows:
ADS 5

Skip list probabilistic approach


The level of every new node to be inserted is chosen randomly with a probability distribution that keeps approximately the proportions of nodes of level i as described above. Probabilistic analysis shows that the average cost of insert, delete and search operations is O(log n).

ADS

Skip lists operations are O(n) in the worst case. It happens when all (or almost all) the nodes are given level 1 at insertion. In that case, the skip list becomes an ordinary linked list. As this case is highly unlikely to happen as n increases. For example, the probability that 10 nodes in a row are at level 1 is 1/1024! Skip lists may be viewed as trees where the header is the root, the nodes levels correspond to the levels in a ADS 7 tree and level 1 nodes are the leaves.

What is a Skip List


A skip list for a set S of distinct (key, element) items is a series of lists S0, S1 , , Sh such that Each list Si contains the special keys g and g List S0 contains the keys of S in nondecreasing order Each list is a subsequence of the previous one, i.e., S0 S1 Sh List Sh contains only the two special keys We show how to use a skip list to implement the dictionary ADT
g g g g
12 23 23 26 31 31 31
ADS

S3 S2 S1 S0

g g
34 34 44 56 64 64 78

g g
8

Search
We search for a key x in a a skip list as follows: We start at the first position of the top list At the current position p, we compare x with y n key(after(p)) x ! y: we return element(after(p)) x " y: we scan forward x y: we drop down If we try to drop down past the bottom list, we return NO_SUCH_KEY Example: search for 78 78 < + g S3 g g 78 < + g 31 S2 g g 78 < + g 23 31 34 64 S1 g g

S0

g

12

23

26

31
ADS

34

44

56

64

78

g
9

Randomized Algorithms
A randomized algorithm performs coin tosses (i.e., uses random bits) to control its execution It contains statements of the type b n random() if b ! 0 do A else { b ! 1} do B Its running time depends on the outcomes of the coin tosses We analyze the expected running time of a randomized algorithm under the following assumptions the coins are unbiased, and the coin tosses are independent The worst-case running time of a randomized algorithm is often large but has very low probability (e.g., it occurs when all the coin tosses give heads) We use a randomized algorithm to insert items into a skip list

ADS

10

Insertion
To insert an item (x, o) into a skip list, we use a randomized algorithm:
We repeatedly toss a coin until we get tails, and we denote with i the number of times the coin came up heads If i u h, we add to the skip list new lists Sh1, , Si 1, each containing only the two special keys We search for x in the skip list and find the positions p0, p1 , , pi of the items with largest key less than x in each list S0, S1, , Si For j n 0, , i, we insert item (x, o) into list Sj after position pj

Example: insert key 15, with i ! 2 S3 g


g
15 15 10 15 23 23 36

p2 S2 g p1 S1 g S0 g

g
23

S2 g S1 g S0 g
ADS

g g g
11

p0
10 23 36

g g

Deletion
To remove an item with key x from a skip list, we proceed as follows:
We search for x in the skip list and find the positions p0, p1 , , pi of the items with key x, where position pj is in list Sj We remove positions p0, p1 , , pi from the lists S0, S1, , Si We remove all but one list containing only the two special keys

Example: remove key 34

S3 g p2 S2 g S1 g S0 g
12 23 23 34

g g

S2 g S1 g S0 g
12 23 23 45

g g g
12

p1
34

g

p0
34 45

g
ADS

Implementation
We can implement a skip list with quad-nodes A quad-node stores:

item link to the node before link to the node after link to the node below link to the node after

quad-node

Also, we define special keys PLUS_INF and MINUS_INF, and we modify the key comparator to handle them

A skip list with n items has height O(log n)


ADS 13

Search and Update Times


The search time in a skip list is proportional to the number of drop-down steps, plus the number of scan-forward steps The drop-down steps are bounded by the height of the skip list and thus are O(log n) with high probability To analyze the scan-forward steps, we use yet another probabilistic fact: Fact 4: The expected number of coin tosses required in order to get tails is 2 When we scan forward in a list, the destination key does not belong to a higher list A scan-forward step is associated with a former coin toss that gave tails By Fact 4, in each list the expected number of scan-forward steps is 2 Thus, the expected number of scanforward steps is O(log n) We conclude that a search in a skip list takes O(log n) expected time The analysis of insertion and deletion gives similar results

ADS

14

Summary
A skip list is a data structure for dictionaries that uses a randomized insertion algorithm In a skip list with n items The expected space used is O(n) The expected search, insertion and deletion time is O(log n) Using a more complex probabilistic analysis, one can show that these performance bounds also hold with high probability Skip lists are fast and simple to implement in practice

ADS

15

This presentation & the practice problems will be available in the course page.
http://discovery.bits-pilani.ac.in/discipline/csis/vimal/course%2007-08%20First/DSA_Offcampus/DSA.htm

Questions ?

ADS

16

Potrebbero piacerti anche