CS6100: Topics in Design and Analysis of Algorithms Line Segment Intersections John Augustine CS6100 (Even 2012): Line Segment Intersections
Line Segment Intersections Scenes and maps can be approximated by sets of lines. Intersections often carry some meaning. Problem. Given a set S of n closed line segments in 2D, report all intersections. O ( n 2 ) algorithm is straightforward. ∃ sets of lines with Ω( n 2 ) intersections. Can we do better? What does it even mean to do better? Answer: Design an output sensitive algorithm! We get to experience the plane sweep technique. CS6100 (Even 2012): Line Segment Intersections 1
Plane Sweep Algorithm Definitions: • A sweep line L “moves” from top to bottom. • The status of L at any time is the L → R sequence of lines intersected by L . • A point at which the status changes is called an event point . – Start and end points of line segments – Also, intersection points. Degeneracies: • No horizontal lines. • Two lines meet at at most 2 points. • No three lines meet at the same point. CS6100 (Even 2012): Line Segment Intersections 2
Plane Sweep Algorithm 1: { Let Q be a priority queue of event points prioritized by y -coordinates. Need to implement Q such that we can test if an event e is already in it. Such a queue can be implemented as a balanced binary search tree } 2: Q is initialized to the set of end points. 3: { Let L be the status of the event line also stored as a balanced binary search tree. } 4: L ← ∅ 5: { Let I be the output set of intersection points. } 6: I ← ∅ . 7: while E is not empty do Extract next event e from Q . 8: if e is a start point of a line ℓ then 9: Add ℓ to L . 10: if current neighbors (if any) of L intersect 11: with ℓ then Add those intersections to Q (if not already 12: added). end if 13: else if e is an end point of a line ℓ then 14: CS6100 (Even 2012): Line Segment Intersections 3
if two neighbors of ℓ in Q intersect each other 15: below e then Add their intersection point to Q (if not 16: already added). end if 17: Remove ℓ from L 18: else if e is an intersection of ℓ 1 and ℓ 2 then 19: I ← I ∪ { e } . 20: Switch order of ℓ 1 and ℓ 2 in L . 21: { Note: ℓ 1 and ℓ 2 may have new neighbors in 22: L . } if new neighbour of ℓ 1 intersects ℓ 1 then 23: Add the intersection to Q . 24: end if { Repeat above if statement for ℓ 2 . } 25: end if 26: 27: end while Exercise . How would you implement a priority queue with facility to check if an element already exists? CS6100 (Even 2012): Line Segment Intersections 4
Sketch of the Analysis Lemma 1. For any intersection point p induced by line segments ℓ 1 and ℓ 2 , there is an event point above it where ℓ 1 and ℓ 2 become adjacent in L and are therefore tested for intersection. Lemma 2. There are at most ( n + k ) event points, where k is the number of intersections. Each event point is handled in O (log n ) time. Theorem 1. There exists an output sensitive algorithm that, given a set of n line segments with k intersection points, computes the intersection points correctly in O (( n + k ) log n ) time. Proof Sketch. Follows from Lemmas 1 and 2. There is one nuance that you must clarify. Each event point could be detected multiple times. Would that mean that Q is updated more than O ( n + k ) times? CS6100 (Even 2012): Line Segment Intersections 5
Recommend
More recommend