pptx - Parasol Lab
Download
Report
Transcript pptx - Parasol Lab
CH 9.4 : SKIP LISTS
ACKNOWLEDGEMENT:THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN
C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY M. AMATO AND JORY DENNY
1
RANDOMIZED ALGORITHMS
ο‘ A randomized algorithm controls its execution
through random selection (e.g., coin tosses)
ο‘ It contains statements like:
π β randomBit()
if π = 0
do somethingβ¦
else //π = 1
do something elseβ¦
ο‘ Its running time depends on the outcomes of the
βcoin tossesβ
ο‘ Through probabilistic analysis we can derive the
expected running time of a randomized algorithm
ο‘ We make the following assumptions in the analysis:
ο‘
the coins are unbiased
ο‘
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 to insert in expected π(log π)βtime
ο‘ When randomization is used in data structures they are
referred to as probabilistic data structures
WHAT IS A SKIP LIST?
ο‘ A skip list for a set S of distinct (key, element) items is a series of lists
π0 , π1 , β¦ , πβ
ο‘
Each list ππ contains the special keys +β and ββ
ο‘
List π0 contains the keys of π in non-decreasing order
ο‘
Each list is a subsequence of the previous one, i.e.,
π0 β π1 β β― β πβ
ο‘
List πβ contains only the two special keys
ο‘ Skip lists are one way to implement the Ordered Map ADT
ο‘ Java applet
S3 -ο₯
+ο₯
S2 -ο₯
S1 -ο₯
S0 -ο₯
+ο₯
31
23
12
23
26
31
34
31
34
+ο₯
64
44
56
64
78
+ο₯
3
IMPLEMENTATION
ο‘ We can implement a skip list with quad-nodes
ο‘ A quad-node stores:
ο‘
(Key, Value)
ο‘
links to the nodes before, after, below, and above
quad-node
x
ο‘ Also, we define special keys +β and ββ, and we
modify the key comparator to handle them
4
SEARCH - FIND(π)
ο‘ We search for a key π in a skip list as follows:
ο‘
We start at the first position of the top list
ο‘
At the current position π, we compare π with π¦ β π. next(). key()
π₯ = π¦: we return π. next(). value()
π₯ > π¦: we scan forward
π₯ < π¦: we drop down
ο‘
If we try to drop down past the bottom list, we return NO_SUCH_KEY
ο‘ Example: search for 78
S3 -ο₯
+ο₯
S2 -ο₯
S1 -ο₯
S0 -ο₯
+ο₯
31
23
12
23
26
31
34
31
34
+ο₯
64
44
56
64
78
+ο₯
5
EXERCISE
SEARCH
ο‘ We search for a key π in a skip list as follows:
ο‘
We start at the first position of the top list
ο‘
At the current position π, we compare π with π¦ β π. next(). key()
π₯ = π¦: we return π. next(). value()
π₯ > π¦: we scan forward
π₯ < π¦: we drop down
ο‘
If we try to drop down past the bottom list, we return NO_SUCH_KEY
ο‘ Ex 1: search for 64: list the (ππ , node) pairs visited and the return value
ο‘ Ex 2: search for 27: list the (ππ , node) pairs visited and the return value
S3 -ο₯
+ο₯
S2 -ο₯
S1 -ο₯
S0 -ο₯
+ο₯
31
23
12
23
26
31
34
31
34
+ο₯
64
44
56
64
78
+ο₯
6
INSERTION - PUT(π, π£)
ο‘ To insert an item (π, π£) into a skip list, we use a randomized algorithm:
ο‘
We repeatedly toss a coin until we get tails, and we denote with π the number of times the coin came up heads
ο‘
If π β₯ β, we add to the skip list new lists πβ+1 , β¦ , ππ+1 each containing only the two special keys
ο‘
We search for π in the skip list and find the positions π0 , π1 , β¦ , ππ of the items with largest key less than π in each list
π0 , π1 , β¦ , ππ
ο‘
For π β 0, β¦ , π, we insert item (π, π£) into list ππ after position ππ
ο‘ Example: insert key 15, with π = 2
p2
S2 -ο₯
p1
S1 -ο₯
S0 -ο₯
S3 -ο₯
23
p0
10
23
36
+ο₯
+ο₯
S2 -ο₯
15
+ο₯
S1 -ο₯
15
23
+ο₯
S0 -ο₯
15
23
10
+ο₯
+ο₯
36
+ο₯
7
DELETION - ERASE(π)
ο‘ To remove an item with key π from a skip list, we proceed as follows:
ο‘ We search for π in the skip list and find the positions π0 , π1 , β¦ , ππ of the items with key π, where position ππ is
in list ππ
ο‘ We remove positions π0 , π1 , β¦ , ππ from the lists π0 , π1 , β¦ , ππ
ο‘ We remove all but one list containing only the two special keys
ο‘ Example: remove key 34
S3 -ο₯
+ο₯
p2
S2 -ο₯
34
p1
S1 -ο₯
S0 -ο₯
23
34
p0
12
23
34
45
+ο₯
S2 -ο₯
+ο₯
S1 -ο₯
+ο₯
S0 -ο₯
+ο₯
+ο₯
23
12
23
45
+ο₯
8
SPACE USAGE
ο‘ Consider a skip list with π items
ο‘ The space used by a skip list depends on the random
bits used by each invocation of the insertion
algorithm
ο‘ We use the following two basic probabilistic facts:
ο‘
Fact 1: The probability of getting π consecutive heads
1
when flipping a coin is π
ο‘
By Fact 1, we insert an item in list ππ with probability
ο‘
By Fact 2, the expected size of list ππ is
Fact 2: If each of π items is present in a set with
probability π, the expected size of the set is ππ
π
2π
ο‘ The expected number of nodes used by the skip list
is
β
2
ο‘
1
2π
π=0
π
=π
2π
β
π=0
1
< 2π
2π
ο‘ Thus the expected space is π 2π
9
HEIGHT
ο‘ Consider a skip list with π items
ο‘ The running time of find π , put π, π£ , and erase π
operations are affected by the height β of the skip
list
ο‘ We show that with high probability, a skip list with π
items has height π log π
ο‘ We use the following additional probabilistic fact:
ο‘
Fact 3: If each of π events has probability π, the
probability that at least one event occurs is at most ππ
ο‘
By Fact 1, we insert an item in list ππ with
1
probability π
2
ο‘
By Fact 3, the probability that list ππ has at least
π
one item is at most π
2
ο‘ By picking π = 3 log π, we have that the
probability that π3 log π has at least one item is
π
π
1
at most 23 log π = π3 = π2
ο‘ Thus a skip list with π items has height at most
1
3 log π with probability at least 1 β π2
10
SEARCH AND UPDATE TIMES
ο‘ The search time in a skip list is proportional to
ο‘
the number of drop-down steps
ο‘
the number of scan-forward steps
ο‘ The drop-down steps are bounded by the height of
the skip list and thus are π log π expected time
ο‘ 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 scan-forward steps
is π(log π)
ο‘ We conclude that a search in a skip list takes
π log π expected time
ο‘ The analysis of insertion and deletion gives similar
results
11
EXERCISE
ο‘ You are working for ObscureDictionaries.com a new online start-up which specializes in sci-fi
languages. The CEO wants your team to describe a data structure which will efficiently allow for
searching, inserting, and deleting new entries.You believe a skip list is a good idea, but need to
convince the CEO. Perform the following:
ο‘
Illustrate insertion of βX-wingβ into this skip list. Randomly generated (1, 1, 1, 0).
ο‘
Illustrate deletion of an incorrect entry βEnterpriseβ
ο‘
Argue the complexity of deleting from a skip list
S2 -ο₯
+ο₯
S1 -ο₯
S0 -ο₯
+ο₯
Enterprise
Boba Fett
Enterprise
Yoda
+ο₯
12
SUMMARY
ο‘ A skip list is a data structure for dictionaries that
uses a randomized insertion algorithm
ο‘ In a skip list with π items
ο‘
The expected space used is π π
ο‘
The expected search, insertion and deletion time is
π(log π)
ο‘ 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
13