Presentation is loading. Please wait.

Presentation is loading. Please wait.

242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis.

Similar presentations


Presentation on theme: "242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis."— Presentation transcript:

1 242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis (ADA) 242-535, Semester 1 2014-2015 16. Computational Geometry Topics

2 242-535 ADA: 16. CG Topics2 1.Intersection of Multiple Line Segments o the sweeping algorithm 2.Finding the Convex Hull o Graham Scan, Jarvis' March, QuickHull 3.Finding the Closest Pair of Points o divide-and-conquer 4.The Art Gallery ProblemOverview

3 242-535 ADA: 16. CG Topics3 1.1. Multiple Line Segments 1.2. A Brute-Force Algorithm 1.3. The Sweeping Algorithm 1.4. Implementing Sweeping 1. Intersection of Multiple Line Segments

4 1.1. Multiple Line Segments Input: a set of n line segments in the plane. Output : all intersections, and for each intersection the involved segments..

5 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(n 2 ) Most segments do not intersect, or if they do, only with a few other segments Need a faster algorithm that deals with such situations! But the lines are sparsly distributed in practice:

6 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 also known as the "Bentley-Ottmann" Algorithm and the Sweep Line Algorithm

7 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.

8 Sweep Line Status Sweep Line Status The set of segments intersecting the sweep line. It changes as the sweep line moves, but not continuously. Updates of status happen only at event points. endpoints intersections event points A G C T

9 Ordering Segments A total order over the segments that intersect the current position of the sweep line: A B C D E B > C > D (A and E not in the ordering) C > D (B drops out of the ordering) At an event point, the sequence of segments changes:  Update the status.  Detect the intersections. later

10 Status Update (1)  A new segment L intersecting the sweep line L M K  Check if L intersects with the segment above (K) and the segment below (M). new event point  Intersection(s) are new event points. Event point is the left endpoint of a segment. N K, M, NK, L, M, N O

11 Status Update (2)  The two intersecting segments (L and M) change order. L M K  Check intersection with new neighbors (M with O and L with N).  Intersection(s) are new event points. Event point is an intersection. N O O, L, M, N O, M, L, N

12 Status Update (3)  The two neighbors (O and L) become adjacent. L M K  Check if they (O and L) intersect.  Intersection is new event point. Event point is a lower endpoint of a segment. N O, M, L, NO, L, N O

13 242-535 ADA: 16. CG Topics13 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. 1.4. Implementing Sweeping

14 Event Point Queue Operations Data structure: a balanced binary search tree (e.g., red-black tree). 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.  inserting an event  fetching the next event Every event point p is stored with all segments starting at p. // O(log m) m = # event points currently being managed m = # event points currently being managed

15 242-535 ADA: 16. CG Topics15 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 tree for T (e.g. Red-black trees): O(log n) for each operation Sweep Line Operations

16 242-535 ADA: 16. CG Topics16 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 Segments intersect Pseudocode

17 242-535 ADA: 16. CG Topics17 Execution Example edbedb the intersection of segments d and b is detected when segment c is deleted

18 242-535 ADA: 16. CG Topics18 If the segment set contains n segments, then ANY-SEGMENTS-INTERSECT runs in time O(n log n) o Line 1 takes O(1) time. o Line 2 takes O(n log n) time, using merge sort or heapsort. o 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. o 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). o Total cost = O(1) + O(n * (log n + 1)) = O(n log n) Running Time

19 242-535 ADA: 16. CG Topics19 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. Finding the Convex Hull

20 2.1. Convex and Concave Sets A planar region R is called convex if and only if for any pair of points p, q in R, the line segment pq lies completely in R. Otherwise, it is called concave. Convex p q R 1 p q R 2 Concave

21 2.2. The Convex Hull The convex hull CH(P) of a set of points P is the smallest convex region that contains all of P. Rubber band When P is finite, its convex hull is the unique convex polygon whose vertices are from P and that contains all points of P.

22 The Convex Hull Problem Input: a set P = { p, p, …, p } of points Output: a list of vertices of CH(P) in counterclockwise order. 1 2 n Example 8 p p p p p p p p p p 9 2 10 5 76 1 3 4 CH(P) = [ p 5, p 9, p 2, p 8, p 10, p 7 ] Not all the points in P are in CH(P) Not all the points in P are in CH(P)

23 Edges of a Convex Hull For every edge both endpoints p, q  P. p q All other points in P lie to the same side of the line passing through p and q all points on this side of the q – p edge

24 Floating Arithmetic is not Exact p r q Nearly colinear points p, q, r. p to the left of qr. q to the left of rp. r to the left of qp. All three accepted as edges! The algorithm is not robust – it could fail due to small numerical error.

25 2.3. The Graham Scan p 0 p p p p p p p p p p 1 2 3 4 p 5 6 7 8 9 10 11 The center point has the minimum y-coordinate Labels are in the polar angle order. (What if two points have the same polar angle?) How to break a tie? sort by polar angle handling degeneracies

26 242-535 ADA: 16. CG Topics26 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. o 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. A Turning Algorithm

27 242-535 ADA: 16. CG Topics27 Turning in Action C is a left turn compared to A – B; add C D is a right turn compared to B - C; remove C D is a left turn compared to A - B; add D X

28 Graham-Scan(P) let p0 be the point in P with minimum y-coordinate let  p1, p2, …, pn-1  be 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) for i = 3 to n  1 while ( pi makes a nonleft turn from the line segment determined by s.top() and s.nextToTop() ) s.pop() s.push(pi) return s The Graham Scan Algorithm the convex hull points are stored on a stack the convex hull points are stored on a stack

29 Stack Usage p p p p p p p p p p p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 2

30 p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 3

31 p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 4

32 p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 4 p 5

33 p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 4 p 6

34 p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 4 p 6 p 7 p 8

35 p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 4 p 6 p 7

36 p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 4 p 6 p 9 p 10

37 p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 11 p p p S 0 1 4 p 6 p 9 p

38 Finish p p p p p p p p pp p 0 1 2 3 4 p 5 6 7 8 9 10 11 p p p S 0 1 4 p 6 p 9 p

39 Proof of Correctness Each point popped from stack S is not a vertex of CH(P). p p p p 0 i j k p p p p 0 i j k Two cases when pj is popped: In neither case can pj become a vertex of CH(P). Proof pk is a right turn compared to pi - pj pk is a right turn compared to pi - pj pk is a 0 angle turn compared to pi - pj pk is a 0 angle turn compared to pi - pj X X

40 The points on stack S always form the vertices of a convex polygon in counterclockwise order (an invariant). Popping a point from S preserves the invariant. The region containing pi The invariant still holds. Proof The claim holds at S initialization when p0, p1, p2 form a triangle (which is obviously convex) Consider a point pi being pushed onto S. p 0 p j p i

41 Running Time The running time of Graham’s Scan is O(n log n). #operations time / operation total Push n O(1)  (n) Pop  n  2 O(1) O(n) Sorting 1 O(n log n) O(n log n) Why? Finding p 0 1  (n)  (n)

42 2.4. Jarvis’ March A “package/gift wrapping” technique using two "chains"

43 242-535 ADA: 16. CG Topics43 We choose the first vertex as the lowest point p0, and start the right chain. o The next vertex, p1, has the smallest polar angle of any point with respect to p0. o Then, p2 has the smallest polar angle with respect to p1. o 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. The Operation of Jarvis’ March

44 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.

45 242-535 ADA: 16. CG Topics45 Concentrate on points close to hull boundary Named for similarity to Quicksort o O(n log n) 2.5. QuickHull 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) a b finds one of upper or lower hull c A

46 242-535 ADA: 16. CG Topics46 The worst-case time to find the convex hull of n points using a decision tree model is  (n log n) Proof based on sorting: o Given an unsorted list of n numbers: (x 1,x 2,…, x n ) o Form an unsorted set of points: (x i, x i 2 ) for each x i o Convex hull of these points produces a sorted list! a parabola: so every point is on the convex hull o Finding the convex hull of n points is therefore at least as hard as sorting n points, so worst-case time is in  (n log n) 2.6. Lower Bound of O(n log n) Convex hull of red pts

47 242-535 ADA: 16. CG Topics47 There are a set of n points P = { p 1, …p n }. Find a pair of points p, q such that |p – q| is the minimum of all |p i – p j | Easy to do in O(n 2 ) time o 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 3. Finding the Closest Pair of Points

48 242-535 ADA: 16. CG Topics48 Divide : o Compute the median of the x-coordinates o 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 Combine the results (the hard part) Divide and Conquer PLPR median line

49 242-535 ADA: 16. CG Topics49 dL = closestDist(PL) dR = closestDist(PR) d = min( d1, d2 ) Observe: o Need to check only pairs which cross the dividing line o 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 lineCombine PLPR median line

50 242-535 ADA: 16. CG Topics50 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) Scanning the Strip

51 242-535 ADA: 16. CG Topics51 Combine : O(n log n) because we sort the y-coords. But, we can: o Sort all points the y-coords at the beginning, outside of the main loop since the Divide stage preserves the y-order of points o Then this combine stage only takes O(n) We get T(n)=2T(n/2)+O(n), so T(n) is O(n log n) Running Time divide the data by half and calculate the closest pair on PL and PR combine

52 4. The Art Gallery Problem camera How many cameras are needed to guard a gallery and where should they be placed?

53 Simple Polygon Model Model the art gallery as a region bounded by some simple polygon (no self-crossing). Regions with holes are not allowed. convex polygon one camera an arbitrary n-gon (n vertices) Bad news: finding the minimum number of cameras for a given polygon is NP-hard (exponential time).

54 Triangulation To make things easier, we decompose a polygon into pieces that are easy to guard. Draw diagonals between pairs of vertices. diagonals Triangulation: decomposition of a polygon into triangles by a maximal set of non-intersecting diagonals. Guard the art gallery by placing a camera in every triangle …

55 No. of Cameras Theorem: Every simple polygon has a triangulation. Any triangulation of a simple polygon with n vertices consists of n – 2 triangles.  n – 2 cameras can guard the simple polygon. Note: a camera sitting on a diagonal guards two triangles.  the no. of cameras can be reduced to roughly n/2 Note: a vertex is adjacent to many triangles so placing cameras at vertices can reduce the number even more…

56 3-Coloring of Vertices Idea: Select vertices, such that every triangle has at least one of those vertices. Assign each vertex a color: pink, green, or yellow. Any two vertices connected by an edge or a diagonal must be assigned different colors. Thus the vertices of every triangle will be in three different colors. Choose the smallest color set, and place cameras at all the vertices using that color   n/3  cameras.

57 The Dual Graph A dual graph G has a node inside every triangle and an edge between every pair of nodes whose corresponding triangles share a diagonal. The dual graph G is actually a tree (marked in red on this slide), and it is possible to build one using DFS.


Download ppt "242-535 ADA: 16. CG Topics1 Objective o an examination of four important CG topics o just a taster of a very large research area Algorithm Design and Analysis."

Similar presentations


Ads by Google