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

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

CSE 4101/5101 Prof. Andy Mirzaian Augmenting Data Structures.
David Luebke 1 6/7/2014 CS 332: Algorithms Skip Lists Introduction to Hashing.
David Luebke 1 6/7/2014 ITCS 6114 Skip Lists Hashing.
Hash Tables CIS 606 Spring 2010.
Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006.
CS 473Lecture X1 CS473-Algorithms I Lecture X Augmenting Data Structures.
David Luebke 1 5/22/2015 CS 332: Algorithms Augmenting Data Structures: Interval Trees.
Augnenting data structures Augment an existing data structure to apply to a new problem. Red-black tree as an example.
14. Augmenting Data Structures Hsu, Lih-Hsing. Computer Theory Lab. Chapter 13P Dynamic order statistics We shall also see the rank of an element―its.
CSE 2331/5331 Topic 10: Balanced search trees Rotate operation Red-black tree Augmenting data struct.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
2IL50 Data Structures Spring 2015 Lecture 8: Augmenting Data Structures.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 18: Hash Tables.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 12.
1.1 Data Structure and Algorithm Lecture 9 Hashing Topics Reference: Introduction to Algorithm by Cormen Chapter 12: Hash Tables.
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
11.Hash Tables Hsu, Lih-Hsing. Computer Theory Lab. Chapter 11P Directed-address tables Direct addressing is a simple technique that works well.
CSC 2300 Data Structures & Algorithms February 27, 2007 Chapter 5. Hashing.
4a-Searching-More1 More Searching Dan Barrish-Flood.
Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm.
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Lecture 10: Search Structures and Hashing
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
Introduction to Analysis of Algorithms CAS CS 330 Lecture 16 Shang-Hua Teng Thanks to Charles E. Leiserson and Silvio Micali of MIT for these slides.
Binary Search Trees CSE, POSTECH. Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than skip lists and.
David Luebke 1 8/9/2015 CS 332: Algorithms Review for Final.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Lecture X Augmenting Data Structures
Design and Analysis of Algorithms Hash Tables Haidong Xue Summer 2012, at GSU.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms 6.046J/18.401J LECTURE 10 Balanced Search.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
David Luebke 1 10/25/2015 CS 332: Algorithms Review For Midterm.
Data Structures Hash Tables. Hashing Tables l Motivation: symbol tables n A compiler uses a symbol table to relate symbols to associated data u Symbols:
David Luebke 1 10/25/2015 CS 332: Algorithms Skip Lists Hash Tables.
2IL50 Data Structures Fall 2015 Lecture 9: Range Searching.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 11 Prof. Erik Demaine.
Hashing Amihood Amir Bar Ilan University Direct Addressing In old days: LD 1,1 LD 2,2 AD 1,2 ST 1,3 Today: C
David Luebke 1 11/26/2015 Hash Tables. David Luebke 2 11/26/2015 Hash Tables ● Motivation: Dictionaries ■ Set of key/value pairs ■ We care about search,
Midterm Midterm is Wednesday next week ! The quiz contains 5 problems = 50 min + 0 min more –Master Theorem/ Examples –Quicksort/ Mergesort –Binary Heaps.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
CSC 413/513: Intro to Algorithms Hash Tables. ● Hash table: ■ Given a table T and a record x, with key (= symbol) and satellite data, we need to support:
2IL05 Data Structures Spring 2010 Lecture 9: Augmenting Data Structures.
David Luebke 1 3/19/2016 CS 332: Algorithms Augmenting Data Structures.
David Luebke 1 3/20/2016 CS 332: Algorithms Skip Lists.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Analysis of Algorithms CS 477/677
Introduction to Algorithms
Hash table CSC317 We have elements with key and satellite data
CS 332: Algorithms Hash Tables David Luebke /19/2018.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Dynamic Order Statistics
Augmenting Data Structures
Chapter 14: Augmenting Data Structures
Hash Tables – 2 Comp 122, Spring 2004.
CS 583 Analysis of Algorithms
Introduction to Algorithms
CS 5243: Algorithms Hash Tables.
CSE2331/5331 Topic 7: Balanced search trees Rotate operation
Augmenting Data Structures: Interval Trees
CS 3343: Analysis of Algorithms
Hash Tables – 2 1.
Presentation transcript:

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

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

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

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

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

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

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

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?

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); }

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); }

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

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

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

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

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…

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?

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?

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

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?

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?

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?

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?

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?

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

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?

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)

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()