David Luebke 1 5/22/2015 CS 332: Algorithms Augmenting Data Structures: Interval Trees.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

CSE 4101/5101 Prof. Andy Mirzaian Augmenting Data Structures.
Chapter 12 Binary Search Trees
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Trees Types and Operations
Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006.
Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 473Lecture X1 CS473-Algorithms I Lecture X Augmenting Data Structures.
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.
David Luebke 1 5/22/2015 ITCS 6114 Universal Hashing Dynamic Order Statistics.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 12.
Binary Search Trees CIS 606 Spring Search trees Data structures that support many dynamic-set operations. – Can be used as both a dictionary and.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
10/22/2002CSE Red Black Trees CSE Algorithms Red-Black Trees Augmenting Search Trees Interval Trees.
Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part II Prof. Dr. Th. Ottmann Summer Semester 2006.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
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.
David Luebke 1 8/7/2015 CS 332: Algorithms Graph Algorithms.
David Luebke 1 8/9/2015 CS 332: Algorithms Review for Final.
Red-Black Trees Lecture 10 Nawazish Naveed. Red-Black Trees (Intro) BSTs perform dynamic set operations such as SEARCH, INSERT, DELETE etc in O(h) time.
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
Interval Trees.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Lecture X Augmenting Data Structures
Interval Trees CS302 Data Structures Modified from Dr Monica Nicolescu.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms 6.046J/18.401J LECTURE 10 Balanced Search.
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.
David Luebke 1 10/25/2015 CS 332: Algorithms Review For Midterm.
1 Data Structures and Algorithms Searching Red-Black and Other Dynamically BalancedTrees.
2IL50 Data Structures Fall 2015 Lecture 9: Range Searching.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 11 Prof. Erik Demaine.
October 9, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Aalborg University
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
Red-Black Tree Insertion Start with binary search insertion, coloring the new node red NIL l Insert 18 NIL l NIL l 1315 NIL l
Augmenting AVL trees. How we’ve thought about trees so far Good for determining ancestry Can be good for quickly finding an element Good for determining.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
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.
CSC317 1 Binary Search Trees (binary because branches either to left or to right) Operations: search min max predecessor successor. Costs? Time O(h) with.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
64 Algorithms analysis and design BY Lecturer: Aisha Dawood.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
Analysis of Algorithms CS 477/677
Introduction to Algorithms
Augmenting AVL trees.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Dynamic Order Statistics
Summary of General Binary search tree
CS200: Algorithms Analysis
Augmenting Data Structures
Chapter 14: Augmenting Data Structures
CS6045: Advanced Algorithms
CS 583 Analysis of Algorithms
Introduction to Algorithms
CSE2331/5331 Topic 7: Balanced search trees Rotate operation
Augmenting Data Structures: Interval Trees
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Algorithms CSCI 235, Spring 2019 Lecture 21 Binary Search Trees
Presentation transcript:

David Luebke 1 5/22/2015 CS 332: Algorithms Augmenting Data Structures: Interval Trees

David Luebke 2 5/22/2015 Review: Dynamic Order Statistics ● We’ve seen algorithms for finding the ith element of an unordered set in O(n) time ● OS-Trees: a structure to support finding the ith element of a dynamic set in O(lg n) time ■ Support standard dynamic set operations ( Insert(), Delete(), Min(), Max(), Succ(), Pred() ) ■ Also support these order statistic operations: void OS-Select(root, i); int OS-Rank(x);

David Luebke 3 5/22/2015 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: M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1

David Luebke 4 5/22/2015 Review: OS-Select ● 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 5 5/22/2015 Review: OS-Select ● 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 6 5/22/2015 Review: OS-Select ● 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 7 5/22/2015 Review: OS-Select ● 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 8 5/22/2015 Review: OS-Select ● 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 9 5/22/2015 Review: OS-Select ● Example: show OS-Select(root, 5): Note: use a sentinel NIL element at the leaves with size = 0 to simplify code, avoid testing for NULL 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 10 5/22/2015 Review: Determining The Rank Of An Element M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 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; } Idea: rank of right child x is one more than its parent’s rank, plus the size of x’s left subtree

David Luebke 11 5/22/2015 Review: Determining The Rank Of An Element M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 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; } Example 1: find rank of element with key H y r = 1

David Luebke 12 5/22/2015 Review: Determining The Rank Of An Element M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 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; } Example 1: find rank of element with key H r = 1 y r = = 3

David Luebke 13 5/22/2015 Review: Determining The Rank Of An Element M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 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; } Example 1: find rank of element with key H r = 1 r = 3 y r = = 5

David Luebke 14 5/22/2015 Review: Determining The Rank Of An Element M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 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; } Example 1: find rank of element with key H r = 1 r = 3 r = 5 y r = 5

David Luebke 15 5/22/2015 Review: Determining The Rank Of An Element M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 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; } Example 2: find rank of element with key P y r = 1

David Luebke 16 5/22/2015 Review: Determining The Rank Of An Element M8M8 C5C5 P2P2 Q1Q1 A1A1 F3F3 D1D1 H1H1 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; } Example 2: find rank of element with key P r = 1 y r = = 7

David Luebke 17 5/22/2015 Review: Maintaining Subtree Sizes ● So by keeping subtree sizes, order statistic operations can be done in O(lg n) time ● Next: maintain sizes during Insert() and Delete() operations ■ Insert(): Increment size fields of nodes traversed during search down the tree ■ Delete(): Decrement sizes along a path from the deleted node to the root ■ Both: Update sizes correctly during rotations

David Luebke 18 5/22/2015 Reivew: Maintaining Subtree Sizes ● Note that rotation invalidates only x and y ● Can recalculate their sizes in constant time ● Thm 15.1: can compute any property in O(lg n) time that depends only on node, left child, and right child y 19 x 11 x 19 y 12 rightRotate(y) leftRotate(x)

David Luebke 19 5/22/2015 Review: Methodology For Augmenting Data Structures ● Choose underlying data structure ● Determine additional information to maintain ● Verify that information can be maintained for operations that modify the structure ● Develop new operations

David Luebke 20 5/22/2015 Interval Trees ● The problem: maintain a set of intervals ■ E.g., time intervals for a scheduling program: i = [7,10]; i  low = 7; i  high = 10

David Luebke 21 5/22/2015 Interval Trees ● The problem: maintain a set of intervals ■ E.g., time intervals for a scheduling program: ■ Query: find an interval in the set that overlaps a given query interval ○ [14,16]  [15,18] ○ [16,19]  [15,18] or [17,19] ○ [12,14]  NULL i = [7,10]; i  low = 7; i  high = 10

David Luebke 22 5/22/2015 Interval Trees ● Following the methodology: ■ Pick underlying data structure ■ Decide what additional information to store ■ Figure out how to maintain the information ■ Develop the desired new operations

David Luebke 23 5/22/2015 Interval Trees ● Following the methodology: ■ Pick underlying data structure ○ Red-black trees will store intervals, keyed on i  low ■ Decide what additional information to store ■ Figure out how to maintain the information ■ Develop the desired new operations

David Luebke 24 5/22/2015 Interval Trees ● Following the methodology: ■ Pick underlying data structure ○ Red-black trees will store intervals, keyed on i  low ■ Decide what additional information to store ○ We will store max, the maximum endpoint in the subtree rooted at i ■ Figure out how to maintain the information ■ Develop the desired new operations

David Luebke 25 5/22/2015 Interval Trees [17,19] [5,11][21,23] [4,8][15,18] [7,10] int max What are the max fields?

David Luebke 26 5/22/2015 Interval Trees [17,19] 23 [5,11] 18 [21,23] 23 [4,8] 8 [15,18] 18 [7,10] 10 int max Note that:

David Luebke 27 5/22/2015 Interval Trees ● Following the methodology: ■ Pick underlying data structure ○ Red-black trees will store intervals, keyed on i  low ■ Decide what additional information to store ○ Store the maximum endpoint in the subtree rooted at i ■ Figure out how to maintain the information ○ How would we maintain max field for a BST? ○ What’s different? ■ Develop the desired new operations

David Luebke 28 5/22/2015 Interval Trees ● What are the new max values for the subtrees? [11,35] 35 [6,20] 20 [6,20] ??? [11,35] ??? rightRotate(y) leftRotate(x) … 14 … 19 … 30 … ???

David Luebke 29 5/22/2015 Interval Trees ● What are the new max values for the subtrees? ● A: Unchanged ● What are the new max values for x and y? [11,35] 35 [6,20] 20 [6,20] ??? [11,35] ??? rightRotate(y) leftRotate(x) … 14 … 19 … 30 … 14 … 19 … 30

David Luebke 30 5/22/2015 Interval Trees ● What are the new max values for the subtrees? ● A: Unchanged ● What are the new max values for x and y? ● A: root value unchanged, recompute other [11,35] 35 [6,20] 20 [6,20] 35 [11,35] 35 rightRotate(y) leftRotate(x) … 14 … 19 … 30 … 14 … 19 … 30

David Luebke 31 5/22/2015 Interval Trees ● Following the methodology: ■ Pick underlying data structure ○ Red-black trees will store intervals, keyed on i  low ■ Decide what additional information to store ○ Store the maximum endpoint in the subtree rooted at i ■ Figure out how to maintain the information ○ Insert: update max on way down, during rotations ○ Delete: similar ■ Develop the desired new operations

David Luebke 32 5/22/2015 Searching Interval Trees IntervalSearch(T, i) { x = T->root; while (x != NULL && !overlap(i, x->interval)) if (x->left != NULL && x->left->max  i->low) x = x->left; else x = x->right; return x } ● What will be the running time?

David Luebke 33 5/22/2015 IntervalSearch() Example ● Example: search for interval overlapping [14,16] [17,19] 23 [5,11] 18 [21,23] 23 [4,8] 8 [15,18] 18 [7,10] 10 IntervalSearch(T, i) { x = T->root; while (x != NULL && !overlap(i, x->interval)) if (x->left != NULL && x->left->max  i->low) x = x->left; else x = x->right; return x }

David Luebke 34 5/22/2015 IntervalSearch() Example ● Example: search for interval overlapping [12,14] [17,19] 23 [5,11] 18 [21,23] 23 [4,8] 8 [15,18] 18 [7,10] 10 IntervalSearch(T, i) { x = T->root; while (x != NULL && !overlap(i, x->interval)) if (x->left != NULL && x->left->max  i->low) x = x->left; else x = x->right; return x }

David Luebke 35 5/22/2015 Correctness of IntervalSearch() ● Key idea: need to check only 1 of node’s 2 children ■ Case 1: search goes right ○ Show that  overlap in right subtree, or no overlap at all ■ Case 2: search goes left ○ Show that  overlap in left subtree, or no overlap at all

David Luebke 36 5/22/2015 Correctness of IntervalSearch() ● Case 1: if search goes right,  overlap in the right subtree or no overlap in either subtree ■ If  overlap in right subtree, we’re done ■ Otherwise: ○ x  left = NULL, or x  left  max < x  low (Why?) ○ Thus, no overlap in left subtree! while (x != NULL && !overlap(i, x->interval)) if (x->left != NULL && x->left->max  i->low) x = x->left; else x = x->right; return x;

David Luebke 37 5/22/2015 Correctness of IntervalSearch() ● Case 2: if search goes left,  overlap in the left subtree or no overlap in either subtree ■ If  overlap in left subtree, we’re done ■ Otherwise: ○ i  low  x  left  max, by branch condition ○ x  left  max = y  high for some y in left subtree ○ Since i and y don’t overlap and i  low  y  high, i  high < y  low ○ Since tree is sorted by low’s, i  high < any low in right subtree ○ Thus, no overlap in right subtree while (x != NULL && !overlap(i, x->interval)) if (x->left != NULL && x->left->max  i->low) x = x->left; else x = x->right; return x;