more on randomized data structures. motivation for randomized data structures we’ve seen many data...
TRANSCRIPT
More on Randomized Data
Structures
Motivation for
Randomized Data StructuresWe’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 TimeAverage (1/N) xi
Expectation Pr(xi) xi
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
TreapsRandomized 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
1512
1030
915
78
418
67
29
prioritykey
Treap Insert Choose a random priority Insert as in normal BST Rotate up until heap order is restored
67
insert(15)
78
29
1412
67
78
29
1412
915
67
78
29
915
1412
Tree + Heap… Why Bother? Insert data in sorted order into a treap …
What shape tree comes out?
67
insert(7)
67
insert(8)
78
67
insert(9)
78
29
67
insert(12)
78
29
1512
Treap Delete Find the key Increase its value to Rotate it to the fringe Snip it off
delete(9)
67
78
29
915
1512
78
67
9
915
1512
Treap Delete (2)
78
67
9
915
1512
78
67
9
915
1512
78
67
9
915
1512
Treap Delete (3)
78
67
9
915
1512
9
78
67
915
1512
78
67
915
1512
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