Skip Lists
-
Upload
graiden-kim -
Category
Documents
-
view
47 -
download
3
description
Transcript of Skip Lists
Linked Lists
• Fast modifications given a pointer• Slow traversals to random point
• What if we add an express lane?
Linked Lists
• Find(x): Slide right while next <= x Move down Slide right while next <= x If current == x return true else return false
Fast Lane
• Optimal balance?– 1 node in fast lane would cut work in half– 2 nodes in fast lane would cut work into third– …
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
Probabilistic Structure
• Adding a node:– Find location in bottom list– Add it– While coin flip is heads• Add to level above current
Real Structure
• Can be implemented as– Quad node– Node with just down/right poitners
• Ends marked with Sentinel nodes or traditional head/null
Real Structure
• Can be implemented as– Quad node– Node with just down/right pointers
• Ends marked with Sentinel nodes or traditional head/null
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
Expectations
• Lowest level = n nodes• Next level = n/2 nodes• Next level = n/4 nodes
…
Where do we expect last node?
Expectations
• Lowest level = n nodes 100• Next level = n/2 nodes 50• Next level = n/4 nodes 25
…
Where do we expect last node?
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
Total Nodes
• Expected nodes where n = number values:n + n/2 + n/4 + n/8…orn·(1 + 1/2 + 1/4 + 1/8 + …)
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 + ….
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
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…
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
Search Efficiency
Each move over has 50% chance to get to taller node
Expect two moves over before moving up
Insert/Delete
• Find node/location = O(logn)• Update pointers– Expected average = 2 levels– Expected max = logn
• Total = logn + logn
= O(logn)
So what…
• Alternative to AVL / RedBlack tree• Easier to implement• Easier to implement concurrency in– Tree rebalancing can have global affect