slide1
Download
Skip this Video
Download Presentation
資料結構 與 演算法

Loading in 2 Seconds...

play fullscreen
1 / 20

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


  • 226 Views
  • Uploaded on

資料結構 與 演算法. 台大資工系 呂學一 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:

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

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
slide1

資料結構與演算法

台大資工系

呂學一

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

segment intersection

Segment intersection

演算法策略

balanced search tree

的搭配

slide3
「線段交叉」問題
  • 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 4An 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

ad