CS6100: Topics in Design and Analysis of Algorithms Line Segment - - PDF document

cs6100 topics in design and analysis of algorithms
SMART_READER_LITE
LIVE PREVIEW

CS6100: Topics in Design and Analysis of Algorithms Line Segment - - PDF document

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


slide-1
SLIDE 1

CS6100: Topics in Design and Analysis of Algorithms

Line Segment Intersections John Augustine

CS6100 (Even 2012): Line Segment Intersections

slide-2
SLIDE 2

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(n2) algorithm is straightforward. ∃ sets of lines with Ω(n2) 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

slide-3
SLIDE 3

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
  • f 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

slide-4
SLIDE 4

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 8:

Extract next event e from Q.

9:

if e is a start point of a line ℓ then

10:

Add ℓ to L.

11:

if current neighbors (if any) of L intersect with ℓ then

12:

Add those intersections to Q (if not already added).

13:

end if

14:

else if e is an end point of a line ℓ then

CS6100 (Even 2012): Line Segment Intersections 3

slide-5
SLIDE 5

15:

if two neighbors of ℓ in Q intersect each other below e then

16:

Add their intersection point to Q (if not already added).

17:

end if

18:

Remove ℓ from L

19:

else if e is an intersection of ℓ1 and ℓ2 then

20:

I ← I ∪ {e}.

21:

Switch order of ℓ1 and ℓ2 in L.

22:

{Note: ℓ1 and ℓ2 may have new neighbors in L.}

23:

if new neighbour of ℓ1 intersects ℓ1 then

24:

Add the intersection to Q.

25:

end if{Repeat above if statement for ℓ2.}

26:

end if

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

slide-6
SLIDE 6

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

  • utput

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