Convex hull: basic facts Convex hull: basic facts CG Lecture 1 CG Lecture 1 Problem : give a set of n points P in the plane, compute Convex Hull Algorithms its convex hull CH ( P ). Basic facts : • 2D • CH ( P ) is a convex polygon with complexity O ( n ). • Basic facts • Vertices of CH ( P ) are a subset of the input points P. • Algorithms: Naïve, Gift wrapping, Graham scan, Quick hull, p 9 p 3 Divide-and-conquer • Lower bound p 8 p 4 p 13 p 1 • 3D p 7 p 5 p 10 • Basic facts p 6 Input: Input: p 1 ,…, p 13 p 2 p 12 • Algorithms: Gift wrapping, Divide and conquer, incremental CH vertices: p 1 ,p 2 ,p 11 ,p 12 ,p 13 ,p 9 ,p 3 CH vertices: • Convex hulls in higher dimensions p 11 1 2 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Naive algorithm Triangle Area Naive algorithm Triangle Area Algorithm 2 ⋅ Area = ( P − P ) ( × P − P ) • For each pair of points construct its 2 1 3 1 connecting segment and supporting line . = − ⋅ − α P P P P sin 2 1 3 1 • Find all the segments whose supporting Yes Yes P 3 (x 3 ,y 3 ) − − x x y y lines divide the plane into two halves, = 2 1 2 1 − − x x y y such that one half plane contains all the 3 1 3 1 α other points. x y 1 1 1 P 1 (x 1 ,y 1 ) P 2 (x 2 ,y 2 ) • Construct the convex hull out of these = x y 1 No No 2 2 segments. x y 1 3 3 Time complexity The determinant is twice the area of the triangle • All pairs: ⎛ ⎞ n − n n ( 1) = = O ( ) O ( ) O n ( 2 ) ⎜ ⎟ 2 whose vertices are the rows of the matrix. ⎝ ⎠ 2 • Check all points for each pair: O ( n ) each, O ( n 3 ) total. 3 4 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Triangle Area Triangle Area Orientation and point classification Orientation and point classification ( x 1 , y 1 ) x y 1 ⋅ = − × − 2 Area ( P P ) ( P P ) 1 1 1 2 1 3 1 = Area x y 1 = − ⋅ − α P P P P sin 2 2 2 + 2 1 3 1 x y 1 P 3 (x 3 ,y 3 ) − − 3 3 x x y y ( x 2 , y 2 ) = 2 1 2 1 ( x 3 , y 3 ) − − x x y y 3 1 3 1 • The area sign indicates the orientation of the points. α x y 1 • Positive area ≡ counterclockwise orientation ≡ left turn. 1 1 P 1 (x 1 ,y 1 ) P 2 (x 2 ,y 2 ) = x y 1 2 2 • Negative area ≡ clockwise orientation ≡ right turn. x y 1 3 3 • This test can be used to determine whether a given point The determinant is twice the area of the triangle is “above” or “below” a given line whose vertices are the rows of the matrix. 5 6 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 1
Possible problems Possible problems Orientation and point classification Orientation and point classification • Degenerate cases, e.g., 3 collinear ( x 1 , y 1 ) x y 1 1 1 1 points, may harm the correctness of = Area x y 1 2 2 the algorithm. Segments AB , BC and 2 + A x y 1 AC will all be included in the 3 3 ( x 2 , y 2 ) ( x 3 , y 3 ) B convex hull. • The area sign indicates the orientation of the points. C • Positive area ≡ counterclockwise orientation ≡ left turn. • Negative area ≡ clockwise orientation ≡ right turn. • Numerical problems – We might conclude that none of the three segments (or a wrong • This test can be used to determine whether a given point pair of them) belongs to the convex hull. is “above” or “below” a given line • Question: How is colinearity detected? 7 8 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 General position assumption General position assumption Gift wrapping algorithm Gift wrapping algorithm • When designing a geometric algorithm, we first p 3 Algorithm: make some simplifying assumptions, e.g.: p 8 p 13 1. Find the lowest point p 1 and its hull edge e p 4 p 1 • No three collinear points; 2. For each remaining point p i ( i > 2) do p 7 p 5 p 10 • Compute the CCW angle α i from the • No two points with the same x or y coordinate; p 6 p 2 p 2 previous hull edge • Other configurations: no three points on a circle, … • Let p j be the point with the smallest α i p 1 • Later, we consider the general case: • Make ( p 1 p i ) the new hull edge • Behavior of algorithm to degenerate cases? Rotate counterclockwise a line through p 1 until it touches one of the other points • Will the correctness be preserved? Time complexity: O ( n 2 ) • Will the running time remain the same? In fact, the complexity is O( nh ), • Modify/extend algorithm to handle degeneracies where n is the input size and h is the hull size. 9 10 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Line equation and angle Line equation and angle Graham Graham’ ’s scan algorithm s scan algorithm Algorithm: • Let ( x 1 ,y 1 ) and ( x 2 ,y 2 ) be two points. • Find a point p 0 in the interior of the hull. • The explicit line equation is: • Compute the CCW angle α i from p 0 to all p 0 = + y mx c other points. • Sort the points by angle α i. − − y y y x y x y = mx+c y = mx+c = + y 2 1 x 1 2 2 1 • Construct the boundary by scanning the − − x x x x m = tan m = tan θ θ 2 1 2 1 points in the sorted order and performing − y y only “right turns” (trim off “left turns”). θ = tan 2 1 c c − x x p 0 2 1 p 0 Use a stack to process sorted points Time Complexity : O ( n log n ) • Singularity at x 1 = x 2 (vertical line) Right turn Left turn Right turn Left turn Question: How do we check for a right or left turn? 11 12 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 2
Graham’ Graham ’s s scan: complexity analysis Quick hull algorithm Quick hull algorithm scan: complexity analysis a Algorithm : • Sorting – O( n log n ) • Find four extreme points of P: highest a , lowest c b , leftmost c , rightmost d. d • D i = number of points popped on processing p i , • Discard all points in the quadrilateral interior n n • Find the hulls of the four triangular regions ∑ ∑ = + = + time ( D 1) n D exterior to the quadrilateral. i i b = = i 1 i 1 • To process triangular regions, find the extreme • Each point is pushed on the stack only once. point in linear time. Two new exterior regions A, B will be formed, each processed separately. • Once a point is popped – it cannot be popped again. • Recurse until no points are left in the exterior: • Hence the convex hull is the union of the exteriors. n ∑ ≤ D n extreme point c i Time Complexity: |B|= |B|= β = i 1 |A|= α |A|= α T ( n ) = O ( n )+ T ( α )+ T ( β ) where α + β =n– 1 a = T ( n –1) + O ( n ) = O ( n 2 ) worst case! 13 14 b Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Divide- -and and- -Conquer Conquer Finding tangents (1) Divide Finding tangents (1) Algorithm: • Two disjoint convex polygons have four tangents median median that classify them as either being entirely to the • Find a point with a median x coordinate in left (+) or to the right ( − ) of the line: O ( n ) time tangents 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 left hull right hull right hull Time Complexity: O ( n log n ) ( − ( − , , − − ) ) ⎛ n ⎞ = ⎜ ⎟ + T ( n ) 2 T O ( n ) ⎝ 2 ⎠ 15 16 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Finding tangents (2) Finding tangents (2) Lower bound for convex hull in 2D Lower bound for convex hull in 2D Lower tangent: Claim: Convex hull computation Connect rightmost 1: (- 1: ( -,+) ,+) takes Θ ( n log n ) point of left hull to 2: (- -,*) ,*) 2: ( leftmost point in Proof: reduction from Sorting to right hull and Convex Hull: 5: (*,- -) ) 5: (*, 3: (*,*) 3: (*,*) “walk” around both • Given n real values x i , generate n boundaries until the points on the graph of a convex lower tangent is function, e.g. ( x i ,x i 2 ). reached. • Compute the (ordered) convex Complexity(CH)= Ω ( n log n ) Complexity : O ( n ) . hull of the points. Since there is a O( n log n )-time algorithm, • The order of the convex hull Complexity(CH)= Θ ( n log n ) points is the order of the x i . 17 18 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 Leo Joskowicz, Spring 2005 3
Recommend
More recommend