You are on page 1of 25

Introduction to Algorithms

6.046J/18.401J LECTURE 11
Augmenting Data Structures Dynamic order statistics Methodology Interval trees Prof. Charles E. Leiserson
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 Ss elements. IDEA: Use a red-black tree for the set S, but keep subtree sizes in the nodes. Notation for nodes:
October 24, 2005

key key size size


L11.2

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Example of an OS-tree
M M 9 9 C C 5 5 A A 1 1 D D 1 1 F F 3 3 H H 1 1 N N 1 1 P P 3 3 Q Q 1 1

size[x] = size[left[x]] + size[right[x]] + 1


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) i th smallest element in the subtree rooted at x k size[left[x]] + 1 k = rank(x) if i = k then return x if i < k then return OS-SELECT( left[x], i ) else return OS-SELECT( right[x], i k ) (OS-RANK is in the textbook.)
October 24, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.4

Example
OS-SELECT(root, 5)
i=5 k=6 i=5 k=2 A A 1 1 D D 1 1 C C 5 5 F F 3 3 i=3 k=2 H H 1 1 i=1 k=1 N N 1 1 M M 9 9 P P 3 3 Q Q 1 1

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: INSERT and 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)
C C 5 6 5 6 A A 1 1 D D 1 1 F F 3 4 3 4 H H 1 2 1 2 K K 1 1
October 24, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.7

M M 10 9 10 9 P P 3 3 N N 1 1 Q Q 1 1

Handling rebalancing
Dont forget that RB-INSERT and RB-DELETE may 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:
C C 11 11 7
October 24, 2005

E E 16 16 4 3 7

C C 16 16

E E 8 8 3 4
L11.8

RB-INSERT and RB-DELETE still run in O(lg n) time.


Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Data-structure augmentation
Methodology: (e.g., order-statistics trees) 1. Choose an underlying data structure (redblack 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 (RBINSERT, RB-DELETE dont forget rotations). 4. Develop new dynamic-set operations that use the information (OS-SELECT and 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.
i = [7, 10] low[i] = 7 5 4 10 = high[i] 11 17 15 19 18 22

23

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 int m m
October 24, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L11.11

Example interval tree


17,19 17,19 23 23 5,11 5,11 18 18 4,8 4,8 8 8 7,10 7,10 10 10 15,18 15,18 18 18 22,23 22,23 23 23

m[x] = max

high[int[x]] m[left[x]] m[right[x]]


L11.12

October 24, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Modifying operations
3. Verify that this information can be maintained for modifying operations. INSERT: Fix ms on the way down. Rotations Fixup = O(1) time per rotation:
11,15 11,15 30 30 6,20 6,20 30 30 30 30 14 14 19 19 30 30 14 14 6,20 6,20 30 30 11,15 11,15 19 19 19 19

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] dont 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
5,11 5,11 18 18 4,8 4,8 8 8 7,10 7,10 10 10 15,18 15,18 18 18 17,19 17,19 23 23 22,23 22,23 23 23

x root [14,16] and [17,19] dont overlap 14 18 x left[x]


L11.15

October 24, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Example 1: INTERVAL-SEARCH([14,16])
17,19 17,19 23 23 5,11 5,11 18 18 15,18 15,18 18 18 7,10 7,10 10 10 22,23 22,23 23 23

x
4,8 4,8 8 8

[14,16] and [5,11] dont overlap 14 > 8 x right[x]


L11.16

October 24, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Example 1: INTERVAL-SEARCH([14,16])
17,19 17,19 23 23 5,11 5,11 18 18 4,8 4,8 8 8 22,23 22,23 23 23

x
7,10 7,10 10 10

15,18 15,18 18 18

[14,16] and [15,18] overlap return [15,18]


L11.17

October 24, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Example 2: INTERVAL-SEARCH([12,14])
x
5,11 5,11 18 18 4,8 4,8 8 8 7,10 7,10 10 10 15,18 15,18 18 18 17,19 17,19 23 23 22,23 22,23 23 23

x root [12,14] and [17,19] dont overlap 12 18 x left[x]


L11.18

October 24, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Example 2: INTERVAL-SEARCH([12,14])
17,19 17,19 23 23 5,11 5,11 18 18 15,18 15,18 18 18 7,10 7,10 10 10 22,23 22,23 23 23

x
4,8 4,8 8 8

[12,14] and [5,11] dont overlap 12 > 8 x right[x]


L11.19

October 24, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Example 2: INTERVAL-SEARCH([12,14])
17,19 17,19 23 23 5,11 5,11 18 18 4,8 4,8 8 8 22,23 22,23 23 23

x
7,10 7,10 10 10

15,18 15,18 18 18

[12,14] and [15,18] dont overlap 12 > 10 x right[x]


L11.20

October 24, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Example 2: INTERVAL-SEARCH([12,14])
17,19 17,19 23 23 5,11 5,11 18 18 4,8 4,8 8 8 7,10 7,10 10 10 15,18 15,18 18 18 22,23 22,23 23 23

x
x = NIL no interval that overlaps [12,14] exists
L11.21

October 24, 2005

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

Analysis
Time = O(h) = O(lg n), since INTERVAL-SEARCH does 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(k lg 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 xs 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, its always safe to take only 1 of the 2 children: well 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 were 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]. i j L
high[ j] = m[left[x]] low(i)

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 } = .
i
October 24, 2005

j i

Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson

L11.25

You might also like