580 likes | 876 Views
Algorithm Design and Analysis (ADA). 242-535 , Semester 1 2013-2014. Objective an examination of four important CG topics just a taster of a very large research area. 16. Computational Geometry Topics. Overview. Intersection of Multiple Line Segments the sweeping algorithm
E N D
Algorithm Design and Analysis (ADA) 242-535, Semester 1 2013-2014 • Objective • an examination of four important CG topics • just a taster of a very large research area 16. Computational Geometry Topics
Overview • Intersection of Multiple Line Segments • the sweeping algorithm • Finding the Convex Hull • Graham Scan, Jarvis' March, QuickHull • Finding the Closest Pair of Points • divide-and-conquer • The Art Gallery Problem
1. Intersection of Multiple Line Segments 1.1. Multiple Line Segments 1.2. A Brute-Force Algorithm 1.3. The Sweeping Algorithm 1.4. Implementing Sweeping
1.1. Multiple Line Segments Input: a set of nline segments in the plane. Output: all intersections, and for each intersection the involved segments. .
1.2. A Brute-Force Algorithm Look at each pair of segments, and check if they intersect. If so, output the intersection. n(n-1)/2 comparison are needed in the worst case, so the running time is O(n2) But the lines are sparslydistributedin practice: • Most segments do not intersect, or if they do, only with a few other segments Need a faster algorithm that deals with such situations!
1.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 also known as the "Bentley-Ottmann" Algorithm and the Sweep Line Algorithm
Non-Degeneracy Assumptions No segment is vertical. // this means that the sweep line will always hit a segment at a point. If an input segment is vertical, then it is rotated clockwise by a tiny angle.
Sweep Line Status The set of segments intersecting the sweep line. It changes as the sweep line moves, but not continuously. endpoints Updates of status happen only at event points. intersections A G C T event points
Ordering Segments A total order over the segments that intersect the current position of the sweep line: later C > D (B drops out of the ordering) B > C > D (A and E not in the ordering) B E A C D At an event point, the sequence of segments changes: Update the status. Detect the intersections.
Status Update (1) Event point is the left endpoint of a segment. • A new segment L intersecting the sweep line K O L • Check if L intersects with the segment above (K) and the segment below (M). M new eventpoint N • Intersection(s) are new event points. K, M, N K, L, M, N
Status Update (2) Event point is an intersection. • The two intersecting segments (L and M) change order. K O L • Check intersection with new neighbors (M with O and L with N). M • Intersection(s) are new event points. N O, M, L, N O, L, M, N
Status Update (3) Event point is a lower endpoint of a segment. • The two neighbors (O and L) become adjacent. K O L • Check if they (O and L) intersect. M • Intersection is new event point. N O, M, L, N O, L, N
1.4. Implementing Sweeping The algorithm manages two kinds of data: • 1. The sweep line status gives the relationships among the objects intersected by the sweep line. • 2. The event point queue is a sequence of event points that defines the halting positions of the sweep line.
Event Point Queue Operations Manages the ordering of event points: • by x-coordinates • by y-coordinates in case of a tie in x-coordinates Supports the following operations on a segment s. m = #event points currently being managed fetching the next event // O(log m) // O(log m) inserting an event Every event point p is stored with all segments starting at p. Data structure: a balanced binary search tree(e.g., red-black tree).
Sweep Line Operations • The sweep line status (T) requires the following operations: • INSERT(T, s): insert segment s into T. • DELETE(T, s): delete segment s from T. • ABOVE(T, s): return the segment immediately above segment s in T. • BELOW(T, s): return the segment immediately below segment s in T. • Use a balanced binary search treefor T (e.g. Red-black trees): O(log n) for each operation
Segments intersect Pseudocode ANY-SEGMENTS-INTERSECT(S) 1 T = Ø 2 sort the endpoints of the segments in S from left to right, breaking ties by putting left endpoints before right endpoints and breaking further ties by putting points with lower y-coordinates first 3 for (each point p in the sorted list of endpoints) { 4 if (p is the left endpoint of a segment s) { 5 INSERT(T, s) 6 if (ABOVE(T, s) exists and intersects s) or (BELOW(T, s) exists and intersects s) 7 return TRUE } 8 if (p is the right endpoint of a segment s) { 9 if (both ABOVE(T, s) and BELOW(T, s) exist) and (ABOVE(T, s) intersects BELOW(T, s)) 10 return TRUE 11 DELETE(T, s) }} 12 return FALSE
Execution Example e d b the intersection of segments d and b is detected when segment c is deleted
Running Time • If the segment set contains n segments, then ANY-SEGMENTS-INTERSECT runs in time O(n log n) • Line 1 takes O(1) time. • Line 2 takes O(n log n) time, using merge sort or heapsort. • The for loop of lines 3–11 iterates at most once per event point, and so with 2n event points, the loop iterates at most 2n times. • Each iteration takes O(log n) time, since each tree operation takes O(log n) time and each intersection test takes O(1) time (by using the intersection function from part 15). • Total cost = O(1) + O(n * (log n + 1)) = O(n log n)
2. Finding the Convex Hull 2.1. Convex & Concave Sets 2.2. The Convex Hull 2.3. The Graham Scan 2.4. Jarvis’ March 2.5. QuickHull 2.6. Lower Bound of O(n log n)
2.1. Convex and Concave Sets p p R R 1 2 q q Concave Convex A planar region R is called convexif and only if for any pair of points p, qin R, the line segment pq lies completelyin R. Otherwise, it is called concave.
2.2. The Convex Hull The convex hullCH(P) of a set of points Pis the smallestconvex region that contains all of P. Rubber band When Pis finite, its convex hull is the unique convex polygonwhose vertices are from Pand that contains all points of P.
The Convex Hull Problem p 5 p p p 9 p 6 7 1 p p 2 p p 4 3 10 8 p Input: a set P = { p , p , …,p } of points 1 2 n Output: a list of vertices of CH(P) in counterclockwiseorder. Example Not all the points in P are in CH(P) CH(P) = [ p5, p9, p2, p8, p10, p7 ]
Edges of a Convex Hull For every edge both endpoints p, q P. All other points in P lie to the same side of the line passing through p and q q p all points on this side of the q – p edge
Floating Arithmetic is not Exact p to the left of qr. q to the left of rp. r to the left of qp. Nearly colinear points p, q, r. q r p All three accepted as edges! The algorithm is not robust– it could fail due to small numerical error.
2.3. The Graham Scan p p 6 9 p p 7 4 p 11 p p 8 p 5 10 p 3 p p 2 1 p 0 handling degeneracies sort by polar angle The center point has the minimumy-coordinate How to break a tie? Labels are in the polar angle order. (What if two points have the same polar angle?)
A Turning Algorithm • Consider each of the points in the sorted sequence. • For each point, is moving from the two previously considered points to this point a "left turn" or "right turn"? • "Right turn": this means that the second-to-last point is not part of the convex hull and should be removed. • this process is continued for as long as the set of the last three points is a "right turn" • "Left turn": the algorithm moves to the next point in the sequence.
Turning in Action C is a left turn compared to A – B; add C X D is a right turn compared to B - C; remove C D is a left turn compared to A - B; add D
The Graham Scan Algorithm Graham-Scan(P) let p0be the point in P with minimumy-coordinate let p1 , p2 , …, pn-1be the remaining points in P sorted in counterclockwise order by polar angle around p0 Stack s = new Stack(); s.push(p0) s.push(p1) s.push(p2) fori =3 ton 1 while ( pimakes a nonleftturn from the line segment determined by s.top() and s.nextToTop() ) s.pop() s.push(pi) returns the convex hull points are stored on a stack
Stack Usage S p 2 p 1 p 0 p p 6 9 p p 7 4 p 11 p p 8 p 5 10 p 3 p p 2 1 p 0
p p 6 9 p p 7 4 p 11 S p p 8 p 5 10 p p 3 3 p 1 p p 2 1 p 0 p 0
p p 6 9 p p 7 4 p 11 S p p 8 p 5 10 p p 4 3 p 1 p p 2 1 p 0 p 0
p p 6 S 9 p p 7 4 p 11 p p p 5 8 p 5 10 p p 4 3 p 1 p p 2 1 p 0 p 0
p p 6 S 9 p p 7 4 p 11 p p 6 p 8 p 5 10 p p 4 3 p 1 p p 2 1 p 0 p 0
S p p 8 p 6 9 p p p 7 4 7 p 11 p p 6 p 8 p 5 10 p p 4 3 p 1 p p 2 1 p 0 p 0
S p p 6 9 p p p 7 4 7 p 11 p p 6 p 8 p 5 10 p p 4 3 p 1 p p 2 1 p 0 p 0
S p p 10 p 6 9 p p 4 9 p p 11 7 p p 6 p 8 p 5 10 p p 4 3 p 1 p p 2 1 p 0 p 0
S p p 11 p 6 9 p p 4 9 p p 11 7 p p 6 p 8 p 5 10 p p 4 3 p 1 p p 2 1 p 0 p 0
Finish S p p 11 p 6 9 p p 4 9 p p 11 p 7 p 8 6 p p 5 10 p p 4 3 p 1 p p 2 1 p 0 p 0
Proof of Correctness p k p j p i p p i k p j p 0 p 0 Each point popped from stack S is not a vertex of CH(P). Two cases when pj is popped: Proof X X pk is a 0 angle turn compared to pi - pj pk is a right turn compared to pi - pj In neither case can pj become a vertex of CH(P).
p j The region containing pi p i p 0 The points on stack S always form the vertices of a convex polygon in counterclockwise order (an invariant). Proof • The claim holds at S initialization when p0, p1, p2 form a triangle (which is obviously convex) • Popping a point from S preserves the invariant. • Consider a point pi being pushed onto S. The invariant still holds.
Running Time Finding 1 (n) (n) p 0 #operations time / operation total Sorting 1 O(nlog n) O(nlog n) Push n O(1) (n) Pop n 2 O(1) O(n) Why? The running time of Graham’s Scan is O(nlog n).
2.4. Jarvis’ March A “package/gift wrapping” technique using two "chains"
The Operation of Jarvis’ March • We choose the first vertex as the lowest point p0, and start the right chain. • The next vertex, p1, has the smallest polar angle of any point with respect to p0. • Then, p2 has the smallest polar angle with respect to p1. • The right chain goes as high as the highest point p3. • Then,we return to p0 and build the left chain by finding smallest polar angles with respect to the negative x-axis.
Running Time of Jarvis’ March Let h be the number of vertices of the convex hull. • For each vertex, finding the point with the minimum Polar angle, that is, the next vertex, takes time O(n) • The comparison between two polar angles can be done using the cross product. Thus O(nh) time in total.
2.5. QuickHull • Concentrate on points close to hull boundary • Named for similarity to Quicksort • O(n log n) a b A Set QuickHull(a, b, S) if S = 0 return {} else c = index of point with max distance from a-b A = points strictly right of (a, c) B = points strictly right of (c, b) return QuickHull(a, c, A) + {c} + QuickHull(c, b, B) c finds one of upper or lower hull
2.6. Lower Bound of O(n log n) • The worst-case time to find the convex hull of n points using a decision tree model is O(n log n) • Proof based on sorting: • Given an unsorted list of n numbers: (x1,x2 ,…, xn) • Form an unsorted set of points: (xi, xi2) for each xi • Convex hull of these points produces a sorted list! • a parabola: so every point is on the convex hull • Finding the convex hull of n points is therefore at least as hard as sorting n points, so worst-case time is in O(n log n) Convex hull of red pts
3. Finding the Closest Pair of Points • There are a set of n points P = { p1,…pn }. • Find a pair of points p, q such that |p – q| is the minimum of all |pi – pj| • Easy to do in O(n2) time • for all pi ≠ pj, compute ║pi - pj║on all the pairs and choose the minimum, which involves n(n-1)/2 comparisons • We will aim for O(n log n) time
Divide and Conquer • Divide: • Compute the median of the x-coordinates • Split the points into a left half PL and right half PR, each of size n/2 • Conquer: compute the closest pairs for PL and PR • Combinethe results (the hard part) median line PL PR
Combine median line • dL = closestDist(PL)dR = closestDist(PR)d = min( d1, d2 ) • Observe: • Need to check only pairs which cross the dividing line • Only interested in pairs within distance < d of each other • It's enough to look at only the points in the 2d wide strip around the median line PL PR
Scanning the Strip • Sort all points in the strip by their y-coords, forming q1…qk, k ≤ n. • Let yi be the y-coord of qi dmin = d for i = 1 to k { j = i - 1 while (yi - yj < d){ if(║qi-qj║< d) dmin =║qi-qj║ j = j-1 } } • Report dmin (and the corresponding pair)