Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees...

38
Skip Lists 1 Skip Lists William Pugh: ”Skip Lists: A Probabilistic Alternative to Balanced Trees”, 1990 S 0 S 1 S 2 S 3 10 36 23 15 15 23 15

Transcript of Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees...

Page 1: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 1

Skip ListsWilliam Pugh: ”Skip Lists: A Probabilistic Alternative to Balanced Trees”, 1990

S0

S1

S2

S3

10 362315

15

2315

Page 2: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 2

Outline and Reading

What is a skip listOperations Search Insertion Deletion

ImplementationAnalysis Space usage Search and update times

Page 3: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Intro to Skip ListsMotivation: Hashing is a way to extend arrays to for

map/dictionary How to extend linked lists (LL) for

map/dictionary? Unordered LL: fast insertion, slow search Ordered LL: slow insertion, slow search

Basic idea of skip lists Organize ordered list hierarchically so we

don’t need to scan all elements in search

Skip Lists 3

Page 4: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 4

What is a Skip ListA skip list for a set S of distinct keys is a series of lists S0, S1 , … , Sh such that

Each list Si contains the special keys and 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

56 64 78 31 34 44 12 23 26

31

64 31 34 23

S0

S1

S2

S3

Page 5: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 5

SearchSteps for search a key x in a a skip list:

Start at the first position of the top list At the current position p, we compare x with y key(next(p))

x y: Return next(p)x y: Scan forward x y: Drop down

Repeat the above step. (If “drop down” pasts the bottom list, return null.)

Example: search for 78

S0

S1

S2

S3

31

64 31 34 23

56 64 78 31 34 44 12 23 26

© 2010 Goodrich, Tamassia

scan forward

drop down

Find the intervalwhere x belong to…

Page 6: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 6

Page 7: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 7

Page 8: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 8

Page 9: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 9

Page 10: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 10

Page 11: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 11

Page 12: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 12

Page 13: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 13

Page 14: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 14

Page 15: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 15

Page 16: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 16

Page 17: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 17

Page 18: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 18

Page 19: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 19

Page 20: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 20

Page 21: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 21

Page 22: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 22

Page 23: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 23

Page 24: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 24

Page 25: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 25

Page 26: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 26

Page 27: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 27

Page 28: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 28

Page 29: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Implementation (1/2)

Skip Lists 29

Resutls due to different

randomization

Anotherlinked list

implementation

Page 30: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 30

Implementation (2/2)We can implement a skip list with quad-nodesA quad-node stores:

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

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

x

quad-node

Page 31: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 31

Outline and Reading

What is a skip listOperations Search Insertion Deletion

ImplementationAnalysis Space usage Search and update times

Page 32: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 32

Randomized AlgorithmsA randomized algorithm performs coin tosses (i.e., uses random bits) to control its executionIt contains statements of the type

b 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

Page 33: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 33

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 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 0, …, i, we insert item (x, o) into list Sj after position pj

Example: insert key 15, with i 2

Insertion

10 36

23

23

S0

S1

S2

S0

S1

S2

S3

10 362315

15

2315p0

p1

p2

n nodesn/2 nodesin average

n/4 nodesin average

Page 34: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 34

DeletionTo 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

4512

23

23

S0

S1

S2

S0

S1

S2

S3

4512 23 34

34

23 34p0

p1

p2

Page 35: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 35

Space UsageThe space used by a skip list depends on the random bits used by each invocation of the insertion algorithmWe use the following two basic probabilistic facts:Fact 1: The probability of

getting i consecutive heads when flipping a coin is 12i

Fact 2: If each of n items is present in a set with probability p, the expected size of the set is np

Consider a skip list with n items

By Fact 1, we insert an item in list Si with probability 12i

By Fact 2, the expected size of list Si is n2i

The expected number of nodes used by the skip list is

nnnn

h

h

ii

h

ii

22

12

2

1

2 00

Thus, the expected space usage of a skip list with n items is O(n)

Page 36: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 36

HeightThe running time of the search an insertion algorithms is affected by the height h of the skip listWe show that with high probability, a skip list with n items has height O(log n)

We use the following additional probabilistic fact:Fact 3: If each of n events has

probability p, the probability that at least one event occurs is at most np

Consider a skip list with n items

By Fact 1, we insert an item in list Si with probability 12i

By Fact 3, the probability that list Si has at least one item is at most n2i

By picking i 3log n, we have that the probability that S3log n has at least one item isat most

n23log n nn3 1n2

Thus a skip list with n items has height at most 3log n with probability at least 1 1n2

Page 37: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Search and Update Times

The search time in a skip list is proportional to the sum of

#drop-downs #scan-forwards

#drop-downs Bounded by the height of

the skip list O(log n)

#scan-forwards Each scan forward bounded

by nodes in an interval O(2) in average for each scan forward O(log n) overall.

Thus the complexity for search in a skip list is O(log n)The analysis of insertion and deletion gives similar results

Skip Lists 37

Page 38: Skip Lists1 Skip Lists William Pugh: ” Skip Lists: A Probabilistic Alternative to Balanced Trees ”, 1990  S0S0 S1S1 S2S2 S3S3  103623 15.

Skip Lists 38

Summary

A skip list is a data structure for dictionaries that uses a randomized insertion algorithmIn 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 probabilitySkip lists are fast and simple to implement in practice