Skiplists PPT

Download Report

Transcript Skiplists PPT

CSC 172
DATA STRUCTURES
SKIP LISTS
Read Weiss 10.4.2
SKIP LISTS
 Dictionary
 Efficient
Data Structure
SKIP LISTS
 Dictionary
Data Structure (insert delete lookup)
 Efficient O(lg n)
SKIP LISTS
 Dictionary
Data Structure (insert delete lookup)
 Efficient O(lg n)
SKIP LISTS
 Dictionary
Data Structure
 Efficient (with high probability)
 Randomized
SKIP LISTS
 Dictionary
Data Structure
 Efficient (with high probability)
 Randomized
 Easy to implement
LISTS
 How
much time does it take to search a sorted
linked list?
 How can this be improved?
EXAMPLE
EXAMPLE
 What
is this sequence?
14,23,28,34,42,50,59,66,72,
79,86,96,103,110
EXAMPLE
 What
is this sequence?
14,23,34,42,50,59,66,72,
79,86,96,103,110
EXAMPLE
 What
is this sequence?
14,23,34,42,50,59,66,72,
79,86,96,103,110,116,125
SKIP LISTS
 Use
two lists
 L2 stores all element
 L1 stores some elements
 Links between shared elements
Lookup on a skip list
Lookup on a skip list
1)Take L1 until you go too far
2)Back up one
3)Transfer to L2
4)Take L2 until you find element
(or go too far – not found – or insert)
Lookup on a skip list
How should we distribute the L1 list?
What is the time cost of a search?
Lookup on a skip list
How should we distribute the L1 list?
What is the time cost of a search?
Minimize : L1.length + (L2.length/L1.length)
NEXT STEP
2
linked lists 2(n^(1/2))
 Can we improve this further?
NEXT STEP
2
linked lists 2(n^(1/2))
 Can we improve this further?
 3 linked lists 3(n^(1/3))
 k linked lists k(n^(1/k))
 N linked lists ????
 lg n linked lists lg n (n^(1/lg n))
BALLANCED SKIP LISTS
 Ideal
as long as structure is maintained
BALLANCED SKIP LISTS
 Ideal
as long as structure is maintained
 Insertions and deletions mess up structure
INSERTION ON SKIP LISTS
 Search
to find location
 Must insert on bottom list
 Which other lists?
 FLIP A COIN
 If
heads add to level above and flip again.
 If tails done.
INSERTION ON SKIP LISTS
 FLIP A COIN
 If
heads add to level above and flip again.
 If tails done.
½ of the elements go up one level
¼ of the elements go up 2 levels
1/8 of the elements go up 3 levels
INSERTION ON SKIP LISTS
 EXAMPLE
ANALYSIS
 Intuitively:
 Height
of the structure is O(lg n)
 How many coin flips do we need to get lg n heads?