Convex Hull Algorithms
Total Page:16
File Type:pdf, Size:1020Kb
CG Lecture 1 Convex Hull Algorithms •2D • Basic facts • Algorithms: Naïve, Gift wrapping, Graham scan, Quick hull, Divide-and-conquer • Lower bound •3D • Basic facts • Algorithms: Gift wrapping, Divide and conquer, incremental • Convex hulls in higher dimensions 1 Leo Joskowicz, Spring 2005 Convex hull: basic facts Problem: give a set of n points P in the plane, compute its convex hull CH(P). Basic facts: • CH(P) is a convex polygon with complexity O(n). • Vertices of CH(P) are a subset of the input points P. p p3 9 p8 p4 p p1 13 p7 p5 p10 p p2 6 p12 Input: p1,…, p13 CH vertices: p1,p2,p11,p12,p13,p9,p3 p11 2 Leo Joskowicz, Spring 2005 1 Naive algorithm Algorithm • For each pair of points construct its connecting segment and supporting line. • Find all the segments whose supporting Yes lines divide the plane into two halves, such that one half plane contains all the other points. • Construct the convex hull out of these segments. No Time complexity • All pairs: ⎛⎞n nn(1)− 2 OO()⎜⎟== ( ) On () ⎝⎠2 2 • Check all points for each pair: O(n) 3 3 Leo Joskowicz, Spring 2005each, O(n ) total. Triangle Area 2()()⋅=−×−Area P21 P P 31 P =−⋅−PPPP21 31sinα P (x ,y ) xxyy−− 3 3 3 = 21 21 xxyy31−− 31 xy1 α 11 P (x ,y ) 1 1 1 P2(x2,y2) = xy221 xy331 The determinant is twice the area of the triangle whose vertices are the rows of the matrix. 4 Leo Joskowicz, Spring 2005 2 Orientation and point classification xy1 (x1,y1) 1 11 Area = xy221 2 + xy331 (x2,y2) (x3,y3) • The area sign indicates the orientation of the points. • Positive area ≡ counterclockwise orientation ≡ left turn. • Negative area ≡ clockwise orientation ≡ right turn. • This test can be used to determine whether a given point is “above” or “below” a given line 5 Leo Joskowicz, Spring 2005 Triangle Area 2()()⋅=−×−Area P21 P P 31 P =−⋅−PPPP21 31sinα P (x ,y ) xxyy−− 3 3 3 = 21 21 xxyy31−− 31 xy1 α 11 P (x ,y ) 1 1 1 P2(x2,y2) = xy221 xy331 The determinant is twice the area of the triangle whose vertices are the rows of the matrix. 6 Leo Joskowicz, Spring 2005 3 Orientation and point classification xy1 (x1,y1) 1 11 Area = xy221 2 + xy331 (x2,y2) (x3,y3) • The area sign indicates the orientation of the points. • Positive area ≡ counterclockwise orientation ≡ left turn. • Negative area ≡ clockwise orientation ≡ right turn. • This test can be used to determine whether a given point is “above” or “below” a given line 7 Leo Joskowicz, Spring 2005 Possible problems • Degenerate cases, e.g., 3 collinear points, may harm the correctness of the algorithm. Segments AB, BC and A AC will all be included in the convex hull. B C • Numerical problems – We might conclude that none of the three segments (or a wrong pair of them) belongs to the convex hull. • Question: How is colinearity detected? 8 Leo Joskowicz, Spring 2005 4 General position assumption • When designing a geometric algorithm, we first make some simplifying assumptions, e.g.: • No three collinear points; • No two points with the same x or y coordinate; • Other configurations: no three points on a circle, … • Later, we consider the general case: • Behavior of algorithm to degenerate cases? • Will the correctness be preserved? • Will the running time remain the same? • Modify/extend algorithm to handle degeneracies 9 Leo Joskowicz, Spring 2005 Gift wrapping algorithm p Algorithm: 3 p8 p13 1. Find the lowest point p1 and its hull edge e p4 p1 2. For each remaining point pi (i > 2) do p7 p5 p10 • Compute the CCW angle αi from the p p6 p previous hull edge 2 2 •Let pj be the point with the smallest αi p1 • Make (p1 pi) the new hull edge Rotate counterclockwise a line through p1 until it touches one of the other points Time complexity: O(n2) In fact, the complexity is O(nh), where n is the input size and h is the hull size. 10 Leo Joskowicz, Spring 2005 5 Line equation and angle •Let (x1,y1) and (x2,y2) be two points. • The explicit line equation is: y = mx + c y − y y x − y x y = mx+c y = 2 1 x + 1 2 2 1 x2 − x1 x2 − x1 m = tan θ y2 − y1 tanθ = c x2 − x1 • Singularity at x1= x2 (vertical line) 11 Leo Joskowicz, Spring 2005 Graham’s scan algorithm Algorithm: • Find a point p0 in the interior of the hull. • Compute the CCW angle α from p to all i 0 p0 other points. • Sort the points by angle αi. • Construct the boundary by scanning the points in the sorted order and performing only “right turns” (trim off “left turns”). p p0 Use a stack to process sorted points 0 Time Complexity: O(n log n) Right turn Left turn Question: How do we check for a right or left turn? 12 Leo Joskowicz, Spring 2005 6 Graham’s scan: complexity analysis • Sorting – O(n log n) • Di = number of points popped on processing pi, nn time=+=+∑ (DnDii 1) ∑ ii==11 • Each point is pushed on the stack only once. • Once a point is popped – it cannot be popped again. • Hence n ∑ Dni ≤ i=1 13 Leo Joskowicz, Spring 2005 Quick hull algorithm a Algorithm: • Find four extreme points of P: highest a, lowest c b, leftmost c, rightmost d. d • Discard all points in the quadrilateral interior • Find the hulls of the four triangular regions exterior to the quadrilateral. b • To process triangular regions, find the extreme point in linear time. Two new exterior regions A, B will be formed, each processed separately. • Recurse until no points are left in the exterior: the convex hull is the union of the exteriors. extreme point c Time Complexity: |B|= β T(n) = O(n)+T(α)+T(β) where α+β=n–1 |A|= α a = T(n–1) + O(n) = O(n2) worst case! 14 Leo Joskowicz, Spring 2005 b 7 Divide-and-Conquer Algorithm: median • Find a point with a median x coordinate in O(n) time tangents • Partition point set in two halves • Compute the convex hull of each half (recursive execution) • Combine the two convex hulls by finding their upper and lower tangents in O(n). left hull right hull Time Complexity: O(n log n) ⎛ n ⎞ T (n) = 2T ⎜ ⎟ + O(n) ⎝ 2 ⎠ 15 Leo Joskowicz, Spring 2005 Finding tangents (1) • Two disjoint convex polygons have four tangents that classify them as either being entirely to the left (+) or to the right (−) of the line: (+,+) (+,−) (−,+) (−,−) 16 Leo Joskowicz, Spring 2005 8 Finding tangents (2) Lower tangent: Connect rightmost 1: (-,+) point of left hull to 2: (-,*) leftmost point in right hull and “walk” around both 3: (*,*)5: (*,-) boundaries until the lower tangent is reached. Complexity: O(n). 17 Leo Joskowicz, Spring 2005 Lower bound for convex hull in 2D Claim: Convex hull computation takes Θ(n log n) Proof: reduction from Sorting to Convex Hull: •Given n real values xi, generate n points on the graph of a convex 2 function, e.g. (xi,xi ). • Compute the (ordered) convex Complexity(CH)=Ω(n log n) hull of the points. Since there is a O(n log n)-time • The order of the convex hull algorithm, Complexity(CH)= Θ(n log n) points is the order of the xi. 18 Leo Joskowicz, Spring 2005 9 Convex hulls in 3D z y Output: Convex hull x Input: Points in 3D Representation: Planar subdivision 19 Leo Joskowicz, Spring 2005 Convex hull in 3D: properties Theorem: A convex polyhedron with |V| = n vertices hast at most |E| = 3n–6 edges and |F| = 2n–4 faces. Proof: from Euler’s formula for planar graphs: |V| – |E| + |F| = 2 Every face has at least 3 edges (triangle) Every edge is incident to at least 2 faces Æ 3|F| ≤ 2|E| Æ |E| ≤ 3n–6 and |F| ≤ 2n–4 • The complexity of the convex hull is O(n). • The equality holds when all faces are triangles. • When no three points are on a line and no four on a plane, the faces of the convex hull are all triangles. 20 Leo Joskowicz, Spring 2005 10 Gift wrapping in 3D (1) Idea: generalize the 2D procedure. The wrapping element is a plane instead of a line. Pivot a plane around the a edges of the hull; find the smallest angle of the b planes Πi containing segment ab and points pi 21 Leo Joskowicz, Spring 2005 Gift wrapping in 3D (2) • Form a triangular face containing a,b,c and repeat the operation for edges bc and ac. c c b b a a • Start from lowest edge in the convex hull and “work around and upwards” until the wrap is over. e d f b d c b c a 22 Leo Joskowicz, Spring 2005 a 11 Gift wrapping in 3D (3) Algorithm sketch Maintain a queue of facets and examine their edges in turn, computing for each the smallest angle with it. Complexity • O(n) operations are required at each edge to find the minimum angle. • Each edge is visited at most once. • Since there are O(n) edges, the complexity is O(n2). • In fact, it is O(n|F|), where |F| is the number of facets in the final hull (as in 2D case). 23 Leo Joskowicz, Spring 2005 Divide-and-Conquer in 3D Idea: generalize the 2D procedure. Recursively split B the point set into two disjoint sets, compute their hulls and merge them in linear time.