Computational Geometry Computational Geometry
2D Convex Hulls 2D Convex Hulls
Joseph S. B. Mitchell Stony Brook University
Chapter 2: Devadoss-O’Rourke
Convexity Convexity
Set Set XX is is convexconvex if if p,qp,qX X pq pq X X
Point Point pp X X is an is an extreme pointextreme point if there exists if there exists a line (hyperplane) through
a line (hyperplane) through pp such that all such that all other points of
other points of X X lie strictly to one sidelie strictly to one side
q p Extreme points in red r
p q
non-convex q
p convex
Convex Hull Convex Hull
33
Convex Hull, Extreme Points
Convex Hull, Extreme Points
Convex Hull: Equivalent Def Convex Hull: Equivalent Def
Convex combination of points: Convex combination of points:
55
Convex Hull: Equivalent Defs
Convex Hull: Equivalent Defs
Equivalent Definitions of Equivalent Definitions of
Convex Hull, CH(
Convex Hull, CH( X X ) )
{all convex combinations of {all convex combinations of d+1 d+1 points of points of X X } }
[Caratheodory’s Thm]
[Caratheodory’s Thm] (in any dimension (in any dimension dd))
Set-theoretic “smallest” convex set containing Set-theoretic “smallest” convex set containing XX. .
In 2D: min-area (or min-perimeter) enclosing In 2D: min-area (or min-perimeter) enclosing convex body containing
convex body containing X X
In 2D: In 2D:
77
halfspace
H X H
H
,
c X
b a
abc
, ,
T convex
X T
T
, Devadoss-O’Rourke Def
Convex Hull in 2D Convex Hull in 2D
FactFact: If : If X=SX=S is a finite set of points in 2D, is a finite set of points in 2D, then CH(
then CH(XX) is a convex polygon whose vertices ) is a convex polygon whose vertices (extreme points) are points of
(extreme points) are points of SS..
Input Input : : n n points points S = S = ( ( p p
1, p p
22, …, p , …, p
nn) )
OutputOutput: A boundary representation, e.g., : A boundary representation, e.g., ordered list of
ordered list of verticesvertices (extreme points), of (extreme points), of the convex hull, CH(
the convex hull, CH(SS), of ), of SS (convex polygon) (convex polygon)
Fundamental Algorithmic Fundamental Algorithmic Problem: 2D Convex Hulls Problem: 2D Convex Hulls
99
More generally:
CH(polygons) p4
p1 p3
p2
p6 p5
p8 p7
p9
Output: (9,6,4,2,7,8,5)
Convex Hull Problem in 2D
Convex Hull Problem in 2D
Computational Complexity Computational Complexity
1111
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 Comparing O(n), O(n log n), O(n
22) )
n n log n n² 210 10³ 10 • 210 104 220 106 220 106 20 • 220 2 • 107 240 1012
Interactive
Processing n log n algorithms n² algorithms n = 1000 yes ?
n = 1000000 ? 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 2D Convex Hull Algorithms
O(O(nn44) simple, brute force (but finite!) ) simple, brute force (but finite!)
O(O(nn33) still simple, brute force) still simple, brute force
O(O(nn22) incremental algorithm) incremental algorithm
O(O(nhnh) simple, “output-sensitive”) simple, “output-sensitive”
• hh = output size (# vertices) = output size (# vertices)
O(O(n log nn log n) worst-case optimal (as fcn of ) worst-case optimal (as fcn of nn))
O(O(n log hn log h) “ultimate” time bound ) “ultimate” time bound (as fcn of (as fcn of n,hn,h))
Randomized, expected O(Randomized, expected O(n log nn log n))
1313
[DO] Section 2.2
Lower Bound Lower Bound
Lower bound: Lower bound: ((n log nn log n))
From SORTING:
y= x2
xi
(xi,xi2 )
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
Lower Bound Lower Bound
1515
[DO] Section 2.5
computing the ordered convex hull of n points in the plane.
Lower Bound Lower Bound
[DO] Section 2.5
computing the ordered convex hull of n points in the plane.
An even stronger statement is true:
Primitive Computation Primitive Computation
• ““Left” tests: sign of a cross product Left” tests: sign of a cross product (determinant), which determines the (determinant), which determines the
orientation of 3 points orientation of 3 points
• Time O(1) (“constant”)Time O(1) (“constant”)
1717
a c b Left( a, b, c ) = TRUE ab ac > 0
c is left of ab
SuperStupidCH:
SuperStupidCH: O(n O(n
44) )
FactFact: If : If ss ∆∆pqrpqr, then , then ss is not a vertex of is not a vertex of CH(CH(SS))
pp
q q p p
r r p,q p,q
s s p,q,r: p,q,r: If If s s ∆pqr ∆pqr then mark then mark ss as as NON-vertex
NON-vertex
OutputOutput: Vertices of CH(: Vertices of CH(SS))
Can sort (Can sort (O(n log n) O(n log n) ) to get ordered) to get ordered O(n3)
O(n)
p s
q
r
StupidCH:
StupidCH: O(n O(n
33) )
FactFact: If all points of : If all points of SS lie strictly to one lie strictly to one side of the line
side of the line pqpq or lie in between or lie in between pp and and q, q, then
then pqpq is an edge of CH( is an edge of CH(SS).).
pp
q q p p
r r p,q: p,q: If If r r redred then mark then mark pqpq as as NON-edge (ccw)
NON-edge (ccw)
OutputOutput: Edges of CH(: Edges of CH(SS))
Can sort (Can sort (O(n log n) O(n log n) ) to get ordered) to get ordered
1919
O(n2)
O(n)
p r q
Caution!! Numerical errors require care to avoid crash/infinite loop!
Applet by Snoeyink
Incremental Algorithm Incremental Algorithm
[DO] Section 2.2
Incremental Algorithm 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
2121
Incremental Algorithm
Incremental Algorithm
Incremental Algorithm Incremental Algorithm
2323
O(nh)
O(nh) : Gift-Wrapping : Gift-Wrapping
IdeaIdea: Use one edge to help find the next edge.: Use one edge to help find the next edge.
OutputOutput: Vertices of CH(: Vertices of CH(SS))
Demo applet Demo applet of Jarvis marchof Jarvis marchp
q r
Jarvis March
Key observation:
O(n) per step h steps
Total: O(nh)
Gift-Wrapping Gift-Wrapping
2525
[DO] Section 2.4
Gift-Wrapping Gift-Wrapping
[DO] Section 2.4
O(n log n)
O(n log n) : Graham Scan : Graham Scan
IdeaIdea: Sorting helps!: Sorting helps!
Start with Start with vvlowest lowest (min-(min-yy), a known vertex), a known vertex
Sort Sort SS by angle about by angle about vvlowestlowest
Graham scan:Graham scan:
• Maintain a stack representing (left-turning) CH so farMaintain a stack representing (left-turning) CH so far
If If ppi i is left of last edge of stack, then PUSHis left of last edge of stack, then PUSH
Else, POP stack until it is left, charging work to popped pointsElse, POP stack until it is left, charging work to popped points
2828
O(n log n)
O(n) O(n)
Demo applet vlowest CH so far
Graham Scan
Graham Scan
Red points form right turn, so are discarded[DO] Section 2.4
Graham Scan Graham Scan
3030
[DO] Section 2.4
Graham Scan Graham Scan
[O’Rourke, Chapter 3]
Sorted points:
3232
[O’Rourke, Chapter 3]
Stack History Stack History
[O’Rourke, Chapter 3]
O(n log n)
O(n log n) : : Divide and Conquer Divide and Conquer
Split Split SS into into SSleft left and and SSright right , , sizes sizes n/2n/2
Recursively compute CH(Recursively compute CH(SSleft left ), CH(), CH(SSrightright))
Merge the two hulls by finding upper/lower Merge the two hulls by finding upper/lower bridges in O(
bridges in O(nn), by “wobbly stick”), by “wobbly stick”
3434
Sleft
Sright
Time:
T(n) 2T(n/2) + O(n)
T(n) = O(n log n)
Time O(n)
Time O(n) Time 2T(n/2)
Demo applet
Divide and Conquer Divide and Conquer
[DO] Section 2.6
Divide and Conquer Divide and Conquer
3636
[DO] Section 2.6
Divide and Conquer Divide and Conquer
[DO] Section 2.6
QuickHull QuickHull
QuickHull(QuickHull(a,b,Sa,b,S) ) to compute upperhull(to compute upperhull(SS))
• If If S=S=, , return ()return ()
• Else return (QuickHull(Else return (QuickHull(a,c,Aa,c,A), ), cc, QuickHull(, QuickHull(c,b,Bc,b,B))))
3838
Qhull website Qhull, Brad Barber (used within MATLAB) a
b c
S A B
Worst-case: O(n2 ) Avg: O(n)
Discard points in abc
c = point furthest from ab
Works well in higher
dimensions too!
QuickHull QuickHull
Applet (programmed by Jeff So)Applet (programmed by Jeff So)
Applet (by Lambert)Applet (by Lambert)
When is it “bad”? (see hw2)When is it “bad”? (see hw2)
• If no points are discarded (in If no points are discarded (in abc), at each abc), at each iteration, and
iteration, and
• The “balance” may be very lop-sided The “balance” may be very lop-sided
(between sets A and B; in fact, one set could (between sets A and B; in fact, one set could
be empty, at every iteration!) be empty, at every iteration!)
QuickHull QuickHull
4040
[David Mount lecture notes, Lecture 3]
O(n log n)
O(n log n) : : Incremental Construction Incremental Construction
Add points in the order given: Add points in the order given: vv11 ,v ,v22 ,…,v ,…,vnn
Maintain current Maintain current QQii = CH(= CH(vv11 ,v ,v22 ,…,v ,…,vii ))
Add Add vvi+1i+1 ::
• If If vvi+1i+1 QQi i , , do nothingdo nothing
• Else insert Else insert vvi+1 i+1 by by finding tangencies, finding tangencies,
updating the hull updating the hull
Worst-case cost of insertion: O(Worst-case cost of insertion: O(log nlog n))
But, uses complex data structuresBut, uses complex data structures
Point location test:
O(log n)
Binary search: O(log n)
AMS 545 / CSE 555
O(n log n)
O(n log n) : : Randomized Incremental Randomized Incremental
Add points in Add points in randomrandom order order
Keep current Keep current QQii = CH(= CH(vv11 ,v ,v22 ,…,v ,…,vii ))
Add Add vvi+1i+1 ::
• If If vvi+1i+1 QQi i , , do nothingdo nothing
• Else insert Else insert vvi+1 i+1 by by finding tangencies, finding tangencies,
updating the hull updating the hull
ExpectedExpected cost of insertion: O( cost of insertion: O(log nlog n))
4242
AMS 545 / CSE 555
Each uninserted Each uninserted vvjj Q Qii ((j>i+1j>i+1) points to the bucket ) points to the bucket (cone) containing it; each cone points to the list of (cone) containing it; each cone points to the list of
uninserted
uninserted vvjj Q Qi i within itwithin it (with its conflict edge)(with its conflict edge)
4343
vi+1 e
Add Add vvi+1i+1 Q Qii ::
• Start from “conflict” edge Start from “conflict” edge ee, and , and walk cw/ccw to establish new
walk cw/ccw to establish new tangencies from
tangencies from vvi+1 i+1 , , charging walk charging walk to deleted vertices
to deleted vertices
• Rebucket points in affected conesRebucket points in affected cones (update lists for each bucket)
(update lists for each bucket)
• Total work: O(Total work: O(nn) + rebucketing work) + rebucketing work
• E(rebucket cost for E(rebucket cost for vvj j at step at step ii) = ) = O(1)
O(1) P(rebucket) P(rebucket) O(1) O(1) (2/ (2/i i ) = O(1/) = O(1/i i )) E(total rebucket cost for
E(total rebucket cost for vvj j ) = ) =
O(1/O(1/i i ) )= O(= O(log nlog n))
Backwards Analysis:
vj was just rebucketed iff the last point inserted was one of the 2 endpoints of the (current) conflict edge, e, for v
vj
AMS 545 / CSE 555
Output-Sensitive:
Output-Sensitive: O(n log h) O(n log h)
The “ultimate” convex hull (in 2D) The “ultimate” convex hull (in 2D)
• ““Marriage before Conquest” : O(Marriage before Conquest” : O(n log hn log h))
• Lower bound Lower bound ((n log hn log h)) [Kirkpatrick & Seidel’86][Kirkpatrick & Seidel’86]
Simpler Simpler [Chan’95][Chan’95]
2 Ideas: 2 Ideas:
• Break point set Break point set SS into groups of appropriate size into groups of appropriate size mm (ideally,
(ideally, m = hm = h))
• Search for the “right” value of Search for the “right” value of mm ( = ( =hh, which we do , which we do not know in advance) by repeated squaring
not know in advance) by repeated squaring
4444
h = output size So, O(n log h) is BEST
POSSIBLE, as function of n and h !!
AMS 545 / CSE 555
Chan’s Algorithm Chan’s Algorithm
Break Break SS into into n/mn/m groups, each of size groups, each of size mm
• Find CH of each group (using, e.g., Graham scan):Find CH of each group (using, e.g., Graham scan):
O(m log mO(m log m) per group, so total O(() per group, so total O((n/mn/m) ) m log mm log m) = O() = O(n log mn log m))
Gift-wrap the Gift-wrap the n/mn/m hulls to get overall CH: hulls to get overall CH:
• At each gift-wrap step, when pivoting around vertex At each gift-wrap step, when pivoting around vertex vv
find the tangency point (binary search, O(find the tangency point (binary search, O(log mlog m)) to each group CH)) to each group CH
pick the smallest angle among the pick the smallest angle among the n/mn/m tangencies: tangencies:
• O( O( h (n/m) log mh (n/m) log m))
Hope: Hope: m=hm=h
Try Try mm = 4, 16, 256, 65536,… = 4, 16, 256, 65536,…
= O(n log h)
= O( h (n/h) log h) = O(n log h)
O(n ( log (221 ) + log (222 ) + log (223 ) + … log (22 log (log (h)) )
= O(n (21 + 22 + 23 + … + 2log (log (h)) ))
v
AMS 545 / CSE 555
CH of Simple Chains/Polygons CH of Simple Chains/Polygons
InputInput: : orderedordered list of points that form a simple list of points that form a simple chain/cycle
chain/cycle
Importance of Importance of simplicitysimplicity (noncrossing, Jordan curve) (noncrossing, Jordan curve) as a means of “sorting” (vs. sorting in
as a means of “sorting” (vs. sorting in x, yx, y))
Melkman’s Algorithm: O(Melkman’s Algorithm: O(nn) ) (vs. O((vs. O(n log nn log n) or O() or O(n log hn log h))))
4646
1
2
n
vi
Melkman’s Algorithm Melkman’s Algorithm
Keep hull in DEQUE: <
Keep hull in DEQUE: < v vb b ,v,vb+1 b+1 ,…,v,…,vt-1 t-1 ,v,vt t =v=vb b >>
While Left(
While Left(vvbb ,v ,vb+1b+1 ,v ,vii ) and Left() and Left(vvt-1t-1 ,v ,vtt ,v ,vii )) i i i+1 i+1
Remove
Remove vvb+1 b+1 , v , vb+2 b+2 ,… ,…
and and vvt-1 t-1 , v , vt-2 t-2 ,… ,…
until convexity restored until convexity restored New New vvbb =v =vtt = v = vii
Claim:
Claim: Simplicity assures that the only way for the chain Simplicity assures that the only way for the chain to exit the current hull is via pockets
to exit the current hull is via pockets vvbb v vb+1b+1 or or vvt-1t-1 v vtt . .
4747
vb
vb+1 vt-1
vt vi vt
vb
vt-1
vb+1
Simplicity helps!
Melkman’s Algorithm Melkman’s Algorithm
4848
Melkman’s Algorithm
Melkman’s Algorithm
Melkman’s Algorithm Melkman’s Algorithm
5050
Expected Size of CH
Expected Size of CH
Expected Size, h, of CH Expected Size, h, of CH
Claim: For n points uniform in a square, Claim: For n points uniform in a square, the expected number of maximal points the expected number of maximal points
is O(log n) is O(log n)
Corollary: E(h) = O(log n) Corollary: E(h) = O(log n)
Thus, Gift-Wrap/Jarvis runs in Thus, Gift-Wrap/Jarvis runs in expected O(n log n), if points are expected O(n log n), if points are
uniform in a box, etc.
uniform in a box, etc.
5252
(since CH vertices are subset of maximal points)
CH, Polytopes in Higher CH, Polytopes in Higher
Dimensions
Dimensions
CH, Polytopes in Higher CH, Polytopes in Higher
Dimensions Dimensions
5454
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
Convex Hull in 3D
Convex Hull in 3D Convex Hull in 3D
5656
CH in Higher Dimensions CH in Higher Dimensions
3D: Divide and conquer: 3D: Divide and conquer:
• T(T(nn) ) 2T( 2T(n/2n/2) + O() + O(nn))
• O(O(n log nn log n))
• Output-sensitive: O(Output-sensitive: O(n log hn log h) [Chan]) [Chan]
Higher dimensions: ( Higher dimensions: ( d d 4) 4)
• O(O(n n d/2 d/2 ), which is worst-case OPT, since point ), which is worst-case OPT, since point sets exist with
sets exist with h=h=((n n d/2 d/2 ))
• Output-sensitive: O((Output-sensitive: O((n+hn+h) ) loglogd-2 d-2 h h), for ), for d=d=4,54,5
5757
merge
h= O(n) applet
Convex Hull in 3D Convex Hull in 3D
5858
Convex Hull in 3D
Convex Hull in 3D
More Demos More Demos
Various 2D and 3D algorithms in an 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 from Jack Snoeyink Applets from Jack Snoeyink
6060
Review: Convex Hull Algorithms Review: Convex Hull Algorithms
O(O(nn44), O(), O(nn33), naïve ), naïve
O(O(nn22) incremental algorithm) incremental algorithm
O(O(nhnh) simple, “output-sensitive”) simple, “output-sensitive”
O(O(n log nn log n) Graham scan, divide-and-conquer, incremental ) Graham scan, divide-and-conquer, incremental (with fancy data structures); worst-case optimal (as fcn of (with fancy data structures); worst-case optimal (as fcn of nn))
O(O(n log hn log h) “ultimate” time bound ) “ultimate” time bound (as fcn of (as fcn of n,hn,h))
Randomized, expected O(Randomized, expected O(n log nn log n) (simple)) (simple)
3D: Divide and conquer: O(3D: Divide and conquer: O(n log nn log n))
• Output-sensitive: O(Output-sensitive: O(n log hn log h) [Chan]) [Chan]
Higher dimensions: (Higher dimensions: (d d 4)4)
• O(O(n n d/2 d/2 ), which is worst-case OPT, ), which is worst-case OPT,
• Output-sensitive: O((Output-sensitive: O((n+hn+h) ) loglogd-2 d-2 h h), for ), for d=d=4,54,5