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