Lecture 7-cs345-2014

Preview:

DESCRIPTION

 

Citation preview

Design and Analysis of Algorithms(CS345/CS345A)

Lecture 7 Augmented BST (Final lecture)

• Orthogonal range searching (more details)• Interval tree: an augmented BST used for geometric problems

1

For the sake of continuity, the slides on orthogonal range searching is appended to the slides of the previous lecture.

2

INTERVAL TREE

An augmented BST used for many geometric problems

3

Motivation through an application

Given a set of axis-parallel rectangles, determine if any two of them intersect.

4

Interval

An interval [, ] = {| }.For an interval = [, ], high[] = low[] = Given any two intervals [, ] and [, ], they either overlap or don’t overlap.

It takes O(1) time to check if two intervals overlap.

5

𝒂 𝒃𝒄 𝒅 𝒄 𝒅𝒄 𝒅𝒄 𝒅

Problem Definition

Maintain a data structure for a set of intervals so that the following operations can be performed efficiently.Overlap(,): determine if overlaps any interval from . Insert(,): Insert interval into .Delete(,): Delete from .

6

1 34 20

7 11

10 19 23 27

24 30

How to build a BST on intervals ?

7

1 34 20

7 11

10 19 23 27

24 30

1 34 20

7 11

10 19 23 27

24 30

How to build a BST on intervals ?

How to put a total order on intervals ?

By considering low[] for each interval.

8

How to build a BST on intervals ?

9

1 3 10 19 23 27

24 307 11

4 20

How to augment the BST ?

Main objective: How to perform Overlap(,) efficiently ?“determine if a query interval overlaps any interval in tree storing intervals .”

10

?

? ?

?

?

?

How to perform Overlap(,) efficiently ?

11

𝒃𝒂𝒄 𝒅𝒄 𝒅 𝒄 𝒅

𝑳 𝑹

How to perform Overlap(,) efficiently ?

Question: How to surely determine whether a query interval overlaps any interval in ?Answer: by knowing the maximum high of any interval in ?

12

𝒃𝒂𝒄 𝒅

𝑳 𝑹

𝒄 𝒅

Augmentation of BST

For each node , keep a field Max-high():Max-high() = max{ high() : T()}

= max( ? , ? , ? )

13

1 3

10 19

23 27

24 307 11

4 2020

3 11

30

30

30

Max-high(left()) high() Max-high(right())

This field is efficient to maintain during insertion/deletion

Overlap( )

Overlap(T,){ found=false; T ; While( ?? ) if(overlaps Interval(u))

; else { If( ? and ? ) u ; else u ; } } If(found) print “Interval(u) overlaps ”; else print “No interval overlaps ”.}

14

<> NULL Max-high(left(u)) Low()

u<>NULL and not found {

𝒃𝒂

𝒄 𝒅𝒄 𝒅 𝒄 𝒅

ApplicationGiven a set of axis-parallel rectangles, determine if any two of them intersect.Spend some time to realize the difficulty of the problem.

15Line sweep method

Algorithm

Each rectangle has two intervals:red-interval: upper sideblue-interval: lower side

Total rectangles total intervals.

empty treeProcess the intervals in increasing order of y-coordinates (virtual line sweep):

For each blue-interval: query if it intersects any interval in Insert the interval if query fails

For each red-interval : remove the corresponding blue interval

This is an O(log ) time algorithm.Homework: Write a neat pseudo-code of this algorithm.

16

Application 2

Given a set of axis-parallel rectangles, compute their total area.

It requires some slight modifications to Interval tree.

17

just for a nice trial . Not important from point of view of exams.