Introduction to Algorithms

Similar documents
CS 372: Computational Geometry Lecture 3 Line Segment Intersection

External Memory Algorithms for Geometric Problems. Piotr Indyk (slides partially by Lars Arge and Jeff Vitter)

Orthogonal Range Queries

Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2005 Simonas Šaltenis E1-215b

Line segment intersection. Family of intersection problems

Computational Geometry

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

Binary Search Trees Treesort

Polygon Triangulation. (slides partially by Daniel Vlasic )

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

Point Enclosure and the Interval Tree

2 A Plane Sweep Algorithm for Line Segment Intersection

Data Structures and Algorithms

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

1 The range query problem

Plane Sweep Algorithms

Polygon Triangulation. (slides partially by Daniel Vlasic )

CMSC 754 Computational Geometry 1

Lecture 9 March 15, 2012

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

26 The closest pair problem

More about The Competition

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

Priority Queues and Binary Heaps

Computational Geometry

CMSC 341 Lecture 14: Priority Queues, Heaps

Computational Geometry

Solutions to problem set 1

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

Sorting and Selection

Multidimensional Divide and Conquer 2 Spatial Joins

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

Solutions to Problem Set 1

Algorithms. Algorithms GEOMETRIC APPLICATIONS OF BSTS. 1d range search line segment intersection kd trees interval search trees rectangle intersection

Geometric Computation: Introduction. Piotr Indyk

Algorithms. Algorithms GEOMETRIC APPLICATIONS OF BSTS. 1d range search line segment intersection kd trees interval search trees rectangle intersection

Lecture 3 February 9, 2010

Geometric Algorithms. Geometric search: overview. 1D Range Search. 1D Range Search Implementations

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

Lecture 8 13 March, 2012

Computational Geometry

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

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

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

1D Range Searching (cont) 5.1: 1D Range Searching. Database queries

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

Algorithms GEOMETRIC APPLICATIONS OF BSTS. 1d range search line segment intersection kd trees interval search trees rectangle intersection

Computational Geometry

Balanced Binary Search Trees. Victor Gao

COS 226 Algorithms and Data Structures Fall Midterm Solutions

Algorithms. Algorithms GEOMETRIC APPLICATIONS OF BSTS. 1d range search line segment intersection kd trees interval search trees rectangle intersection

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

I/O-Algorithms Lars Arge Aarhus University

! Good algorithms also extend to higher dimensions. ! Curse of dimensionality. ! Range searching. ! Nearest neighbor.

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

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

CS S Lecture February 13, 2017

Range Searching II: Windowing Queries

Efficient Algorithmic Techniques for Several Multidimensional Geometric Data Management and Analysis Problems

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

Introduction to Computers and Programming. Today

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

Planar Point Location

6.854J / J Advanced Algorithms Fall 2008

9. Heap : Priority Queue

Parallel Sorting Algorithms

27 Geometric Intersection

Containers and genericity. A standardized naming scheme. Cursor properties. Lists. Adding a cell. A specific implementation: (singly) linked lists

Algorithms. Algorithms GEOMETRIC APPLICATIONS OF BSTS. 1d range search kd trees line segment intersection interval search trees rectangle intersection

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Algorithm Analysis Advanced Data Structure. Chung-Ang University, Jaesung Lee

Binary heaps (chapters ) Leftist heaps

Simulation Algorithms

Computational Geometry. Lecture 17

Priority Queues and Heaps

Data Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions.

CS102 Binary Search Trees

Field: We have been doing geometry eg linear programming. But in computational geometry, key difference in focus: low dimension d

Trapezoid and Chain Methods

CS133 Computational Geometry

Priority Queues (Heaps)

Priority Queues. 04/10/03 Lecture 22 1

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

Lecture Notes on Priority Queues

Recursion: The Beginning

Testing Bipartiteness of Geometric Intersection Graphs David Eppstein

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Computational Geometry

Computation Geometry Exercise Range Searching II

The priority is indicated by a number, the lower the number - the higher the priority.

COS 226 Algorithms and Data Structures Practice Design Questions

CS61BL. Lecture 5: Graphs Sorting

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

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

CS6100: Topics in Design and Analysis of Algorithms

Pop Quiz 2: Answers. Data structures Recursion Big-O

WINDOWING PETR FELKEL. FEL CTU PRAGUE Based on [Berg], [Mount]

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

Algorithms and Data Structures

CSE100. Advanced Data Structures. Lecture 8. (Based on Paul Kube course materials)

Transcription:

Introduction to Algorithms 6.046J/18.401 Lecture 17 Prof. Piotr Indyk

Computational Geometry ctd. Segment intersection problem: Given: a set of n distinct segments s 1 s n, represented by coordinates of endpoints Detection: detect if there is any pair s i s j that intersects Reporting: report all pairs of intersecting segments 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.2

Segment intersection Easy to solve in O(n 2 ) time Is it possible to get a better algorithm for the reporting problem? NO (in the worst-case) However: We will see we can do better for the detection problem Moreover, the number of intersections P is usually small. Then, we would like an output sensitive algorithm, whose running time is low if P is small. 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.3

Result We will show: O(n log n) time for detection O( (n +P) log n) time for reporting We will use (no, not divide and conquer) Binary Search Trees Specifically: Line sweep approach 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.4

Orthogonal segments V-segment All segments are either horizontal or vertical Assumption: all coordinates are distinct Therefore, only vertical- horizontal intersections exist H-segment 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.5

Orthogonal segments Sweep line: A vertical line sweeps the plane from left to right It stops at all important x-coordinates, i.e., when it hits a V-segment or endpoints of an H-segment Invariant: all intersections on the left side of the sweep line have been already reported 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.6

Orthogonal segments ctd. We maintain sorted y- coordinates of H-segments currently intersected by the sweep line (using a balanced 17 BST V) When we hit the left point of 12 an H-segment, we add its y- coordinate to V When we hit the right point of an H-segment, we delete its y- coordinate from V 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.7

Orthogonal segments ctd. y top Whenever we hit a V- segment having coord. ytop, y bot ), we report all H-segments in V with y- coordinates in [y top, y bot ] y bot 17 12 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.8

Algorithm Sort all V-segments and endpoints of H- segments by their x-coordinates this gives the trajectory of the sweep line Scan the elements in the sorted list: Left endpoint: add segment to tree V Right endpoint: remove segment from V V-segment: report intersections with the H-segments stored in V 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.9

Analysis Sorting: O(n log n) Add/delete H-segments to/from vertical data structure V: O(log n) per operation O(n log n) total Processing V-segments: O(log n) per intersection - SEE NEXT SLIDE O(P log n) total Overall: O( (P+ n) log n) time Can be improved to O(P +n log n) 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.10

Analyzing intersections Given: A BST V containing y-coordinates An interval I=[y bot,y top ] Goal: report all y s in V that belong to I Algorithm: y=successor(y bot ) While y y top Report y y:=successor(y) End Time: (number of reported y s)*o(logn) + O(logn) 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.11

The general case Assumption: all coordinates of endpoints and intersections distinct In particular: No vertical segments No three segments intersect at one point 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.12

Sweep line Invariant (as before): all intersections on the left of the sweep line have been already reported Stops at all important x- coordinates, i.e., when it hits endpoints or intersections Do not know the intersections in advance! The list of intersection coordinates is constructed and maintained dynamically (in a horizontal data structure H) 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.13

Sweep line Also need to maintain the information about the segments intersecting the sweep line Cannot keep the values of y- coordinates of the segments! Instead, we will maintain their order.i.e., at any point, we maintain all segments intersecting the sweep line, sorted by the y- coordinates of the intersections (in a vertical data structure V) 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.14

Algorithm Initialize the vertical BST V (to empty ) Initialize the horizontal priority queue H (to contain the segments endpoints sorted by x-coordinates) Repeat Take the next event p from H: // Update V If p is the left endpoint of a segment, add the segment to V If p is the right endpoint of a segment, remove the segment from V If p is the intersection point of s and s, swap the order of s and s in V, report p 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.15

Algorithm ctd. // Update H For each new pair of neighbors s and s in V: Check if s and s intersect on the right side of the sweep line If so, add their intersection point to H Remove the possible duplicates in H Until H is empty 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.16

Analysis Initializing H: O(n log n) Updating V: O(log n) per operation O( (P+n) log n) total Updating H: O(log n) per intersection O(P log n) total Overall: O( (P+ n) log n) time 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.17

Correctness All reported intersections are correct Assume there is an intersection not reported. Let p=(x,y) be the first such unreported intersection (of s and s ) Let x be the last event before p. Observe that: At time x segments s and s are neighbors on the sweep line Since no intersections were missed till then, V maintained the right order of intersecting segments Thus, s and s were neighbors in V at time x. Thus, their intersection should have been detected 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.18

Changes Y s change the order 2003 by Piotr Indyk Introduction to Algorithms November 15, 2004 L18.19