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.
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.
David Luebke 1 5/22/2015 ITCS 6114 Universal Hashing Dynamic Order Statistics.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
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.
CSC 2300 Data Structures & Algorithms February 27, 2007 Chapter 5. Hashing.
Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm.
Lecture 10: Search Structures and Hashing
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.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
IS 2610: Data Structures Searching March 29, 2004.
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.
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.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 11 Prof. Erik Demaine.
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 3358 NOTE SET 13 Data Structures and Algorithms.
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:
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.
Many slides here are based on E. Demaine , D. Luebke slides
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Analysis of Algorithms CS 477/677
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Introduction to Algorithms
Hash table CSC317 We have elements with key and satellite data
Lecture No.43 Data Structures Dr. Sohail Aslam.
CS 332: Algorithms Hash Tables David Luebke /19/2018.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Introduction to Algorithms 6.046J/18.401J
Summary of General Binary search tree
B-Trees © Dave Bockus Acknowledgements to:
Lecture 25 Splay Tree Chapter 10 of textbook
Chapter 20: Binary Trees.
Augmenting Data Structures
Chapter 21: Binary Trees.
Randomized Algorithms CS648
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Chapter 14: Augmenting Data Structures
Hash Tables – 2 Comp 122, Spring 2004.
CS 583 Analysis of Algorithms
Advanced Implementation of Tables
Introduction to Algorithms
Advanced Implementation of Tables
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Topic 6: Binary Search Tree Data structure Operations
CS 5243: Algorithms Hash Tables.
CSE2331/5331 Topic 7: Balanced search trees Rotate operation
Chapter 20: Binary Trees.
Augmenting Data Structures: Interval Trees
CS 3343: Analysis of Algorithms
CS 6310 Advanced Data Structure Wei-Shian Wang
Hash Tables: Associative Containers with Constant Time Operations --- On Average Consider the problem of computing the frequency of words.
Chapter 12&13: Binary Search Trees (BSTs)
Hash Tables – 2 1.
Presentation transcript:

Dynamic Order Statistics CS 332: Algorithms Dynamic Order Statistics David Luebke 1 8/26/2018

Review: 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 2 8/26/2018

Review: 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 3 8/26/2018

Review: 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 4 8/26/2018

Review: A Universal Hash Function Choose table size m to be prime Decompose key x into r+1 bytes, so that x = {x0, x1, …, xr} Only requirement is that max value of byte < m Let a = {a0, a1, …, ar} denote a sequence of r+1 elements chosen randomly from {0, 1, …, m - 1} Define corresponding hash function ha  : With this definition,  has mr+1 members David Luebke 5 8/26/2018

Review: 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 6 8/26/2018

Review: 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: M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 David Luebke 7 8/26/2018

How can we use this property to select the ith element of the set? Selection On OS Trees M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 How can we use this property to select the ith element of the set? David Luebke 8 8/26/2018

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 9 8/26/2018

OS-Select Example Example: show OS-Select(root, 5): 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); } M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 David Luebke 10 8/26/2018

OS-Select Example Example: show OS-Select(root, 5): 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); } M 8 i = 5 r = 6 C 5 P 2 A 1 F 3 Q 1 D 1 H 1 David Luebke 11 8/26/2018

OS-Select Example Example: show OS-Select(root, 5): 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); } M 8 i = 5 r = 6 C 5 i = 5 r = 2 P 2 A 1 F 3 Q 1 D 1 H 1 David Luebke 12 8/26/2018

OS-Select Example Example: show OS-Select(root, 5): 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); } M 8 i = 5 r = 6 C 5 i = 5 r = 2 P 2 A 1 F 3 i = 3 r = 2 Q 1 D 1 H 1 David Luebke 13 8/26/2018

OS-Select Example Example: show OS-Select(root, 5): 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); } M 8 C 5 P 2 Q 1 A 1 F 3 D 1 H 1 i = 5 r = 6 i = 5 r = 2 i = 3 r = 2 i = 1 r = 1 David Luebke 14 8/26/2018

OS-Select: A Subtlety What happens at the leaves? 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 15 8/26/2018

OS-Select What will be the running time? 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 16 8/26/2018

Determining The Rank Of An Element C 5 P 2 Q 1 A 1 F 3 D 1 H 1 What is the rank of this element? David Luebke 17 8/26/2018

Determining The Rank Of An Element C 5 P 2 Q 1 A 1 F 3 D 1 H 1 Of this one? Why? David Luebke 18 8/26/2018

Determining The Rank Of An Element C 5 P 2 Q 1 A 1 F 3 D 1 H 1 Of the root? What’s the pattern here? David Luebke 19 8/26/2018

Determining The Rank Of An Element C 5 P 2 Q 1 A 1 F 3 D 1 H 1 What about the rank of this element? David Luebke 20 8/26/2018

Determining The Rank Of An Element C 5 P 2 Q 1 A 1 F 3 D 1 H 1 This one? What’s the pattern here? David Luebke 21 8/26/2018

OS-Rank What will be the running time? 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 22 8/26/2018

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 23 8/26/2018

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 24 8/26/2018

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 25 8/26/2018

Maintaining Size Through Rotation y 19 x 19 rightRotate(y) x 11 y 12 7 6 leftRotate(x) 6 4 4 7 Salient point: rotation invalidates only x and y Can recalculate their sizes in constant time Why? David Luebke 26 8/26/2018

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() David Luebke 27 8/26/2018