資料結構與演算法 ( 下 )
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 PresentationTRANSCRIPT
segment intersection 12010/4/2
資料結構與演算法 (下 )
呂學一 (Hsueh-I Lu)
http://www.csie.ntu.edu.tw/~hil/
segment intersection 22010/4/2
Today
Segment intersection
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
segment intersection 42010/4/2
Illustration
Yes No
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.
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.
segment intersection 72010/4/2
Key idea – Sweep line
a
b
c
de
f
segment intersection 82010/4/2
Segment list L
The segments are “sorted” according to their vertical order at the sweep line
segment intersection 92010/4/2
Segment list L
a
b
c
de
f
a ab
acb
dacb
dcb
edcb
edb
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.
segment intersection 112010/4/2
Segment list L
a
b
c
de
f
a ab
acb
dacb
dcb
edcb
edb
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.
segment intersection 132010/4/2
Segment list L
a
b
c
de
f
a ab
acb
dacb
dcb
edcb
edb
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.
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.
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.
segment intersection 172010/4/2
Segment list L
a
b
c
de
f
a ab
acb
dacb
dcb
edcb
edb
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