CS 372: Computational Geometry Lecture 3 Line Segment Intersection

Similar documents
CS 372: Computational Geometry Lecture 10 Linear Programming in Fixed Dimension

Computational Geometry

Introduction to Algorithms

Line segment intersection. Family of intersection problems

CS 532: 3D Computer Vision 11 th Set of Notes

CMSC 754 Computational Geometry 1

Computational Geometry

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

2 A Plane Sweep Algorithm for Line Segment Intersection

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

1/60. Geometric Algorithms. Lecture 1: Introduction. Convex Hulls

Computing intersections in a set of line segments: the Bentley-Ottmann algorithm

Computational Geometry

Linear Programming in Small Dimensions

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

CS 532: 3D Computer Vision 14 th Set of Notes

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

Advanced Algorithms Computational Geometry Prof. Karen Daniels. Fall, 2012

Data Structures and Algorithms

Computational Geometry Algorithmische Geometrie

Computational Geometry. Lecture 17

External-Memory Algorithms with Applications in GIS - (L. Arge) Enylton Machado Roberto Beauclair

Flavor of Computational Geometry. Voronoi Diagrams. Shireen Y. Elhabian Aly A. Farag University of Louisville

University of Waterloo CS240, Winter 2010 Assignment 2

Outline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.

COMP Analysis of Algorithms & Data Structures

5. DIVIDE AND CONQUER I

Course Review for Finals. Cpt S 223 Fall 2008

Elementary maths for GMT. Algorithm analysis Part II

More about The Competition

CS 361 Data Structures & Algs Lecture 11. Prof. Tom Hayes University of New Mexico

CS61BL. Lecture 5: Graphs Sorting

CMPS 3130/6130 Computational Geometry Spring Voronoi Diagrams. Carola Wenk. Based on: Computational Geometry: Algorithms and Applications

Computational Geometry TOPICS Preliminaries Point in a Polygon Polygon Construction Convex Hulls

Lecture 13: AVL Trees and Binary Heaps

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Balanced Binary Search Trees. Victor Gao

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Algorithm Design and Analysis

CS 410/584, Algorithm Design & Analysis, Lecture Notes 8!

High-Dimensional Computational Geometry. Jingbo Shang University of Illinois at Urbana-Champaign Mar 5, 2018

Notes and Answers to Homework Exam 1, Geometric Algorithms, 2017

Lecture 1: September 6, 2001

Programming II (CS300)

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

Algorithms, Spring 2014, CSE, OSU Lecture 2: Sorting

CSE 5311 Notes 13: Computational Geometry

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

Lecture: Analysis of Algorithms (CS )

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

2. Instance simplification Solve a problem s instance by transforming it into another simpler/easier instance of the same problem

07 B: Sorting II. CS1102S: Data Structures and Algorithms. Martin Henz. March 5, Generated on Friday 5 th March, 2010, 08:31

CS350: Data Structures Heaps and Priority Queues

Orthogonal line segment intersection. Computational Geometry [csci 3250] Laura Toma Bowdoin College

DIT960 Datastrukturer

CSE 214 Computer Science II Heaps and Priority Queues

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

Basic Data Structures (Version 7) Name:

CS 410/584, Algorithm Design & Analysis, Lecture Notes 8

Computational Geometry

Computer Science Spring 2005 Final Examination, May 12, 2005

Priority queues. Priority queues. Priority queue operations

Priority Queues. T. M. Murali. January 23, T. M. Murali January 23, 2008 Priority Queues

CSE 332, Spring 2010, Midterm Examination 30 April 2010

CS221: Algorithms and Data Structures. Sorting Takes Priority. Steve Wolfman (minor tweaks by Alan Hu)

Priority Queues. T. M. Murali. January 29, 2009

Randomized incremental construction. Trapezoidal decomposition: Special sampling idea: Sample all except one item

26 The closest pair problem

CS 361 Data Structures & Algs Lecture 9. Prof. Tom Hayes University of New Mexico

Unit Outline. Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity of Sorting 2 / 33

Line segment intersection (I): Orthogonal line segment intersection. Computational Geometry [csci 3250] Laura Toma Bowdoin College

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

CITS3001. Algorithms, Agents and Artificial Intelligence. Semester 2, 2016

Pseudo code of algorithms are to be read by.

Geometric Algorithms. 1. Objects

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Red-Black-Trees and Heaps in Timestamp-Adjusting Sweepline Based Algorithms

Lecture 7: Computational Geometry

Approximating a set of points by circles

Computer Science 302 Spring 2007 Practice Final Examination: Part I

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

Sorting and Searching

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Lecture 7. Transform-and-Conquer

Priority Queues. T. M. Murali. January 26, T. M. Murali January 26, 2016 Priority Queues

Binary heaps (chapters ) Leftist heaps

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Course Review. Cpt S 223 Fall 2009

Priority Queues and Heaps

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

l Heaps very popular abstract data structure, where each object has a key value (the priority), and the operations are:

COMP Analysis of Algorithms & Data Structures

Connected Components of Underlying Graphs of Halving Lines

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

Transcription:

CS 372: Computational Geometry Lecture 3 Line Segment Intersection Antoine Vigneron King Abdullah University of Science and Technology September 9, 2012 Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 1 / 32

1 Introduction 2 Preliminary 3 Intersection Detection 4 Intersection reporting Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 2 / 32

Problems Input: A set S = {s 1, s 2,... s n } of n line segments in R 2 given by the coordinates of their endpoints. s 4 s 1 s 5 s 3 s 2 s 6 Intersection detection: Is there a pair (s i, s j ) S 2 such that i j and s i s j? Intersection reporting: Find all pairs (s i, s j ) S 2 such that i j and s i s j. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 3 / 32

Motivation Motion planning: collision detection Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 4 / 32

Motivation Geographic Information Systems: Map overlay More generally: spatial join in databases ROAD MAP Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 5 / 32

Motivation Geographic Information Systems: Map overlay More generally: spatial join in databases ROAD MAP + RIVER MAP Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 6 / 32

Motivation Computer Aided Design: boolean operations P Q P \ Q Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 7 / 32

Intersection of Two Line Segments Finding the intersection of two line segments: Find the intersection of their two support lines Linear system, two variables, two equations. Check whether this intersection point is between the two endpoints of each line segment. If not, the intersection is empty. Degenerate case: same support line. Intersection may be a line segment. O(1) time. Checking whether two line segments intersect without computing the intersection point: 4 CCW ( ) tests. (How?) Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 8 / 32

First Approach Brute force algorithm: Check all pairs of segments for intersection. Running time: ( n 2 ) Θ(1) = Θ(n 2 ) Can we do better? Intersection detection: Yes, see later. Intersection reporting: If all pairs intersect there are Ω(n 2 ) intersections, then our time bound is optimal as a function of n. So we will look for an output-sensitive algorithm. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 9 / 32

Plane Sweep Algorithms Intuition: A vertical line sweeps the plane from left to right and draws the picture. Example: last week s convex hull algorithm. Current event point I know the upper hull Not handled yet Sweep line Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 10 / 32

Plane Sweep Algorithms The sweep line moves from left to right and stops at event points. Convex hull: The event points are just the input points. Sometimes they are not known from the start (see intersection reporting). We maintain invariants. Convex hull: I know the upper hull of the points to the left of the sweep line. At each event, restore the invariants. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 11 / 32

General Position Assumptions No three endpoints are collinear. Let E be the set of the endpoints. Let I be the set of intersection points. No two points in E I have same x-coordinate. No three segments intersect at the same point. Degenerate Cases s s 3 collinear endpoints same x-coordinate multiple intersection Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 12 / 32

Intersection Detection Plane sweep algorithm Let l t be the vertical line with equation x = t. S t is the sequence of segments that intersect l t, ordered by the y-coordinates of their intersections with l t. S t = (s 4, s 5, s 3 ) s 1 s 3 s 7 s 2 s 5 s 6 s 4 l t Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 13 / 32

Intersection Detection Idea: Maintain S t while l t moves from left to right until an intersection is found. Invariants: There is no intersection to the left of lt. We know St. Let t 1 < t 2 < < t 2n be the x-coordinates of the endpoints. l t stops at each time t = t i. Let f be the last index the sweep-line l ti reaches. In other words, let f be the largest index such that there is no intersection point with abscissa smaller than t f. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 14 / 32

Example No intersection l tf = l t7 l tf +1 Leftmost intersection Knowing S ti slides.) for some i < f, we can easily find S ti+1. (See next two Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 15 / 32

First Case: Left Endpoint If t i+1 corresponds to the left endpoint of s, insert s into S ti obtain S ti+1. in order to s 4 s 1 St3 = (s 2, s 3, s 1 ) s 2 s 5 s 3 S t4 = (s 2, s 3, s 4, s 1 ) l t3 l t4 Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 16 / 32

Second Case: Right Endpoint Delete the corresponding segment in order to obtain S ti+1. s 4 s 1 S t4 = (s 2, s 3, s 4, s 1 ) s 2 s 5 s 3 S t5 = (s 2, s 3, s 4 ) l t4 l t5 Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 17 / 32

Data Structure We maintain S t in a balanced binary search tree. For i < f, we obtain S ti+1 from S ti by performing an insertion or a deletion. Each takes O(log n) time. We did not check intersections. How to do it? Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 18 / 32

A Geometric Observation Let q = s s be the leftmost intersection point. s q s l tf = l t7 l tf +1 s and s are adjacent in S tf (proof next slide). Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 19 / 32

Proof by Contradiction Assume that S tf = (... s... s... s... ). The right endpoint of s cannot be to the left of q, by definition of t f. If q is below s, then s intersect s to the left of q, which contradicts the fact that q is the leftmost intersection. Similarly, q cannot be above s. We reached a contradiction. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 20 / 32

Checking Intersection We store S t in a balanced binary search tree T, the order is the vertical order along l t. Given a segment in T, we can find in O(log n) time the next and previous segment in vertical order. When deleting a segment in T, two segments s and s become adjacent. We can find them in O(log n) time and check if they intersect. When inserting a segment s i in T, it becomes adjacent to (at most) two segments s and s. Check if s i s and if s i s. In any case, if we find an intersection, we are done. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 21 / 32

Pseudocode Line segment intersection dectection Algorithm DetectIntersection(S) Input: A set S of line segments in R 2 Output: TRUE iff there exist two intersecting segments in S 1. (e 1, e 2... e 2n ) Endpoints, ordered by increasing x-coordinate 2. T empty balanced BST 3. for i = 1 to 2n 4. if e i is left endpoint of some s S 5. then insert s into T 6. if s intersects next(s) or prev(s) 7. then return TRUE 8. if e i is right endpoint of some s S 9. if next(s) intersects prev(s) 10. then return TRUE 11. else delete s from T 12. return FALSE Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 22 / 32

Analysis Line 1: Θ(n log n) time by mergesort Line 2: O(1) time Line 5 and 6: O(log n) time Loop 3 11: O(n log n) time So our algorithm runs in Θ(n log n) time We will see later that it is optimal in some sense. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 23 / 32

Intersection Reporting Brute force: Θ(n 2 ) time, which is optimal in the worst case, when there are k = Ω(n 2 ) intersections. If there is 1 intersection, the detection algorithm does it in O(n log n) time which is better. We will be able to interpolate between these two algorithms, and find an output-sensitive algorithm. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 24 / 32

Output Sensitive Algorithm Let k = #intersecting pairs. Here k = # intersection points, since we assume general position. k is the output size. Sweep line algorithm reports intersections in O((n + k) log n) time (see later). This is an output sensitive algorithm. Ω(n + k) is a lower bound nearly optimal (within an O(log n) factor). Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 25 / 32

Algorithm Similar to intersection detection. Two types of event points: endpoints and intersection points. We do not know intersection points in advance. We cannot sort them all in advance. We will use an event queue Q. Q contains event points ordered by increasing x-coordinate. Implementation: a min-heap. We can insert an event point in O(log n) time. We can dequeue the event point with smallest x-coordinate in O(log n) time. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 26 / 32

Algorithm Initially, Q contains all the endpoints. The sweep line moves from left to right. It stops at each event point of Q. When it does, we dequeue the corresponding event point. Each time we find an intersection, we insert it into Q. When we reach an intersection point, we swap the corresponding segments in T, and check for intersection the newly adjacent segments. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 27 / 32

Intersection Event s 1 s 2 s 4 s 3 S t = (s 5, s 2, s 3, s 4 ) s 5 S t = (s 5, s 3, s 2, s 4 ) l t l t Check s 4 s 2 and s 3 s 5 At time t, the event queue Q contains all the endpoints with abscissa larger than t and the intersection point s 2 s 3. At time t, we insert s 3 s 5 into Q. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 28 / 32

Analysis Each event is inserted or deleted in O(log(n + k)) time. Problem: An intersection point may be inserted several times into Q. s 1 s 2 is inserted three times. - when we reach the left endpoint of s 2 s 2 - when we reach the right endpoint of s 3 - when we reach the right endpoint of s 4 s 3 s 4 s 1 When we reach an event point that is present several times in Q, we just extract it repeatedly until a new event is found. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 29 / 32

Analysis Some intersection points are inserted several times into Q. How many times does it happen? At most twice at each endpoint or intersection point. At most 4n + 2k times in total. It follows that we insert O(n + k) events into Q. So the algorithm runs in O((n + k) log(n + k)) time. log(n + k) < log(n + n 2 ) = O(log n) The running time is O((n + k) log n). Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 30 / 32

Space Complexity In the worst case, Q contains O(n + k) points. This algorithm requires O(n + k) space. Can we do better? Yes: At time t, only keep intersections between segments that are adjacent in S t. Then the algorithm requires only O(n) space. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 31 / 32

Conclusion New algorithmic paradigm: Plane Sweep. Idea: a sweep line moves from left to right and draws the picture. It stops at a finite set of event points, where the data structure is updated. The event points are stored in a priority queue. It can be used for other problems. For instance: Map overlay: compute a full description of the map. Fixed-radius near neighbor searching in 2D. Antoine Vigneron (KAUST) CS 372 Lecture 3 September 9, 2012 32 / 32