CMSC 420 Skip Lists Kinga Dobolyi Based off
CMSC 420: Skip Lists Kinga Dobolyi Based off notes by Dave Mount
What is the purpose of this class? • Data storage – Speed • Trees – Binary search tree • Could degenerate into a linked list – AVL tree • Better performance, difficult to implement – Splay tree • Good overall performance -- amortized
Can we do “better” • Linked list – Simple to implement – Poor runtime: O(n) • Better runtime – Guaranteed O(logn) – Difficult to implement • Can we have both for general data? – What are the tradeoffs?
Skip Lists • Generalization of a linked list • O(logn) runtime of balanced trees • What is the trick? – Randomized data structure • Efficient in the expected case • Unlike BST, the expected case has nothing to do with the order of the keys being inserted • Probability that it performs badly is very small
General idea • How can we make linked lists better? – Why are lists crummy? What takes forever? • What if we could skip over many elements at a time?
Implementation • Start with a sorted linked list – Add another layer linking every other element – Repeat for that layer, etc • Think of as a hierarchy of sorted linked lists
Skip list runtime • How high does this stack go? – Level 0: n – Level 1: n/2 – Level 2: n/4 • Height of stack will be logn • So search through the skiplist will be O(logn) • But can we maintain this guarantee of efficiency?
Perfect vs Random Skip List • What we just described is a perfect skip list – What happens when you add or delete elements? • To maintain perfect balance • Random skip list – Need to decide when to promote a node to some i level – Use randomization, with a chance of 50%, to decide when to promote a node
Random Skip list • If, on average, we promote half of the nodes at every level – Our skiplist will maintain this O(logn) property in the expected case – It is also likely that, overall, nodes will have a uniform distribution and not bunch up at the ends
Insertion
Exercise • Argue that the expected runtime of search in a skiplist is in O(logn) • What about insertion and deletion?
Easy to implement • For details, see Dave Mount’s notes (linked from website)
- Slides: 12