More on Randomized Data Structures
Motivation for Randomized Data Structures We’ve seen many data structures with good average case performance on random inputs, but bad behavior on particular inputs e.g. Binary Search Trees Instead of randomizing the input (since we cannot!), consider randomizing the data structure No bad inputs, just unlucky random numbers Expected case good behavior on any input
Average vs. Expected Time Average(1/N) x i Expectation Pr(x i ) x i Deterministic with good average time If your application happens to always (or often) use the “bad” case, you are in big trouble! Randomized with good expected time Once in a while you will have an expensive operation, but no inputs can make this happen all the time Like an insurance policy for your algorithm!
Randomized Data Structures Define a property (or subroutine) in an algorithm Sample or randomly modify the property Use altered property as if it were the true property Can transform average case runtimes into expected runtimes (remove input dependency). Sometimes allows substantial speedup in exchange for probabilistic unsoundness.
Randomization in Action Quicksort Randomized data structures Treaps Randomized skip lists
Treap Dictionary Data Structure Treap is a BST binary tree property search tree property Treap is also a heap heap-order property random priorities priority key
Treap Insert Choose a random priority Insert as in normal BST Rotate up until heap order is restored 6767 insert(15)
Tree + Heap… Why Bother? Insert data in sorted order into a treap … What shape tree comes out? 6767 insert(7) 6767 insert(8) insert(9) insert(12)
Treap Delete Find the key Increase its value to Rotate it to the fringe Snip it off delete(9) 9
Treap Delete (2) 9 9 9
Treap Delete (3) 9 9
Treap Summary Implements Dictionary ADT insert in expected O(log n) time delete in expected O(log n) time find in expected O(log n) time but worst case O(n) Memory use O(1) per node about the cost of AVL trees Very simple to implement little overhead – less than AVL trees