1 / 20

# 資料結構 與 演算法 - PowerPoint PPT Presentation

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' 資料結構 與 演算法' - norris

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### 資料結構與演算法

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

### Segment intersection

balanced search tree

「線段交叉」問題

• 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

segment intersection

No

Illustration

segment intersection

• O(n2) time

• For each of the O(n2) pairs of input segments, determine in O(1) time whether they are intersected or not.

segment intersection

• 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.

segment intersection

e

d

a

c

f

b

segment intersection

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

segment intersection

e

d

a

c

f

b

a

a

b

a

c

b

d

a

c

b

d

c

b

e

d

b

e

d

c

b

segment intersection

• 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.

segment intersection

e

d

a

c

f

b

a

a

b

a

c

b

d

a

c

b

d

c

b

e

d

b

e

d

c

b

segment intersection

• 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.

segment intersection

e

d

a

c

f

b

a

a

b

a

c

b

d

a

c

b

d

c

b

e

d

b

e

d

c

b

segment intersection

• 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.

• 我們可以用balanced search tree來implement L。

segment 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.

segment intersection

• The segment list Lis implemented by a binary search tree (e.g., a 2-3-4 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. (How?)

segment intersection

An example

50

15,33

60,70,79

9,11

19,23,30

35,39

55,58

61,67

73,77

81,83

e

d

a

c

f

b

a

a

b

a

c

b

d

a

c

b

d

c

b

e

d

b

e

d

c

b

segment intersection

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

segment intersection

「線段交叉」的複雜度