Docsity

CS 361 Lecture 26: Skip Lists and Their Search Time, Study notes of Computer Science

Toggle sidebar
bg1
CS 361, Lecture 26
Jared Saia
University of New Mexico
Outline
•Skip Lists
1
Project
•Any questions on the group project? (hw6)
2
Administrative
•Lab Section evaluation this week
•This week, Kanglin will take attendance at sections, if you’re
there, you’ll get an extra check for participation
•Sections are Thursday 3:30-4:20 and Friday 1:00-1:50
•Good chance to review material for final
3
Project
•Project will be due May 8th in class
•Late projects will not be accepted
•You can get partial credit for an unfinished project turned in
on time but will get no credit for a finished project turned in
late
4
HW
•There will also be a hw due on May 8th in class
•This will be a “final review” hw
5
pf3
pf4
pf5

Partial preview of the text

Download CS 361 Lecture 26: Skip Lists and Their Search Time and more Study notes Computer Science in PDF only on Docsity!

CS 361, Lecture 26

Jared Saia University of New Mexico

Outline

  • Skip Lists

Project

  • Any questions on the group project? (hw6)

Administrative

  • Lab Section evaluation this week
  • This week, Kanglin will take attendance at sections, if you’re there, you’ll get an extra check for participation
  • Sections are Thursday 3:30-4:20 and Friday 1:00-1:
  • Good chance to review material for final

Project

  • Project will be due May 8th in class
  • Late projects will not be accepted
  • You can get partial credit for an unfinished project turned in on time but will get no credit for a finished project turned in late

HW

  • There will also be a hw due on May 8th in class
  • This will be a “final review” hw

Final

  • Final will be Tuesday May 13th, 7:30-9:30am in our regular classroom
  • Closed book, but two pieces of paper are allowed (for cheat sheets)
  • No calculators

High Level Analysis

Comparison of various BSTs

  • RB-Trees: + guarantee O(log n) time for each operation, easy to augment, − high constants
  • AVL-Trees: + guarantee O(log n) time for each operation, − high constants
  • B-Trees: + works well for trees that won’t fit in memory, guarantee O(log n) time for each operation, − inserts and deletes are more complicated
  • Splay Tress: + small constants, − amortized guarantees only
  • Skip Lists: + easy to implement, − runtime guarantees are probabilistic only

Which Data Structure to use?

  • Splay trees work very well in practice, the “hidden constants” are small
  • Unfortunately, they can not guarantee that every operation takes O(log n)
  • When this guarantee is required, B-Trees are best when the entire tree will not be stored in memory
  • If the entire tree will be stored in memory, RB-Trees, AVL- Trees, and Skip Lists are good

Skip List

  • Technically, not a BST, but they implement all of the same operations
  • Very elegant randomized data structure, simple to code but analysis is subtle
  • They guarantee that, with high probability, all the major op- erations take O(log n) time

Skip List

  • A skip list is basically a collection of doubly-linked lists, L 1 , L 2 ,... , Lx, for some integer x
  • Each list has a special head and tail node, the keys of these nodes are assumed to be −MAXNUM and +MAXNUM re- spectively
  • The keys in each list are in sorted order (non-decreasing)

Skip List

  • Every key is in the list L 1.
  • For all i > 2, if a key k is in the list Li, it is also in Li− 1. Further there are up and down pointers between the k in Li and the k in Li− 1.
  • All the head(tail) nodes from neighboring lists are inter- connected

In-Class Exercise

Q: How much memory do we expect a skip list to use up?

  • Let Xi be the number of lists that element i is inserted in.
  • Q: What is P (Xi ≥ 1), P (Xi ≥ 2), P (Xi ≥ 3)?
  • Q: What is P (Xi ≥ k) for general k?
  • Q: What is E(Xi)?
  • Q: Let X = ∑ni=1 Xi. What is E(X)?

Height of Skip List

  • Assume there are n nodes in the list
  • Q: What is the probability that a particular key i achieves height exceeding k log n for some constant k?
  • A: If p = 1/2, P (Xi ≥ k log n) = (^) n^1 k

Height of Skip List

  • Q: What is the probability that any of the nodes achieve height higher than k log n?
  • A: We want P (X 1 ≥ k log n or X 2 ≥ k log n or... or Xn ≥ k log n)
  • By a Union Bound, this probability is no more than

P (X 1 ≥ k log n) + P (X 2 ≥ k log n) + · · · + P (Xn ≥ k log n)

  • Which equals (^) nnk = n^1 −k

Height of Skip List

  • If we choose k to be, say 10, this probability gets very small as n gets large
  • In particular, the probability of having a skip list of size ex- ceeding k log n is o(1)
  • So we say that the height of the skip list is O(log n) with high probability

Search Time

  • Note that the expected number of “siblings” of a node, x, at any level i is 2
  • Why? Because for a node to be a sibling of x at level i, it must have failed to advance to the next level
  • The first node that advances to the next level ends the pos- sibility of further siblings.
  • This is the same as asking expected number of times we need to flip a coin to get a heads.

Flipping to get Heads

  • How many times in expectation do we need to flip a coin to get heads, if the coin is heads with probability p?
  • Let X be a random variable giving the number of times the coin is flipped until we get heads, then E(X) is the expected number of times needed to flip to get heads
  • Then E(X) = 1+(1−p)E(X) since we take 1 flip, plus in the case where the coin is tails (which happens with probability (1 − p)), we then take “the expected number of times needed to flip to get heads” (i.e. we’re no better off than when we started)
  • Solving for E(X) gives E(X) = 1/p. If p = 1/2, then E(X) = 2

Search Time

  • The expected number of “siblings” of a node, x, at any level i is 2
  • The number of levels is O(log n) with high probability
  • From these two facts, we can prove that the expected search time is O(log n) (the proof is omitted)
  • (Warning: The argument is not as simple as multiplying these two values. We can’t do this since the two random variables are not independent.)