Professional Documents
Culture Documents
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 + and -
List S0 contains the keys of S in non-decreasing order
Each list is a subsequence of the previous one, i.e.,
S0 S1 … Sh
List Sh contains only the two special keys
Skip lists are one way to implement the dictionary ADT
Java applet
S3
S2 31
S1 23 31 34 64
S0 12 23 26 31 34 44 56 64 78
Implementation
link to the node after
link to the node below
x
Also, we define special keys
PLUS_INF and MINUS_INF,
and we modify the key
comparator to handle them
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 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
S3
S2 31
S1 23 31 34 64
S0 12 23 26 31 34 44 56 64 78
We search for a key x in a a skip list as follows:
We start at the first position of theExercise: Search
top list
At the current position p, we compare x with y 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
Ex 1: search for 64: list the (Si, node) pairs visited and the return value
Ex 2: search for 27: list the (Si, node) pairs visited and the return value
S3
S2 31
S1 23 31 34 64
S0 12 23 26 31 34 44 56 64 78
Insertion
S3
p2
S2 34 S2
p1
S1 23 34 S1 23
p0
S0 12 23 34 45 S0 12 23 45
Randomized Algorithms
Through probabilistic analysis we can
A randomized algorithm controls its
derive the expected running time of a
execution through random selection (e.g.,
randomized algorithm
coin tosses)
We make the following assumptions in the
It contains statements like:
analysis:
b randomBit() the coins are unbiased
if b = 0 the coin tosses are independent
The worst-case running time of a
do A … randomized algorithm is often large but
else { b = 1} has very low probability (e.g., it occurs
when all the coin tosses give “heads”)
do B … We use a randomized algorithm to insert
Its running time depends on the outcomes items into a skip list to insert in expected
of the coin tosses
O(log n)-time
When randomization is used in data
structures they are referred to as
probabilistic data structures
Space Usage
The running time of the search an Consider a skip list with n items
insertion algorithms is affected by the By Fact 1, we insert an item in list Si
height h of the skip list
with probability 1/2i
We show that with high probability, a
By Fact 3, the probability that list S
skip list with n items has height O(log n) i
We use the following additional has at least one item is at most n/2i
probabilistic fact: By picking i = 3log n, we have that the
Fact 3: If each of n events probability that S3log n has at least one
item is
has probability p, the at most
probability that at least n/23log n = n/n3 = 1/n2
one event occurs is at most Thus a skip list with n items has height
np at most 3log n with probability at least 1
- 1/n2
Search and Update Times
The search time in a skip list When we scan forward in a list, the
is proportional to destination key does not belong to a
higher list
the number of drop-down steps, plus
A scan-forward step is associated with
the number of scan-forward steps a former coin toss that gave tails
The drop-down steps are By Fact 4, in each list the expected
bounded by the height of the number of scan-forward steps is 2
skip list and thus are O(log n) Thus, the expected number of scan-
with high probability forward steps is O(log n)
We conclude that a search in a skip
To analyze the scan-forward
list takes O(log n) expected time
steps, we use yet another The analysis of insertion and deletion
probabilistic fact: gives similar results
Fact 4: The expected number of coin
tosses required in order to get tails is
2
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”
S2
S1 Enterprise