資料結構與演算法 ( 下 )

18
2010/4/2 segment intersection 1 資資資資資資資資 ( 資 ) 呂呂(Hsueh-I Lu) http://www.csie.ntu.edu.tw/~hi l/

Upload: nina-nicholson

Post on 01-Jan-2016

33 views

Category:

Documents


1 download

DESCRIPTION

資料結構與演算法 ( 下 ). 呂學一 (Hsueh-I Lu) http://www.csie.ntu.edu.tw/~hil/. Today. Segment intersection. The problem. Input: n line segments in the plane Each segment is specified by the coordinates of its endpoints. Output: Determining whether or not there are two input segments intersected. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 資料結構與演算法 ( 下 )

segment intersection 12010/4/2

資料結構與演算法 (下 )

呂學一 (Hsueh-I Lu)

http://www.csie.ntu.edu.tw/~hil/

Page 2: 資料結構與演算法 ( 下 )

segment intersection 22010/4/2

Today

Segment intersection

Page 3: 資料結構與演算法 ( 下 )

segment intersection 32010/4/2

The problem

Input: – n line segments in the plane

Each segment is specified by the coordinates of its endpoints.

Output:– Determining whether or not there are two

input segments intersected

Page 4: 資料結構與演算法 ( 下 )

segment intersection 42010/4/2

Illustration

Yes No

Page 5: 資料結構與演算法 ( 下 )

segment intersection 52010/4/2

Naïve algorithm

O(n2) time– For each of the O(n2) pairs of input segments,

determine in O(1) time whether they are intersected or not.

Page 6: 資料結構與演算法 ( 下 )

segment intersection 62010/4/2

A clever algorithm

Initialization: – Sorting the 2n endpoints according to their x

coordinates– O(n log n) time

Key step:– Process each of the 2n endpoints according to

the above sorted order– Each step takes O(log n) time.

Page 7: 資料結構與演算法 ( 下 )

segment intersection 72010/4/2

Key idea – Sweep line

a

b

c

de

f

Page 8: 資料結構與演算法 ( 下 )

segment intersection 82010/4/2

Segment list L

The segments are “sorted” according to their vertical order at the sweep line

Page 9: 資料結構與演算法 ( 下 )

segment intersection 92010/4/2

Segment list L

a

b

c

de

f

a ab

acb

dacb

dcb

edcb

edb

Page 10: 資料結構與演算法 ( 下 )

segment intersection 102010/4/2

Key observation

Suppose that segments a and b have the leftmost intersection point. Then, they have to be next to each other in the segment list L at some point during the line-sweeping process.

Page 11: 資料結構與演算法 ( 下 )

segment intersection 112010/4/2

Segment list L

a

b

c

de

f

a ab

acb

dacb

dcb

edcb

edb

Page 12: 資料結構與演算法 ( 下 )

segment intersection 122010/4/2

Furthermore

Let p be the intersection point of segments a and b. Then, no segments in L change their order before the sweep-line passing point p.

Page 13: 資料結構與演算法 ( 下 )

segment intersection 132010/4/2

Segment list L

a

b

c

de

f

a ab

acb

dacb

dcb

edcb

edb

Page 14: 資料結構與演算法 ( 下 )

segment intersection 142010/4/2

Maintaining L

The segments are “sorted” according to their vertical order at the sweep line:– When reaching a starting endpoint,

we insert the segment into the list L according to the vertical order at the sweep line.

– When reaching an ending endpoint, we delete the segment from the list L according to

the vertical order at the sweep line.

Page 15: 資料結構與演算法 ( 下 )

segment intersection 152010/4/2

Detecting intersection

We only have to detect intersection for consecutive segments in L.– When inserting a new segment in L, we check

this new segment with its neighbors in L.– When deleting a segment from L, we check

for the segments that become neighbors in L due to the deletion.

Page 16: 資料結構與演算法 ( 下 )

segment intersection 162010/4/2

To be more careful

The segment list L should be “implemented” by a binary search tree (e.g., a 2-3 tree).

The binary search tree is “sorted” by their relative order of y-coordinates.

It takes O(log n) time to do insertion and deletion.

It takes O(log n) time to identify the “neighbors” of each segment.

Page 17: 資料結構與演算法 ( 下 )

segment intersection 172010/4/2

Segment list L

a

b

c

de

f

a ab

acb

dacb

dcb

edcb

edb

Page 18: 資料結構與演算法 ( 下 )

segment intersection 182010/4/2

O(n log n)-time algorithm Initialization:

– Sorting the 2n endpoints according to their x coordinates

– O(n log n) time Key step:

– Process each of the 2n endpoints from according to above sorted order

– Each step takes O(log n) time