Docsity

Skip Lists: A Probabilistic Data Structure for Dictionaries, Slides of Java Programming

Toggle sidebar
bg1
Skip Lists
Calin Curescu, from Goodrich,Tamassia
Skip Lists
Calin Curescu, from Goodrich,Tamassia
12 pages
TDDC32 lecture 5, 2006
Skip Lists
Calin Curescu, from Goodrich,Tamassia
2of 12
TDDC32 lecture 5, 2006
What is a Skip List
•A skip list for a set Sof distinct (key, element) items is a series of
lists S0, S1 , … , Shsuch that
– Each list Sicontains the special keys +∞ and −∞
–List S0contains 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
56 64 78 +∞
31 34 44
−∞ 12 23 26
+∞−∞
+∞
31
−∞
64 +∞
31 34
−∞ 23
S0
S1
S2
S3
pf3
pf4
pf5

Partial preview of the text

Download Skip Lists: A Probabilistic Data Structure for Dictionaries and more Slides Java Programming in PDF only on Docsity!

Skip Lists Calin Curescu, from Goodrich,Tamassia

Skip Lists

Calin Curescu, from Goodrich,Tamassia

12 pages TDDC32 lecture 5, 2006

Skip Lists 2 of 12

What is a Skip List

  • A skip list for a set S of distinct (key, element) items is a series of lists S 0 , S 1 , … , S (^) h such that - Each list Si contains the special keys +∞ and −∞ - List S 0 contains the keys of S in nondecreasing order - Each list is a subsequence of the previous one, i.e., S 0 ⊇ S 1 ⊇ … ⊇ Sh - List Sh contains only the two special keys
  • We show how to use a skip list to implement the dictionary ADT

−∞^31 +∞

S 0

S 1

S 2

S 3

Skip Lists Calin Curescu, from Goodrich,Tamassia

3 of 12 TDDC32 lecture 5, 2006

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 ( next ( p )) x = y : we return element ( next ( p )) x > y : we “scan forward” x < y : we “drop down”
    • If we try to drop down past the bottom list, we return null
  • Example: search for 78

S 0

S 1

S 2

S 3

−∞^31 +∞

−∞ 23 31 34 64 +∞

−∞ 12 23 26 31 34 44 56 64 78 +∞

Skip Lists 4 of 12

Randomized Algorithms

  • A randomized algorithm performs coin tosses (i.e., uses random bits) to control its execution
  • It 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

Skip Lists Calin Curescu, from Goodrich,Tamassia

7 of 12 TDDC32 lecture 5, 2006

Implementation

  • We can implement a skip list with quad-nodes
  • A quad-node stores:
    • entry
    • link to the node prev
    • link to the node next
    • 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

Skip Lists 8 of 12

Space Usage

  • 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 i consecutive heads when flipping a coin is 1 / 2 i Fact 2: If each of n entries is present in a set with probability p , the expected size of the set is np - Consider a skip list with n entries - By Fact 1, we insert an entry in list Si with probability 1 / 2 i - By Fact 2, the expected size of list Si is n / 2 i - The expected number of nodes used by the skip list is

n n

n h

i

i

h

i

i 2 2

∑ =^ ∑^ <

= =

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

Skip Lists Calin Curescu, from Goodrich,Tamassia

9 of 12 TDDC32 lecture 5, 2006

Height

  • The running time of the search an insertion algorithms is affected by the height h of the skip list
  • We 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 entries - By Fact 1, we insert an entry in list S (^) i with probability 1 / 2 i - By Fact 3, the probability that list S (^) i has at least one item is at most n / 2 i - By picking i = 3log n , we have that the probability that S 3log n has at least one entry is at most n / 2 3log^ n^ = n / n^3 = 1 / n^2 - Thus a skip list with n entries has height at most 3log n with probability at least 1 − 1 / n^2

Skip Lists 10 of 12

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 scan- forward 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