Introduction to Algorithms

Slides:



Advertisements
Similar presentations
October 31, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL13.1 Introduction to Algorithms LECTURE 11 Amortized Analysis Dynamic tables.
Advertisements

Chapter 13. Red-Black Trees
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
October 17, 2005 Copyright© Erik D. Demaine and Charles E. Leiserson L2.1 Introduction to Algorithms 6.046J/18.401J LECTURE9 Randomly built binary.
Introduction to Algorithms 6.046J/18.401J
Introduction to Algorithms
© 2001 by Charles E. Leiserson Introduction to AlgorithmsDay 18 L10.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 10 Prof. Erik Demaine.
© 2001 by Charles E. Leiserson Introduction to AlgorithmsDay 17 L9.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 9 Prof. Charles E. Leiserson.
©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 6 Prof. Erik Demaine.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
Addition Facts
1 Functional Programming Lecture 8 - Binary Search Trees.
David Luebke 1 6/1/2014 CS 332: Algorithms Medians and Order Statistics Structures for Dynamic Sets.
CSE 4101/5101 Prof. Andy Mirzaian Augmenting Data Structures.
CS16: Introduction to Data Structures & Algorithms
Data Structures Using C++
ABC Technology Project
CSE 373 Data Structures and Algorithms
Two Segments Intersect?
David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
Definitions and Bottom-Up Insertion
Introduction to Algorithms Red-Black Trees
Squares and Square Root WALK. Solve each problem REVIEW:
Chapter 5 Test Review Sections 5-1 through 5-4.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Addition 1’s to 20.
25 seconds left…...
Week 1.
10 -1 Chapter 10 Amortized Analysis A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)
We will resume in: 25 Minutes.
Chapter 12 Binary Search Trees
CSE Lecture 17 – Balanced trees
Rizwan Rehman Centre for Computer Studies Dibrugarh University
Foundations of Data Structures Practical Session #7 AVL Trees 2.
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.
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 12.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
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.
Lecture X Augmenting Data Structures
© 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms 6.046J/18.401J LECTURE 10 Balanced Search.
1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 11 Prof. Erik Demaine.
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
2IL05 Data Structures Spring 2010 Lecture 9: Augmenting Data Structures.
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
Analysis of Algorithms CS 477/677
Introduction to Algorithms
Dynamic Order Statistics
Design and Analysis of Algorithms
Red-Black Trees Motivations
Augmenting Data Structures
Lecture 9 Algorithm Analysis
Chapter 14: Augmenting Data Structures
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
CS 583 Analysis of Algorithms
Introduction to Algorithms
CSE2331/5331 Topic 7: Balanced search trees Rotate operation
Augmenting Data Structures: Interval Trees
Analysis of Algorithms CS 477/677
Presentation transcript:

Introduction to Algorithms LECTURE 9 Augmenting Data Structures ‧Dynamic order statistics ‧Methodology ‧Interval trees October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.1

Dynamic order statistics OS-SELECT (i,S) : returns the i th smallest element in the dynamic set S. OS-RANK (x,S) : returns the rank of x ∈S in the sorted order of S’s elements. IDEA: Use a red-black tree for the set S, but keep subtree sizes in the nodes. Key Notation for nodes: size October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.2

Example of an OS-tree October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.3

Selection Implementation trick: Use a sentinel (dummy record) for NIL such that size[NIL] = 0. OS-SELECT (x, i) ⊳ ith smallest element in the subtree rooted at x k←size[left[x]] + 1 ⊳ k = rank(x) if i= k thenreturn x if i< k then return OS-SELECT (left[x], i) else return OS-SELECT (right[x], i –k) (OS-RANKis in the textbook.) October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.4

Example OS-SELECT (root, 5) Running time = O(h) = O(lg n) for red-black trees. October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.5

Data structure maintenance Q. Why not keep the ranks themselves in the nodes instead of subtree sizes? A. They are hard to maintain when the red-black tree is modified. Modifying operations: INSERTand DELETE. Strategy: Update subtree sizes when inserting or deleting. October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.6

Example of insertion INSERT (“K”) October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.7

Handling rebalancing Don’t forget that RB-INSERTand RB-DELETEmay also need to modify the red-black tree in order to maintain balance. •Recolorings: no effect on subtree sizes. •Rotations: fix up subtree sizes in O(1) time. Example: ∴RB-INSERT and RB-DELETE still run in O(lg n) time. October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.8

Data-structure augmentation Methodology: (e.g.,order-statistics trees) 1. Choose an underlying data structure (red- black trees). 2. Determine additional information to be stored in the data structure (subtree sizes). 3. Verify that this information can be maintained for modifying operations (RB- INSERT, RB-DELETE—don’t forget rotations). 4. Develop new dynamic-set operations that use the information (OS-SELECTand OS-RANK). These steps are guidelines, not rigid rules. October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.9

Interval trees Goal: To maintain a dynamic set of intervals, such as time intervals. Query: For a given query interval i, find an interval in the set that overlaps i. October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.10

Following the methodology 1. Choose an underlying data structure. • Red-black tree keyed on low (left) endpoint. 2. Determine additional information to be stored in the data structure. • Store in each node x the largest value m[x] in the subtree rooted at x, as well as the interval int[x] corresponding to the key. int m October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.11

Example interval tree October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.12

Modifying operations 3. Verify that this information can be maintained for modifying operations. • INSERT: Fix m’s on the way down. • Rotations —Fixup = O(1) time per rotation: Total INSERT time = O(lg n) ; DELETE similar. October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.13

New operations 4. Develop new dynamic-set operations that use the information. INTERVAL-SEARCH (i) x←root while x≠NIL and (low[i] > high[int[x]] or low[int[x]] > high[i]) do ⊳ i and int[x] don’t overlap if left[x] ≠NIL and low [i] ≤m[left[x]] then x←left[x] else x←right[x] return x October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.14

Example 1: INTERVAL-SEARCH([14,16]) x←root [14,16] and [17,19] don’t overlap 14 ≤18 ⇒ x←left[x] October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.15

Example 1: INTERVAL-SEARCH([14,16]) [14,16] and [5,11] don’t overlap 14 >8 ⇒ x←right[x] October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.16

Example 1: INTERVAL-SEARCH([14,16]) [14,16] and [15,18] overlap return [15,18] October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.17

Example 2: INTERVAL-SEARCH ([12,14]) x←root [12,14] and [17,19] don’t overlap 12 ≤18 ⇒ x←left[x] October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.18

Example 2: INTERVAL-SEARCH ([12,14]) [12,14] and [5,11] don’t overlap 12 >8 ⇒ x←right[x] October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.19

Example 2: INTERVAL-SEARCH ([12,14]) [12,14] and [15,18] don’t overlap 12 >10 ⇒ x←right[x] October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.20

Example 2: INTERVAL-SEARCH ([12,14]) x = NIL⇒no interval that overlaps [12,14] exists October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.21

Analysis Time = O(h) = O(lg n) , since INTERVAL-SEARC Hdoes constant work at each level as it follows a simple path down the tree. List all overlapping intervals: • Search, list, delete, repeat. • Insert them all again at the end. Time = O(klg n) , where k is the total number of overlapping intervals. This is an output-sensitive bound. Best algorithm to date: O(k+ lg n). October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.22

Correctness Theorem. Let L be the set of intervals in the left subtree of node x, and let R be the set of intervals in x’s right subtree. • If the search goes right, then { i′∈L: i′ overlaps i} = ∅. • If the search goes left, then {i′∈L: i′ overlaps i} = ∅ ⇒ {i′∈R: i′ overlaps i} = ∅. In other words, it’s always safe to take only 1 of the 2 children: we’ll either find something, or nothing was to be found. October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.23

Correctness proof Proof. Suppose first that the search goes right. • If left[x] = NIL, then we’re done, since L = ∅. • Otherwise, the code dictates that we must have low[i] > m[left[x]]. The value m[left[x]] corresponds to the high endpoint of some interval j∈L, and no other interval in L can have a larger high endpoint than high[ j]. • Therefore, {i′∈L: i′ overlaps i} = ∅. October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.24

Proof (continued) Suppose that the search goes left, and assume that {i′∈L: i′ overlaps i} = ∅. • Then, the code dictates that low[i] ≤m[left[x]] = high[j] for some j∈L. • Since j∈L, it does not overlap i, and hence high[i] < low[j]. • But, the binary-search-tree property implies that for all i′∈R, we have low[j] ≤low[i′]. • But then {i′∈R: i′ overlaps i} = ∅. October 24, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.25