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