1 / 40

Skip Lists

Skip Lists. Linked Lists. Fast modifications given a pointer Slow traversals to random point. Linked Lists. Fast modifications given a pointer Slow traversals to random point What if we add an express lane?. Linked Lists.

graiden-kim
Télécharger la présentation

Skip Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Skip Lists

  2. Linked Lists • Fast modifications given a pointer • Slow traversals to random point

  3. Linked Lists • Fast modifications given a pointer • Slow traversals to random point • What if we add an express lane?

  4. Linked Lists • Find(x): Slide right while next <= x Move down Slide right while next <= x If current == x return true else return false

  5. Fast Lane • Optimal balance?

  6. Fast Lane • Optimal balance? • 1 node in fast lane would cut work in half • 2 nodes in fast lane would cut work into third • …

  7. Fast Lane • Optimal balance? • 1 node in fast lane would cut work in half • 2 nodes in fast lane would cut work into third • … • n nodes in fast lane would not save any time

  8. Fast Lane • Assume fast lane (L2) breaks up slow lane into equal sized pieces:

  9. Fast Lane • Minmal cost comes when spend equal time on both lists • So:

  10. Fast Lane • Thus

  11. Fast Lane • Minmal cost comes when spend equal time on both lists

  12. Fast Lanes What if we have more than one fast lane?

  13. Fast Lanes What if we have more than one fast lane? 3 level: 4 level: …

  14. Fast Lanes • k levels: • If k = log2n: cost = log2n· = log2n· = log2n· = 2log2n = O(logn)!!!

  15. So… • log(n) performance if levels = log2n • How do we get log2 levels?

  16. So… • log(n) performance if levels = log2n • How do we get log2 levels?

  17. Probabilistic Structure • Adding a node: • Find location in bottom list • Add it • While coin flip is heads • Add to level above current

  18. Real Structure • Can be implemented as • Quad node • Node with just down/right poitners • Ends marked with Sentinel nodes or traditional head/null

  19. Real Structure • Can be implemented as • Quad node • Node with just down/right pointers • Ends marked with Sentinel nodes or traditional head/null

  20. Real Structure • Or nodes can be array of pointers • Array size determined by coin flips for each new node • "Head" node set to some maximum size

  21. Expectations • Lowest level = n nodes • Next level = n/2 nodes • Next level = n/4 nodes … Where do we expect last node?

  22. Expectations • Lowest level = n nodes 100 • Next level = n/2 nodes 50 • Next level = n/4 nodes 25 … Where do we expect last node?

  23. Expectations • Where do we expect last node? • kth level = n/2k nodes 1 = n/2k 2k = n log22k = log2n k = log2(n)  Expected height One node level

  24. Total Nodes • Many values will be represented more than once: • In n values, how many nodes?

  25. Total Nodes • Expected nodes where n = number values:n + n/2 + n/4 + n/8…orn·(1 + 1/2 + 1/4 + 1/8 + …)

  26. Series • What is 1 + 1/2 + 1/4 + 1/8 + …?

  27. Series • What is 1 + 1/2 + 1/4 + 1/8 + …? Call x 1 + 1/2 + 1/4 + 1/8 + … Then 2x is 2 + 1 + 1/2 + 1/4 + ….

  28. Series • What is 1 + 1/2 + 1/4 + 1/8 + …? Call x 1 + 1/2 + 1/4 + 1/8 + … Then 2x is 2 + 1 + 1/2 + 1/4 + …. 2x – x = 2

  29. Series • What is 1 + 1/2 + 1/4 + 1/8 + …? Call x 1 + 1/2 + 1/4 + 1/8 + … Then 2x is 2 + 1 + 1/2 + 1/4 + …. 2x – x = 2 2x – x also = x 2 = x = 1 + 1/2 + 1/4 + 1/8…

  30. Total Nodes • Expected nodes where n = number values:n + n/2 + n/4 + n/8…orn·(1 + 1/2 + 1/4 + 1/8 + …)or n·2 • Expected nodes is 2nAverage node has a height of 2

  31. Search Efficiency • How long should we expect search to take

  32. Search Efficiency Total distance = moves up + moves over

  33. Search Efficiency Total distance = moves up + moves over = O(logn) + ??

  34. Search Efficiency Each move over has 50% chance to get to taller node

  35. Search Efficiency Each move over has 50% chance to get to taller node Expect two moves over before moving up

  36. Search Efficiency Expected max moves up = logn Expected moves over = 2logn = O(logn)

  37. Search Efficiency Total distance = moves up + moves over = O(logn) + O(logn) = O(logn)

  38. Insert/Delete • Find node/location = O(logn) • Update pointers • Expected average = 2 levels • Expected max = logn • Total = logn + logn = O(logn)

  39. So what… • Alternative to AVL / RedBlack tree • Easier to implement • Easier to implement concurrency in • Tree rebalancing can have global affect

  40. Examples • Basis of Java library Concurrent Map • MemSQL storage mechanism:

More Related