資料結構
This presentation is the property of its rightful owner.
Sponsored Links
1 / 20

資料結構 與 演算法 PowerPoint PPT Presentation


  • 173 Views
  • Uploaded on
  • Presentation posted in: General

資料結構 與 演算法. 台大資工系 呂學一 http://www.csie.ntu.edu.tw /~hil/algo/. Segment intersection. 演算法策略 與 b alanced search tree 的搭配. 「線段交叉 」問題. Input: n line segments in the plane Each segment is specified by the coordinates of its endpoints. Output:

Download Presentation

資料結構 與 演算法

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


2505558

資料結構與演算法

台大資工系

呂學一

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


Segment intersection

Segment intersection

演算法策略

balanced search tree

的搭配


2505558

「線段交叉」問題

  • 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


Illustration

Yes

No

Illustration

segment intersection


Na ve algorithm

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.

segment intersection


A clever algorithm

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.

segment intersection


Key idea sweep line

Key idea – Sweep line

e

d

a

c

f

b

segment intersection


Segment list l

Segment list L

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

segment intersection


Segment list l1

Segment list L

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


Key observation

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.

segment intersection


Segment list l2

Segment list L

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


Furthermore

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.

segment intersection


Segment list l3

Segment list L

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


Maintaining l

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.

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

segment intersection


Detecting intersection

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.

segment intersection


Running time

Running time

  • 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

Number of children = 2, 3, or 4

An example

50

15,33

60,70,79

9,11

19,23,30

35,39

55,58

61,67

73,77

81,83


Segment list l4

Segment list L

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

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


2505558

「線段交叉」的複雜度


  • Login