+ All Categories
Home > Technology > Lecture 7-cs345-2014

Lecture 7-cs345-2014

Date post: 07-Dec-2014
Category:
Upload: rajiv-omar
View: 265 times
Download: 1 times
Share this document with a friend
Description:
 
Popular Tags:
17
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
Transcript
Page 1: Lecture 7-cs345-2014

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

Page 2: Lecture 7-cs345-2014

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

2

Page 3: Lecture 7-cs345-2014

INTERVAL TREE

An augmented BST used for many geometric problems

3

Page 4: Lecture 7-cs345-2014

Motivation through an application

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

4

Page 5: Lecture 7-cs345-2014

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

𝒂 𝒃𝒄 𝒅 𝒄 𝒅𝒄 𝒅𝒄 𝒅

Page 6: Lecture 7-cs345-2014

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

Page 7: Lecture 7-cs345-2014

How to build a BST on intervals ?

7

1 34 20

7 11

10 19 23 27

24 30

Page 8: Lecture 7-cs345-2014

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

Page 9: Lecture 7-cs345-2014

How to build a BST on intervals ?

9

1 3 10 19 23 27

24 307 11

4 20

Page 10: Lecture 7-cs345-2014

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

?

? ?

?

?

?

Page 11: Lecture 7-cs345-2014

How to perform Overlap(,) efficiently ?

11

𝒃𝒂𝒄 𝒅𝒄 𝒅 𝒄 𝒅

𝑳 𝑹

Page 12: Lecture 7-cs345-2014

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

𝒃𝒂𝒄 𝒅

𝑳 𝑹

𝒄 𝒅

Page 13: Lecture 7-cs345-2014

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

Page 14: Lecture 7-cs345-2014

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 {

𝒃𝒂

𝒄 𝒅𝒄 𝒅 𝒄 𝒅

Page 15: Lecture 7-cs345-2014

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

Page 16: Lecture 7-cs345-2014

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

Page 17: Lecture 7-cs345-2014

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.


Recommended