Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Luebke 1 5/22/2015 ITCS 6114 Universal Hashing Dynamic Order Statistics.

Similar presentations


Presentation on theme: "David Luebke 1 5/22/2015 ITCS 6114 Universal Hashing Dynamic Order Statistics."— Presentation transcript:

1 David Luebke 1 5/22/2015 ITCS 6114 Universal Hashing Dynamic Order Statistics

2 David Luebke 2 5/22/2015 Choosing A Hash Function ● Choosing the hash function well is crucial ■ Bad hash function puts all elements in same slot ■ A good hash function: ○ Should distribute keys uniformly into slots ○ Should not depend on patterns in the data ● We discussed three methods: ■ Division method ■ Multiplication method ■ Universal hashing

3 David Luebke 3 5/22/2015 Universal Hashing ● When attempting to foil an malicious adversary, randomize the algorithm ● Universal hashing: pick a hash function randomly when the algorithm begins (not upon every insert!) ■ Guarantees good performance on average, no matter what keys adversary chooses ■ Need a family of hash functions to choose from

4 David Luebke 4 5/22/2015 Universal Hashing ● A family of hash functions  is said to be universal if: ■ With a random hash function from , the chance of a collision between x and y is exactly 1/m (x  y) ● We can use this to get good expected performance: ■ Choose h from a universal family of hash functions ■ Hash n keys into a table of m slots, n  m ■ Then the expected number of collisions involving a particular key x is less than 1

5 David Luebke 5 5/22/2015 A Universal Hash Function ● Choose table size m to be prime ● Decompose key x into r+1 bytes, so that x = {x 0, x 1, …, x r } ■ Only requirement is that max value of byte < m ■ Let a = {a 0, a 1, …, a r } denote a sequence of r+1 elements chosen randomly from {0, 1, …, m - 1} ■ Define corresponding hash function h a   : ■ With this definition,  has m r+1 members

6 David Luebke 6 5/22/2015 A Universal Hash Function ●  is a universal collection of hash functions (Theorem 12.4) ● How to use: ■ Pick r based on m and the range of keys in U ■ Pick a hash function by (randomly) picking the a’s ■ Use that hash function on all keys

7 David Luebke 7 5/22/2015 Order Statistic Trees ● OS Trees augment red-black trees: ■ Associate a size field with each node in the tree ■ x->size records the size of subtree rooted at x, including x itself: M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1

8 David Luebke 8 5/22/2015 Selection On OS Trees M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 How can we use this property to select the ith element of the set?

9 David Luebke 9 5/22/2015 OS-Select OS-Select(x, i) { r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r); }

10 David Luebke 10 5/22/2015 OS-Select Example ● Example: show OS-Select(root, 5): M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 OS-Select(x, i) { r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r); }

11 David Luebke 11 5/22/2015 OS-Select Example ● Example: show OS-Select(root, 5): M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 OS-Select(x, i) { r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r); } i = 5 r = 6

12 David Luebke 12 5/22/2015 OS-Select Example ● Example: show OS-Select(root, 5): M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 OS-Select(x, i) { r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r); } i = 5 r = 6 i = 5 r = 2

13 David Luebke 13 5/22/2015 OS-Select Example ● Example: show OS-Select(root, 5): M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 OS-Select(x, i) { r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r); } i = 5 r = 6 i = 5 r = 2 i = 3 r = 2

14 David Luebke 14 5/22/2015 OS-Select Example ● Example: show OS-Select(root, 5): M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 OS-Select(x, i) { r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r); } i = 5 r = 6 i = 5 r = 2 i = 3 r = 2 i = 1 r = 1

15 David Luebke 15 5/22/2015 OS-Select: A Subtlety OS-Select(x, i) { r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r); } ● What happens at the leaves? ● How can we deal elegantly with this? Oops…

16 David Luebke 16 5/22/2015 OS-Select OS-Select(x, i) { r = x->left->size + 1; if (i == r) return x; else if (i < r) return OS-Select(x->left, i); else return OS-Select(x->right, i-r); } ● What will be the running time?

17 David Luebke 17 5/22/2015 Determining The Rank Of An Element M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 What is the rank of this element?

18 David Luebke 18 5/22/2015 Determining The Rank Of An Element M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 Of this one? Why?

19 David Luebke 19 5/22/2015 Determining The Rank Of An Element M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 Of the root? What’s the pattern here?

20 David Luebke 20 5/22/2015 Determining The Rank Of An Element M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 What about the rank of this element?

21 David Luebke 21 5/22/2015 Determining The Rank Of An Element M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 This one? What’s the pattern here?

22 David Luebke 22 5/22/2015 OS-Rank OS-Rank(T, x) { r = x->left->size + 1; y = x; while (y != T->root) if (y == y->p->right) r = r + y->p->left->size + 1; y = y->p; return r; } ● What will be the running time?

23 David Luebke 23 5/22/2015 OS-Trees: Maintaining Sizes ● So we’ve shown that with subtree sizes, order statistic operations can be done in O(lg n) time ● Next step: maintain sizes during Insert() and Delete() operations ■ How would we adjust the size fields during insertion on a plain binary search tree?

24 David Luebke 24 5/22/2015 OS-Trees: Maintaining Sizes ● So we’ve shown that with subtree sizes, order statistic operations can be done in O(lg n) time ● Next step: maintain sizes during Insert() and Delete() operations ■ How would we adjust the size fields during insertion on a plain binary search tree? ■ A: increment sizes of nodes traversed during search

25 David Luebke 25 5/22/2015 OS-Trees: Maintaining Sizes ● So we’ve shown that with subtree sizes, order statistic operations can be done in O(lg n) time ● Next step: maintain sizes during Insert() and Delete() operations ■ How would we adjust the size fields during insertion on a plain binary search tree? ■ A: increment sizes of nodes traversed during search ■ Why won’t this work on red-black trees?

26 David Luebke 26 5/22/2015 Maintaining Size Through Rotation ● Salient point: rotation invalidates only x and y ● Can recalculate their sizes in constant time ■ Why? y 19 x 11 x 19 y 12 rightRotate(y) leftRotate(x) 64 76 47

27 David Luebke 27 5/22/2015 Augmenting Data Structures: Methodology ● Choose underlying data structure ■ E.g., red-black trees ● Determine additional information to maintain ■ E.g., subtree sizes ● Verify that information can be maintained for operations that modify the structure ■ E.g., Insert(), Delete() (don’t forget rotations!) ● Develop new operations ■ E.g., OS-Rank(), OS-Select()


Download ppt "David Luebke 1 5/22/2015 ITCS 6114 Universal Hashing Dynamic Order Statistics."

Similar presentations


Ads by Google