Download presentation
Presentation is loading. Please wait.
Published byFrancis Merritt Modified over 9 years ago
1
More on Randomized Data Structures
2
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
3
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!
4
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.
5
Randomization in Action Quicksort Randomized data structures Treaps Randomized skip lists
6
Treap Dictionary Data Structure Treap is a BST binary tree property search tree property Treap is also a heap heap-order property random priorities 15 12 10 30 9 15 7878 4 18 6767 2929 priority key
7
Treap Insert Choose a random priority Insert as in normal BST Rotate up until heap order is restored 6767 insert(15) 7878 2929 14 12 6767 7878 2929 14 12 9 15 6767 7878 2929 9 15 14 12
8
Tree + Heap… Why Bother? Insert data in sorted order into a treap … What shape tree comes out? 6767 insert(7) 6767 insert(8) 7878 6767 insert(9) 7878 2929 6767 insert(12) 7878 2929 15 12
9
Treap Delete Find the key Increase its value to Rotate it to the fringe Snip it off delete(9) 6767 7878 2 9 15 12 7878 6767 99 9 15 12
10
Treap Delete (2) 7878 6767 99 9 15 12 7878 6767 99 9 15 12 7878 6767 99 9 15 12
11
Treap Delete (3) 7878 6767 99 9 15 12 99 7878 6767 9 15 12 7878 6767 9 15 12
12
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.