Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Computational Geometry Instructor: yedeshi

Similar presentations


Presentation on theme: "1 Computational Geometry Instructor: yedeshi"— Presentation transcript:

1 1 Computational Geometry Instructor: yedeshi yedeshi@gmail.com

2 2 Computational Geometry Algorithms for geometric problems Applications: CAD, GIS, computervision,……. E.g., the closest pair problem: Given: a set of points P={p 1 … p n } in the plane, such that p i =(x i,y i ) Goal: find a pair p i ≠p j that minimizes ||p i –p j ||, where ||p-q||= [(p x -q x ) 2 +(p y -q y ) 2 ] 1/2

3 3 Line-segment A convex combination of two distinct points p 1 = (x 1, y 1 ) and p 2 = (x 2, y 2 ) is any point p 3 = (x 3, y 3 ) such that for some α in the range 0 ≤ α ≤ 1, we have x 3 = αx 1 + (1 - α)x 2 and y 3 = αy 1 + (1 - α)y 2. Also, p 3 = αp 1 + (1 - α)p 2. Line segment: Given two distinct points p 1 and p 2, the line segment is the set of convex combinations of p 1 and p 2. We call p 1 and p 2 the endpoints of segment Sometimes the ordering of p 1 and p 2 matters, and we speak of the directed segment

4 4 Cross products Computing cross products is at the heart of our line- segment methods Consider vectors p 1 and p 2 : The cross product p 1 × p 2 can be interpreted as the signed area of the parallelogram formed by the points (0, 0), p 1, p 2, and p 1 + p 2 = (x 1 + x 2, y 1 + y 2 ).

5 5 Cross product An equivalent, but more useful, definition gives the cross product as the determinant of a matrix

6 6 Cross product If p 1 × p 2 is positive, then p 1 is clockwise from p 2 with respect to the origin (0, 0); If it is negative, then p 1 is counterclockwise from p 2 Boundary condition: cross product is 0; in this case, the vectors are collinear, pointing in either the same or opposite directions. clockwise and counterclockwise regions relative to a vector p p 1 × p 2 is positive

7 7 clockwise To determine whether a directed segment is clockwise from a directed segment ? Compute (p 1 - p 0 ) × (p 2 - p 0 ) = (x 1 - x 0 )(y 2 - y 0 ) - (x 2 - x 0 )(y 1 - y 0 ). (a) If counterclockwise, the points make a left turn (b) If clockwise, they make a right turn

8 8 Two line segments intersect Determine whether two line segments intersect, we check whether each segment straddles the line containing the other. A segment straddles a line if point p 1 lies on one side of the line and point p 2 lies on the other side. Two line segments intersect if and only if either (or both) of the following conditions holds: 1.Each segment straddles the line containing the other. 2.An endpoint of one segment lies on the other segment. (This condition comes from the boundary case.)

9 9 Algorithm SEGMENTS-INTERSECT(p 1, p 2, p 3, p 4 ) 1 d 1 ← DIRECTION(p 3, p 4, p 1 ) 2 d 2 ← DIRECTION(p 3, p 4, p 2 ) 3 d 3 ← DIRECTION(p 1, p 2, p 3 ) 4 d 4 ← DIRECTION(p 1, p 2, p 4 ) 5 if ((d 1 > 0 and d 2 0)) and ((d 3 > 0 and d 4 0)) 6 then return TRUE 7 elseif d 1 = 0 and ON-SEGMENT(p 3, p 4, p 1 ) 8 then return TRUE 9 elseif d 2 = 0 and ON-SEGMENT(p 3, p 4, p 2 ) 10 then return TRUE 11 elseif d 3 = 0 and ON-SEGMENT(p 1, p 2, p 3 ) 12 then return TRUE 13 elseif d 4 = 0 and ON-SEGMENT(p 1, p 2, p 4 ) 14 then return TRUE 15 else return FALSE

10 10 Algorithm con. DIRECTION: computes relative orientations using the cross-product method ON-SEGMENT: which determines whether a point known to be collinear with a segment lies on that segment.

11 11 Algorithm con. DIRECTION(p i, p j, p k ) 1 return (p k - p i ) × (p j - p i ) ON-SEGMENT(p i, p j, p k ) 1 if min(x i, x j ) ≤ x k ≤ max(x i, x j ) and min(y i, y j ) ≤ y k ≤ max(y i, y j ) 2 then return TRUE 3 else return FALSE

12 12 Segment intersection 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

13 13 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) How to prove it? 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.

14 14 Θ(n 2 ) intersections

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

16 16 Determining segments intersects Determining whether any two line segments in a set of segments intersect. Sweeping: is common to many computational- geometry algorithms. An imaginary vertical sweep line passes through the given set of geometric objects, usually from left to right. Ordering segments: Two segments s 1 and s 2, comparable at x if the vertical sweep line with x-coordinate x intersects both of them

17 17 Order segments s 1 is above s 2 at x, written s 1 > x s 2, if s 1 and s 2 are comparable at x and the intersection of s 1 with the sweep line at x is higher than the intersection of s 2 with the same sweep line. For example, we have the relationships a > r c, a > t b, b > t c, a > t c, and b > u c. Segment d is not comparable with any other segment. When segments e and f intersect, their orders are reversed: we have e > v f but f > w e.

18 18 Moving the sweep line Sweeping algorithms typically manage two sets of data: The sweep-line status gives the relationships among the objects intersected by the sweep line. The event-point schedule is a sequence of x-coordinates, ordered from left to right, that defines the halting positions of the sweep line. We call each such halting position an event point. Changes to the sweep-line status occur only at event points. The sweep-line status is a total order T, for which we require 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. If there are n segments in the input, we can perform each of the above operations in O(lg n) time using red-black trees.

19 19 Segment-intersection 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 do if p is the left endpoint of a segment s 5 then INSERT(T, s) 6 if (ABOVE(T, s) exists and intersects s) or (BELOW(T, s) exists and intersects s) 7 then return TRUE 8 if p is the right endpoint of a segment s 9 then if both ABOVE(T, s) and BELOW(T, s) exist and ABOVE(T, s) intersects BELOW(T, s) 10 then return TRUE 11 DELETE(T, s) 12 return FALSE

20 20 Example Each dashed line is the sweep line at an event point, and the ordering of segment names below each sweep line is the total order T at the end of the for loop in which the corresponding event point is processed. The intersection of segments d and b is found when segment c is deleted.

21 21 Correctness We will prove that the call ANY-SEGMENTS-INTERSECT(S) returns TRUE if and only if there is an intersection among the segments in S. if it returns TRUE, there is an intersection. if there is an intersection, then ANY-SEGMENTS-INTERSECT returns TRUE Let us suppose that there is at least one intersection. Let p be the leftmost intersection point. And let a and b be the segments that intersect at p. There exists a sweep line z at which a and b become consecutive in the total order. z is to the left of p or goes through p. There exists a segment endpoint q on sweep line z that is the event point at which a and b become consecutive in the total order. If p is on sweep line z, then q = p. If p is not on sweep line z, then q is to the left of p.

22 22 Correctness If q is processed by ANY-SEGMENTS- INTERSECT, there are only two possibilities for the action taken: Either a or b is inserted into T, and the other segment is above or below it in the total order. Lines 4-7 detect this case. Segments a and b are already in T, and a segment between them in the total order is deleted, making a and b become consecutive. Lines 8-11 detect this case. In either case, the intersection p is found and ANY- SEGMENTS-INTERSECT returns TRUE.

23 23 Running time If there are n segments in set S, then ANY-SEGMENTS- INTERSECT runs in time O(n lg n). Line 1 takes O(1) time. Line 2 takes O(n lg n) time, using merge sort or heapsort. Since there are 2n event points, the for loop of lines 3-11 iterates at most 2n times. Each iteration takes O(lg n) time, since each red-black-tree operation takes O(lg n) time and, using the method of line intersection, each intersection test takes O(1) time. The total time is thus O(n lg n).

24 24 The closet pair problem the closest pair problem: Given: a set of points P={p 1 … p n } in the plane, such that p i =(x i,y i ) Goal: find a pair p i ≠p j that minimizes ||p i –p j ||, where ||p-q||= [(p x -q x ) 2 +(p y -q y ) 2 ] 1/2

25 25 Closest Pair Find a closest pair among p 1 …p n Easy to do in O(n 2 ) time For all p i ≠p j, compute || p i –p j || and choose the minimum We will aim for O(n log n) time

26 26 Divide and conquer Divide: Compute the median of x-coordinates Split the points into P L and P R, each of size n/2 Conquer: compute the closest pairs for P L and P R Combine the results (the hard part) PLPL PRPR

27 27 Combine Let d=min(d 1,d 2 ) Observe: Need to check only pairs which cross the dividing line Only interested in pairs within distance < d Suffices to look at points in the 2d-width strip around the median line 2d d 1 d 2

28 28 Scanning the strip Sort all points in the strip by their y-coordinates, forming q 1 …q k, k ≤ n. Let y i be the y-coordinate of q i d min = d For i=1 to k j=i-1 While y i - y j < d If ||q i –q j ||<d then d min =||q i –q j || j:=j-1 Report d min (and the corresponding pair)

29 29 Analysis Correctness: easy Running time is more involved Can we have many q j ’s that are within distance d from q i ? No Proof by packing argument d

30 30 Analysis, ctd. Theorem: there are at most 7 q j ’s such that y i -y j ≤ d. Pf: Each such q j must lie either in the left or in the right d × d square Note that within each square, all points have distance ≥ d from others We can pack at most 4 such points into one square, so we have 8 points total (incl. q i ) qiqi d × d

31 31 Running time Divide: O(n) Combine: O(n log n) because we sort by y However, we can: Sort all points by y at the beginning Divide preserves the y-order of points Then combine takes only O(n) We get T(n)=2T(n/2)+O(n), soT(n)=O(n log n)

32 32 Finding the convex hull The convex hull of a set Q of points is the smallest convex polygon P for which each point in Q is either on the boundary of P or in its interior. Ex. CH(Q): p 0, p 1, p 3, p 10, p 12.

33 33 Graham's scan Graham's scan solves the convex-hull problem by maintaining a stack S of candidate points. Each point of the input set Q is pushed once onto the stack, and the points that are not vertices of CH(Q) are eventually popped from the stack. When the algorithm terminates, stack S contains exactly the vertices of CH(Q), in counterclockwise order of their appearance on the boundary.

34 34 GRAHAM-SCAN The procedure GRAHAM-SCAN takes as input a set Q of points, where |Q| ≥ 3. It calls the functions TOP(S), which returns the point on top of stack S without changing S, and NEXT-TO- TOP(S), which returns the point one entry below the top of stack S without changing S. As we shall prove in a moment, the stack S returned by GRAHAM-SCAN contains, from bottom to top, exactly the vertices of CH(Q) in counterclockwise order.

35 35 Pseudocode GRAHAM-SCAN(Q) 1 let p 0 be the point in Q with the minimum y-coordinate, or the leftmost such point in case of a tie 2 let 〈 p 1, p 2,..., p m 〉 be the remaining points in Q, sorted by polar angle in counterclockwise order around p 0 (if more than one point has the same angle, remove all but the one that is farthest from p 0 ) 3 PUSH(p 0, S) 4 PUSH(p 1, S) 5 PUSH(p 2, S) 6 for i ← 3 to m 7 do while the angle formed by points NEXT-TO-TOP(S), TOP(S), and p i makes a nonleft turn 8 do POP(S) 9 PUSH(p i, S) 10 return S

36 36 Illustration: sorts the remaining points of Q by polar angle relative to p 0, using the method comparing cross products

37 37 Theorem Theorem. If GRAHAM-SCAN is run on a set Q of points, where |Q| ≥ 3, then at termination, the stack S consists of, from bottom to top, exactly the vertices of CH(Q) in counterclockwise order. Pf. After line 2, we have the sequence of points 〈 p 1, p 2,..., p m 〉. Let us define, for i = 2, 3,..., m, the subset of points Q i = {p 0, p 1,..., p i }. The points in Q - Q m are those that were removed because they had the same polar angle relative to p 0 as some point in Q m ; these points are not in CH(Q), and so CH(Q m ) = CH(Q). Note that just as p 0, p 1, and p m are vertices of CH(Q), the points p 0, p 1, and p i are all vertices of CH(Q i )

38 38 Prof. At the start of each iteration of the for loop of lines 6-9, stack S consists of, from bottom to top, exactly the vertices of CH(Q i-1 ) in counterclockwise order. Initialization: The invariant holds the first time we execute line 6, since at that time, stack S consists of exactly the vertices of Q 2 = Q i-1, and this set of three vertices forms its own convex hull. Moreover, they appear in counterclockwise order from bottom to top. Maintenance: Entering an iteration of the for loop, the top point on stack S is p i-1. Let p j be the top point on S after the while loop of lines 7-8 is executed but before line 9 pushes p i, and let p k be the point just below p j on S.

39 39 Prof. Focus on this moment just before p i is pushed. Because p i 's polar angle relative to p 0 is greater than p j 's polar angle, and because the angle makes a left turn (otherwise we would have popped p j )

40 40 Pf. We now show that CH(Q j ∪ {p i }) is the same set of points as CH(Q i ). Consider any point p t that was popped during iteration i of the for loop, and let p r be the point just below p t on stack S at the time p t was popped (p r might be p j ). The angle ∠ p r p t p i makes a nonleft turn, and the polar angle of p t relative to p 0 is greater than the polar angle of p r p t must be either in the interior of the triangle formed by p 0, p r, and p i or on a side of this triangle (but it is not a vertex of the triangle). Clearly, since p t is within a triangle formed by three other points of Q i, it cannot be a vertex of CH(Q i ). Since p t is not a vertex of CH(Q i ),

41 41 Pf. CH(Q i -{p t }) = CH(Q i ), Let P i be the set of points that were popped during iteration i of the for loop. CH(Q i - P i ) = CH(Q i ). But Q i - P i = Q j ∪ {p i }, and so we conclude that CH(Q j ∪ {p i }) = CH(Q i - P i ) = CH(Q i ). We have shown that once we push p i, stack S contains exactly the vertices of CH(Q i ) in counterclockwise order from bottom to top. Incrementing i will then cause the loop invariant to hold for the next iteration. Termination: When the loop terminates, we have i = m + 1, and so the loop invariant implies that stack S consists of exactly the vertices of CH(Q m ), which is CH(Q), in counterclockwise order from bottom to top. This completes the proof.

42 42 Running time Line 1 takes Θ(n) time. Line 2 takes O(n lg n) time, using merge sort or heapsort to sort the polar angles and the cross-product method of to compare angles. (Removing all but the farthest point with the same polar angle can be done in a total of O(n) time.) Lines 3-5 take O(1) time. Because m ≤ n - 1, the for loop of lines 6-9 is executed at most n - 3 times. Since PUSH takes O(1) time, each iteration takes O(1) time exclusive of the time spent in the while loop of lines 7-8, and thus overall the for loop takes O(n) time exclusive of the nested while loop. We use aggregate analysis to show that the while loop takes O(n) time overall. For i = 0, 1,..., m, each point p i is pushed onto stack S exactly once. We observe that there is at most one POP operation for each PUSH operation. At least three points-p 0, p 1, and p m -are never popped from the stack, so that in fact at most m - 2 POP operations are performed in total. Each iteration of the while loop performs one POP, and so there are at most m - 2 iterations of the while loop altogether. Since the test in line 7 takes O(1) time, each call of POP takes O(1) time, and since m ≤ n - 1, the total time taken by the while loop is O(n). Thus, the running time of GRAHAM-SCAN is O(n lg n).

43 43 Jarvis's march Jarvis's march computes the convex hull of a set Q of points by a technique known as package wrapping (or gift wrapping). The algorithm runs in time O(nh), where h is the number of vertices of CH(Q). When h is o(lg n), Jarvis's march is asymptotically faster than Graham's scan. Jarvis's march builds a sequence H = 〈 p 0, p 1,..., p h-1 〉 of the vertices of CH(Q). We start with p 0, the next convex hull vertex p 1 has the smallest polar angle with respect to p 0. (In case of ties, we choose the point farthest from p 0.) Similarly, p 2 has the smallest polar angle with respect to p 1, and so on. When we reach the highest vertex, say p k (breaking ties by choosing the farthest such vertex), we have constructed the right chain of CH(Q). To construct the left chain, we start at p k and choose p k+1 as the point with the smallest polar angle with respect to p k, but from the negative x-axis. We continue on, forming the left chain by taking polar angles from the negative x-axis, until we come back to our original vertex p 0.

44 44 Illustration

45 45 Running time If implemented properly, Jarvis's march has a running time of O(nh). For each of the h vertices of CH(Q), we find the vertex with the minimum polar angle. Each comparison between polar angles takes O(1) time We can compute the minimum of n values in O(n) time if each comparison takes O(1) time. Thus, Jarvis's march takes O(nh) time.

46 46 Divide-and-conquer Divide: choosing a pivot point p. Partitions the input points into two sets L and R. L: containing the points to the left of p, including p itself. R: the points to the right of p. (by comparing x- coordinates.) Conquer: Recursively solve it. Combine: the hardest one.

47 47 Merge Connecting the two hulls with a line segment between the rightmost point of the hull of L with the leftmost point of the hull of R. Call these points p and q. Actually, let's add two copies of the segment pq and call them bridges. Merge: scanning around the left hull in a clockwise direction (from bridge to points in L) and around the right hull in an anti-clockwise direction (from points in R to bridge). If above turns happen, update the bridge accordingly.

48 48 Illustration of combining p q p q I II III p q

49 49 Illustration of combining p q p q IV V VI p q

50 50 Up to date Graham: O(n log n) Jarvis: O(n h) Yao: lower bound O(n log n) Kirkpatrick and Seidel: O(n lg h) 86 Timothy Chan, O(n lg h) 93

51 51 Polygon Triangulation Given a polygon, we want to decompose it into triangles by adding diagonals: new line segments between the vertices that don't cross the boundary of the polygon.

52 52 Triangulation of simple polygons Does every simple polygon admit a triangulation? Simple: it does not cross itself. If yes, what is the number of triangles? Does any triangulation lead to the same number of triangles? w v u

53 53 Theorem Theorem: Every simple polygon admits a triangulation, and any triangulation of a simple polygon with n vertices consists of exactly n-2 triangles. Pf. by induction. The base case n = 3 is trivial: there is only one triangulation of a triangle, and it obviously has only one triangle. Let P be a polygon with n edges. Draw a diagonal between two vertices. This splits P into two smaller polygons. One of these polygons has k edges of P plus the diagonal, by the induction hypothesis, this polygon can be broken into k - 1 triangles. The other polygon has n - k + 1 edges, and so by the induction hypothesis, it can be broken into n - k -1 tirangles. Putting the two pieces back together, we have a total of (k - 1) + (n - k - 1) = n - 2 triangles.

54 54 Exists of diagonal How do we know that every polygon has a diagonal? [Meisters in 1975] Lemma. Every polygon with more than three vertices has a diagonal. Pf. Let P be a polygon with more than three vertices. Let q be the leftmost vertex. Let p and r be two neighboring vertices of q. Case 1: pr completely in P. Then segement pr is a diagonal r q p

55 55 Pf of Lemma Case 2: pr not completely in P r q p s Let s be the vertex furthest away from the segment pr. Then the line qs is a diagnose.

56 56 Running time O( n 2 ) Time.

57 57 Applications of Polygon Triangulation Motivation: Guarding art galleries Art gallery theorem for simple polygons Partitioning of polygons into monotone pieces Triangulation of y-monotone polygons

58 58 Guarding art galleries “Art Gallery“ Problem

59 59 Guarding art galleries The gallery is represented by a simple polygon A guard is represented by a point within the polygon Guards have a viewport of 360° A polygon is completely guarded, if every point within the polygon is guarded by at least one of the watchmen

60 60 Demo Visibility polygon: The visibility polygon vis(p) of a polygon P is defined by the set of all points that are visible from the base point p. Demo

61 61 Guarding art galleries Even if two polygons have the same number of vertices, one may be easier to guard than the other. We will consider the lower bound for the worst case: We want to determine the number of guards that suffice for an arbitrary polygon with n vertices.

62 62 Equitable Convex Partition and Applications Armbruster, Carlsson, Ge, and Ye

63 63 Problem Statement n points are scattered inside a convex polygon P (in 2D) with m vertices. Does there exist a partition of P into n sub-regions satisfying the following: Each sub-region is a convex polygon Each sub-region contains one point All sub-regions have equal area

64 64 Motivation: Client/Server Network This problem has applications in heuristic methods for what we call the Broadcast Network class of problems, in which we connect a set of clients to a set of servers, using a fixed underlying network topology. Example: Multi-Depot Vehicle Routing Problem (MDVRP). Definition: A set of vehicles located at depots in the plane must visit a set of customers such that the maximum TSP cost is minimized (min-max MDVRP).

65 65 Result Not only such an equitable partition always exists, but also we can find it exactly in running time O(Nn log N), where N = m + n.


Download ppt "1 Computational Geometry Instructor: yedeshi"

Similar presentations


Ads by Google