Computational Geometry 2D Convex Hulls Joseph S. B. Mitchell Stony Brook University Chapter 2: Devadoss-O Rourke
Convexity p p Set X is convex if p,q X pq X q q convex non-convex Point p X is an extreme point if there exists a line (hyperplane) through p such that all other points of X lie strictly to one side Extreme points in red q r p 2
Convex Hull 3
Convex Hull, Extreme Points 4
Convex Hull: Equivalent Def Convex combination of points: 5
Convex Hull: Equivalent Defs 6
Equivalent Definitions of Convex Hull, CH(X) {all convex combinations of d+1 points of X } [Caratheodory s Thm] (in any dimension d) T X H X T, T convex H, Hhalfspace Set-theoretic smallest convex set containing X. In 2D: min-area (or min-perimeter) enclosing convex body containing X In 2D: abc a, b, c X Devadoss-O Rourke Def 7
Convex Hull in 2D Fact: If X=S is a finite set of points in 2D, then CH(X) is a convex polygon whose vertices (extreme points) are points of S. 8
Fundamental Algorithmic Problem: 2D Convex Hulls Input: n points S = (p 1, p 2,, p n ) p 7 p 2 p 4 More generally: CH(polygons) p 8 p 3 p 5 p 1 p 6 p 9 Output: (9,6,4,2,7,8,5) Output: A boundary representation, e.g., ordered list of vertices (extreme points), of the convex hull, CH(S), of S (convex polygon) 9
Convex Hull Problem in 2D 10
Computational Complexity Function T(n) is O(f(n)) if there exists a constant C such that, for sufficiently large n, T(n) < C f(n) 11
Comparing O(n), O(n log n), O(n 2 ) n n log n n² 2 10 10³ 10 2 10 10 4 2 20 10 6 2 20 10 6 20 2 20 2 10 7 2 40 10 12 Function T(n) is O(f(n)) if there exists a constant C such that, for sufficiently large n, T(n) < C f(n) Interactive Processing n log n algorithms n² algorithms n = 1000 yes? n = 1000000? no 12
2D Convex Hull Algorithms O(n 4 ) simple, brute force (but finite!) O(n 3 ) still simple, brute force O(n 2 ) incremental algorithm [DO] Section 2.2 O(nh) simple, output-sensitive h = output size (# vertices) O(n log n) worst-case optimal (as fcn of n) O(n log h) ultimate time bound (as fcn of n,h) Randomized, expected O(n log n) 13
Lower Bound Lower bound: (n log n) [DO] Section 2.5 From SORTING: y= x 2 (x i,x i 2 ) Note: Even if the output of CH is not required to be an ordered list of vertices (e.g., just the # of vertices), (n log n) holds x i As function of n and h: (n log h) holds 14
[DO] Section 2.5 Lower Bound computing the ordered convex hull of n points in the plane. 15
[DO] Section 2.5 Lower Bound computing the ordered convex hull of n points in the plane. An even stronger statement is true: 16
Primitive Computation Left tests: sign of a cross product (determinant), which determines the orientation of 3 points Time O(1) ( constant ) Left( a, b, c ) = TRUE ab ac > 0 a c b c is left of ab 17
SuperStupidCH: O(n 4 ) Fact: If s pqr, then s is not a vertex of CH(S) q p q p r p,q O(n 3 ) s p,q,r: If s pqr then mark s as NON-vertex s p r O(n) Output: Vertices of CH(S) Can sort (O(n log n ) to get ordered 18
StupidCH: O(n 3 ) Fact: If all points of S lie strictly to one side of the line pq or lie in between p and q, then pq is an edge of CH(S). p q p O(n 2 ) r p,q: If r red then mark pq as NON-edge (ccw) Output: Edges of CH(S) O(n) Can sort (O(n log n ) to get ordered 19 Caution!! Numerical errors require care to avoid crash/infinite loop! q r p Applet by Snoeyink
Incremental Algorithm [DO] Section 2.2 20
Incremental Algorithm Sort points by x-coordinate O(n log n) Build CH(X), adding pts left to right 21
Incremental Algorithm 22
Incremental Algorithm 23
O(nh) : Gift-Wrapping Idea: Use one edge to help find the next edge. Jarvis March r O(n) per step h steps q Total: O(nh) Output: Vertices of CH(S) Demo applet of Jarvis march p Key observation: Output-sensitive! 24
[DO] Section 2.4 Gift-Wrapping 25
[DO] Section 2.4 Gift-Wrapping 26
O(n log n) : Graham Scan Idea: Sorting helps! Start with v lowest (min-y), a known vertex O(n) Sort S by angle about v lowest Graham scan: O(n log n) O(n) Maintain a stack representing (left-turning) CH so far If p i is left of last edge of stack, then PUSH Else, POP stack until it is left, charging work to popped points Demo applet v lowest CH so far 28
[DO] Section 2.4 Graham Scan Red points form right turn, so are discarded 29
[DO] Section 2.4 Graham Scan 30
Sorted points: Graham Scan [O Rourke, Chapter 3] 31
[O Rourke, Chapter 3] 32
[O Rourke, Chapter 3] Stack History 33
O(n log n) : Divide and Conquer Split S into S left and S right, sizes n/2 Recursively compute CH(S left ), CH(S right ) Merge the two hulls by finding upper/lower bridges in O(n), by wobbly stick Time O(n) Time 2T(n/2) Time O(n) S left S right Time: T(n) 2T(n/2) + O(n) T(n) = O(n log n) Demo applet 34
Divide and Conquer [DO] Section 2.6 35
Divide and Conquer [DO] Section 2.6 36
Divide and Conquer [DO] Section 2.6 37
QuickHull QuickHull(a,b,S) to compute upperhull(s) If S=, return () Else return (QuickHull(a,c,A), c, QuickHull(c,b,B)) c c = point furthest from ab Discard points in abc Worst-case: O(n 2 ) Avg: O(n) Works well in higher dimensions too! Qhull website a B A S Qhull, Brad Barber (used within MATLAB) b 38
QuickHull Applet (programmed by Jeff So) Applet (by Lambert) When is it bad? (see hw2) If no points are discarded (in abc), at each iteration, and The balance may be very lop-sided (between sets A and B; in fact, one set could be empty, at every iteration!) 39
QuickHull [David Mount lecture notes, Lecture 3] 40
O(n log n) : Incremental Construction Add points in the order given: v 1,v 2,,v n Maintain current Q i = CH(v 1,v 2,,v i ) Add v i+1 : If v i+1 Q i, do nothing Else insert v i+1 by finding tangencies, updating the hull Point location test: O(log n) AMS 545 / CSE 555 Binary search: O(log n) Worst-case cost of insertion: O(log n) But, uses complex data structures 41
O(n log n) : Randomized Incremental Add points in random order Keep current Q i = CH(v 1,v 2,,v i ) AMS 545 / CSE 555 Add v i+1 : If v i+1 Q i, do nothing Else insert v i+1 by finding tangencies, updating the hull Expected cost of insertion: O(log n) 42
Each uninserted v j Q i (j>i+1) points to the bucket (cone) containing it; each cone points to the list of uninserted v j Q i within it (with its conflict edge) Add v i+1 Q i : Start from conflict edge e, and walk cw/ccw to establish new tangencies from v i+1, charging walk AMS to deleted vertices 545 / CSE 555 Rebucket points in affected cones (update lists for each bucket) Total work: O(n) + rebucketing work e E(rebucket cost for v j at step i) = O(1) P(rebucket) O(1) (2/i ) = O(1/i ) v i+1 v j E(total rebucket cost for v j ) = O(1/i ) = O(log n) Total expected work = O(n log n) Backwards Analysis: v j was just rebucketed iff the last point inserted was one of the 2 endpoints of the (current) 43 conflict edge, e, for v j
Output-Sensitive: O(n log h) The ultimate convex hull (in 2D) Marriage before Conquest : O(n log h) Lower bound (n log h) AMS [Kirkpatrick 545 & Seidel 86] / CSE 555 So, O(n log h) is BEST Simpler [Chan 95] 2 Ideas: h = output size POSSIBLE, as function of n and h!! Break point set S into groups of appropriate size m (ideally, m = h) Search for the right value of m ( =h, which we do not know in advance) by repeated squaring 44
Chan s Algorithm Break S into n/m groups, each of size m Find CH of each group (using, e.g., Graham scan): O(m log m) per group, so total O((n/m) m log m) = O(n log m) AMS 545 / CSE 555 At each gift-wrap step, when pivoting around vertex v Gift-wrap the n/m hulls to get overall CH: find the tangency point (binary search, O(log m)) to each group CH pick the smallest angle among the n/m tangencies: O( h (n/m) log m) Hope: m=h Try m = 4, 16, 256, 65536, = O( h (n/h) log h) = O(n log h) = O(n log h) O(n ( log (2 21 ) + log (2 22 ) + log (2 23 ) + log (2 2 log (log (h)) ) = O(n (2 1 + 2 2 + 2 3 + + 2 log (log (h)) )) = O(n (2 log h)) = O(n log h) v v 45
CH of Simple Chains/Polygons Input: ordered list of points that form a simple chain/cycle Importance of simplicity (noncrossing, Jordan curve) as a means of sorting (vs. sorting in x, y) Melkman s Algorithm: O(n) (vs. O(n log n) or O(n log h)) 1 2 n 46
Melkman s Algorithm Keep hull in DEQUE: < v b,v b+1,,v t-1,v t =v b > While Left(v b,v b+1,v i ) and Left(v t-1,v t,v i ) i i+1 Remove v b+1, v b+2, and v t-1, v t-2, v t until convexity restored v v t-1 b v t-1 New v b =v t = v i v t v i v i v b+1 v b Claim: Simplicity assures that the only way for the chain to exit the current hull is via pockets v b v b+1 or v t-1 v t. Time: O(n), since O(1) per insertion v b+1 Simplicity helps! 47
Melkman s Algorithm 48
Melkman s Algorithm 49
Melkman s Algorithm 50
Expected Size of CH 51
Expected Size, h, of CH Claim: For n points uniform in a square, the expected number of maximal points is O(log n) Corollary: E(h) = O(log n) (since CH vertices are subset of maximal points) Thus, Gift-Wrap/Jarvis runs in expected O(n log n), if points are uniform in a box, etc. 52
CH, Polytopes in Higher Dimensions 53
CH, Polytopes in Higher Dimensions Representing the structure of a simplex or polytope: Incidence Graph Later: for planar subdivisions (e.g., boundary of 3-dim polyhedra), we have various data structures: winged-edge, doubly-connected edge list (DCEL), quad-edge, etc. [David Mount lecture notes, Lecture 5] 54
Convex Hull in 3D 55
Convex Hull in 3D 56
CH in Higher Dimensions 3D: Divide and conquer: T(n) 2T(n/2) + O(n) O(n log n) merge Output-sensitive: O(n log h) [Chan] Higher dimensions: (d 4) applet h= O(n) O(n d/2 ), which is worst-case OPT, since point sets exist with h= (n d/2 ) Output-sensitive: O((n+h) log d-2 h), for d=4,5 Qhull website 57
Convex Hull in 3D 58
Convex Hull in 3D 59
More Demos Various 2D and 3D algorithms in an applet Applets of algorithms in O Rourke s book Applets from Jack Snoeyink 60
Review: Convex Hull Algorithms O(n 4 ), O(n 3 ), naïve O(n 2 ) incremental algorithm O(nh) simple, output-sensitive O(n log n) Graham scan, divide-and-conquer, incremental (with fancy data structures); worst-case optimal (as fcn of n) O(n log h) ultimate time bound (as fcn of n,h) Randomized, expected O(n log n) (simple) 3D: Divide and conquer: O(n log n) Output-sensitive: O(n log h) [Chan] Higher dimensions: (d 4) O(n d/2 ), which is worst-case OPT, Output-sensitive: O((n+h) log d-2 h), for d=4,5 61