Presentation is loading. Please wait.

Presentation is loading. Please wait.

N Line Segments Problem Given a set of n line segments in the plane, determine whether or not any intersection exists. It is not required to output all.

Similar presentations


Presentation on theme: "N Line Segments Problem Given a set of n line segments in the plane, determine whether or not any intersection exists. It is not required to output all."— Presentation transcript:

1 n Line Segments Problem Given a set of n line segments in the plane, determine whether or not any intersection exists. It is not required to output all the intersections.

2 A Brute-Force Algorithm Simply take each pair of segments, and check if they intersect. If so, output yes to the original problem; otherwise, output no. Running time  (n ). 2 Most segments do not intersect, or if they do, only with a few other segments. Need an faster algorithm for testing in such situations! Nevertheless, sparse distribution in practice:

3 The Sweeping Algorithm Avoid testing pairs of segments that are far apart. Idea: imagine a vertical sweep line passes through the given set of line segments, from left to right. Sweep line

4 Assumptions on Non-degeneracy 1. No segment is vertical. // the sweep line always hits a segment at // a point. If ≥ 1 vertical segment, rotate all segments by a small angle and then test for intersection. 2. No three segments are concurrent. Exercise 33.2-8 on how to deal with the violating case. Dealing with degeneracies are often the most laborious part of implementing geometric algorithms and proving their correctness.

5 Ordering Segments A total order over the segments that intersect the current position of the sweep line: a b c d e b > c > d (a and e are out of the ordering) Such a total order T is called the sweep-line status.

6 Sweep-line Status Describes the relationships among the segments intersected by the sweep line. Supports the following operations on a segment s. Insert(T, s) Delete(T, s) Above(T, s) // segment immediately above s Below(T, s) // segment immediately below s Red-black tree implementation (key comparisons replaced by cross-product comparisons). O(lg n) for each operation.

7 Sweeping around an Intersection The order of the two intersecting segments gets reversed. a b c d e f before after d < f < c < bd < c < f < b

8 Event-point Schedule The sweeping algorithm does something only when it reaches an endpoint. Sort the segment endpoints by increasing x-coordinate and proceed from left to right. A sequence of x-coordinates, in increasing order. Where will the sweep line make intermediate stops?

9 An Example a b c d e f a Intersect! abab acbacb dacbdacb dcbdcb edcbedcb edbedb Sweep-line status:

10 The Sweeping Algorithm Any-Segments-Intersect(S) // a set S of line segments T = { } // total order of segments intersecting the sweep line sort the endpoints of the segments from left to right for each point p in the sorted list do if p is the left endpoint of a segment s then Insert(T, s) if (Above(T, s) exists and intersects s) or (Below(T, s) exists and intersects s) then return true if p is the right endpoint of a segment s then if both Above(T, s) and Below(T, s) exist and they intersect then return true Delete (T, s) return false Above(T, s) Below(T, s) s p Above(T, s) Below(T, s) p s

11 Running Time Total time O(n lg n). Sorting of line segments takes O(n lg n) time. There are 2n event points, each costs O(lg n) on updating the sweep-line status.


Download ppt "N Line Segments Problem Given a set of n line segments in the plane, determine whether or not any intersection exists. It is not required to output all."

Similar presentations


Ads by Google