Convex Hulls (2D) OβRourke, Chapter 3
Convex Hulls (2D)
OβRourke, Chapter 3
Announcements
β’ We are live on Piazza:
http://piazza.com/jhu/spring2016/600459
β’ Code-base was updated last Wednesday
(Fixes a bug where π¦-values are read in as zero)
Outline
β’ Convex Hulls
β’ Algorithms NaΓ―ve Implementation(s)
Gift Wrapping
Quick Hull
Grahamβs Algorithm
Lower bound complexity
Convexity
A set π is convex if for any two points π, π β π the
line segment ππ β π.
p
q
Not convex
SS
Convex?
Convexity
A set π is convex if it is the intersection of (possibly
infinitely many) half-spaces.
p
q
Not convex
SS
Convex?
Convexity
Given points π1, β¦ , ππ β βπ, a point π β βπ is a
convex combination of the points if π can be
expressed as the linear sum:
π =
π=1
π
πΌπ β ππ
with πΌπ β₯ 0 and πΌ1 + β― + πΌπ = 1.
Convex Hull
The convex hull of a set of points π β βπ,
denoted β(π), is the: set of all convex combinations of points in π,
set of all convex combinations of π + 1 points in π,
intersection of all convex sets πΆ w/ π β πΆ,
intersection of all half-spaces π» w/ π β π»,
smallest convex polygon containing π.β(π)
π1
ππ
Convex Hull
Note:
If π β π β β2 and π is a vertex of the convex
hull then π must be a convex vertex.
Otherwise, we could create
a line segment with vertices
inside of the hull but which
isnβt strictly interior.
π
Convex Hull
Claim:
If π β β2 is a polygon whose vertices are all
convex, then π is convex.
Convex Hull
Proof (by induction):
Otherwise, we could add a diagonal.
β By induction, each half is convex.
Convex Hull
Proof (by induction):
Otherwise, we could add a diagonal.
β If π is not convex there must be a segment
between the two parts that exits π.Choose π1 and π2 above/below the diagonal.
Evolve the segment to π1π2.
Since π1 and π2 are above/below,
π1π2 crosses the diagonal and
is entirely inside π.π1
π2
Convex Hull
Proof (by induction):
Otherwise, we could add a diagonal.
β If π is not convex there must be a segment
between the two parts that exits π.Choose π1 and π2 above/below the diagonal.
Evolve the segment to π1π2.
Since π1 and π2 are above/below,
π1π2 crosses the diagonal and
is entirely inside π.
The last point at which the
evolving segment is not fully
inside must be a reflex vertex.
π1
π2
Convex Hull
The extreme points of a set of points π β β2
are the points which are on the convex hull
and have interior angle strictly less than π.
β(π)π1
ππ
Convex Hull
Goal:
Given a set of points S = π1, β¦ , ππ β βπ,
compute the convex hull β(π) efficiently.
β’ Do we want all points on the hull or just the
extreme ones?
β’ Do the output vertices need
to be sorted or is the set of
(extreme) vertices sufficient?
β(π)π1
ππ
Convex Hull
Goal:
Given a set of points S = π1, β¦ , ππ β βπ,
compute the convex hull β(π) efficiently.
β’ Do we want all points on the hull or just the
extreme ones?
β’ Do the output vertices need
to be sorted or is the set of
(extreme) vertices sufficient?
β(π)π1
ππ
We will focus on the ordered output
of the extreme points on the hull.
Convex Hull (2D)
Note:
We can find a hull vertex in linear time by
finding the vertex that is extremal w.r.t. to
some direction.
Outline
β’ Convex Hulls
β’ Algorithms NaΓ―ve Implementation(s)
Gift Wrapping
Quick Hull
Grahamβs Algorithm
Lower bound complexity
Convex Hull (2D)
NaΓ―ve Algorithm:
For each directed edge π β π Γ π, check if the
half-space to the right of π is empty of points
(and there are no
points on the line
outside the segment).
If the rest of the
points are on one
side, the segment
is on the hull
Otherwise the segment
is not on the hull
Convex Hull (2D)
NaΓ―ve Algorithm π(π3):For each directed edge π β π Γ π, check if
half-space to the right of π is empty of points
(and there are no
points on the line
outside the segment).
Note:
The output is the set
of (unordered) extreme
points on the hull.
Convex Hull (2D)
NaΓ―ve Algorithm π(π3):For each directed edge π β π Γ π, check if
half-space to the right of π is empty of points
(and there are no
points on the line
outside the segment).
Note:
The output is the set
of (unordered) extreme
points on the hull.If we want the ordered points, we can stitch the edges
together in β€ π(π2) time in a post-processing step.
Convex Hull (2D)
NaΓ―ve Algorithm++:
Grow the hull by starting at a hull vertex and
searching for the next edge on the hull by
trying all possible edges and testing if they
are on the hull.
Convex Hull (2D)
NaΓ―ve Algorithm++ π(π2β)*:
Grow the hull by starting at a hull vertex and
searching for the next edge on the hull by
trying all possible edges and testing if they
are on the hull.
Note:
By explicitly forcing the
output to be sorted, we
end up with a faster
algorithm.*β is the number of points on the hull.
Convex Hull (2D)
NaΓ―ve Algorithm++ π(π2β)*:
Grow the hull by starting at a hull vertex and
searching for the next edge on the hull by
trying all possible edges and testing if they
are on the hull.
Note:
By explicitly forcing the
output to be sorted, we
end up with a faster
algorithm.This implementation is output sensitive.*β is the number of points on the hull.
Outline
β’ Convex Hulls
β’ Algorithms NaΓ―ve Implementation(s)
Gift Wrapping
Quick Hull
Grahamβs Algorithm
Lower bound complexity
Convex Hull (2D)
Note:
The next edge on the hull is the one making
the largest angle. (If two points make the
same angle, ignore the closer one.)
Gift Wrapping:
Grow by finding the edge
making the largest angle.
Convex Hull (2D)
Note:
The next edge on the hull is the one making
the largest angle. (If two points make the
same angle, ignore the closer one.)
Gift Wrapping π(πβ):Grow by finding the edge
making the largest angle.
Convex Hull (2D)
Note:
The next edge on the hull is the one making
the largest angle. (If two points make the
same angle, ignore the closer one.)
Gift Wrapping π(πβ):Grow by finding the edge
making the largest angle.A similar approach makes it possible to find
a hull edge in linear time.
Outline
β’ Convex Hulls
β’ Algorithms NaΓ―ve Implementation(s)
Gift Wrapping
Quick Hull
Grahamβs Algorithm
Lower bound complexity
Convex Hull (2D)
Observation:
Given a hull edge (π, π), we can find the point
π furthest from the edge in linear time.
1. The point π is on the hull.
2. The triangle Ξπππpartitions the input
into three regions:I. Points inside Ξπππ.
II. Points to the right of ππ.
III. Points to the right of ππ.
π
π
π
Convex Hull (2D)
Observation:
Given a hull edge (π, π), we can find the point
π furthest from the edge in linear time.
β Divide-and-conquer: Discard points inside Ξπππ
Separately compute the
half-hulls to the right of
ππ and the right of ππ.
Merge the two hulls.
π
π
π
Convex Hull (2D)
Observation:
Given a hull edge (π, π), we can find the point
π furthest from the edge in linear time.
β Divide-and-conquer: Discard points inside Ξπππ
Separately compute the
half-hulls to the right of
ππ and the right of ππ.
Merge the two hulls.
π
π
π
We donβt require ππ to be a hull edge.
As long as itβs a hull diagonal merging is easy.
Convex Hull (2D)
QuickHull( π β β2 ) (π, π) β HorizontalExtrema( π )
π΄ β RightOf( π , ππ )
π΅ β RightOf( π , ππ )
ππ΄ β QuickHalfHull( π΄ , ππ )
ππ΅ β QuickHalfHull( π΅ , ππ ) return π βͺ ππ΄ βͺ π βͺ ππ΅
Convex Hull (2D)
QuickHull( π β β2 ) (π, π) β HorizontalExtrema( πΊ )
π΄ β RightOf( π , ππ )
π΅ β RightOf( π , ππ )
ππ΄ β QuickHalfHull( π΄ , ππ )
ππ΅ β QuickHalfHull( π΅ , ππ ) return π βͺ ππ΄ βͺ π βͺ ππ΅
ππ
Convex Hull (2D)
QuickHull( π β β2 ) (π, π) β HorizontalExtrema( π )
π¨ β RightOf( πΊ , ππ )
π© β RightOf( πΊ , ππ )
ππ΄ β QuickHalfHull( π΄ , ππ )
ππ΅ β QuickHalfHull( π΅ , ππ ) return π βͺ ππ΄ βͺ π βͺ ππ΅
π΄
π΅
ππ
Convex Hull (2D)
QuickHull( π β β2 ) (π, π) β HorizontalExtrema( π )
π΄ β RightOf( π , ππ )
π΅ β RightOf( π , ππ )
πΈπ¨ β QuickHalfHull( π¨ , ππ )
πΈπ© β QuickHalfHull( π© , ππ ) return π βͺ ππ΄ βͺ π βͺ ππ΅
ππ΅
ππ΄
ππ
Convex Hull (2D)
QuickHull( π β β2 ) (π, π) β HorizontalExtrema( π )
π΄ β RightOf( π , ππ )
π΅ β RightOf( π , ππ )
ππ΄ β QuickHalfHull( π΄ , ππ )
ππ΅ β QuickHalfHull( π΅ , ππ ) return π βͺ πΈπ¨ βͺ π βͺ πΈπ©
ππ
Convex Hull (2D)
(Recursion Level 0)
QuickHalfHull( π β β2, ππ β π Γ π ) if( π == β ) return β else
Β» π β Furthest( π , ππ )Β» π΄ β RightOf( π , ππ )
Β» π΅ β RightOf( π , ππ )Β» ππ΄ β QuickHalfHull( π΄ , ππ )
Β» ππ΅ β QuickHalfHull( π΅ , ππ )Β» return ππ΄ βͺ π βͺ ππ΅
π
ππ
Convex Hull (2D)
(Recursion Level 0)
QuickHalfHull( π β β2, ππ β π Γ π ) if( π == β ) return β else
Β» π β Furthest( πΊ , ππ )Β» π΄ β RightOf( π , ππ )
Β» π΅ β RightOf( π , ππ )Β» ππ΄ β QuickHalfHull( π΄ , ππ )
Β» ππ΅ β QuickHalfHull( π΅ , ππ )Β» return ππ΄ βͺ π βͺ ππ΅
π
ππ
Convex Hull (2D)
(Recursion Level 0)
QuickHalfHull( π β β2, ππ β π Γ π ) if( π == β ) return β
else
Β» π β Furthest( π , ππ )Β» π¨ β RightOf( πΊ , ππ )
Β» π© β RightOf( πΊ , ππ )Β» ππ΄ β QuickHalfHull( π΄ , ππ )
Β» ππ΅ β QuickHalfHull( π΅ , ππ )Β» return ππ΄ βͺ π βͺ ππ΅
π΅π΄
π
π
π
Convex Hull (2D)
(Recursion Level 0)
QuickHalfHull( π β β2, ππ β π Γ π ) if( π == β ) return β else
Β» π β Furthest( π , ππ )Β» π΄ β RightOf( π , ππ )
Β» π΅ β RightOf( π , ππ )Β» πΈπ¨ β QuickHalfHull( π¨ , ππ )
Β» ππ΅ β QuickHalfHull( π΅ , ππ )Β» return ππ΄ βͺ π βͺ ππ΅
π΄
π
π
Convex Hull (2D)
(Recursion Level 1)
QuickHalfHull( π β β2, ππ β π Γ π ) if( πΊ == β ) return β else
Β» π β Furthest( π , ππ )Β» π΄ β RightOf( π , ππ )
Β» π΅ β RightOf( π , ππ )Β» ππ΄ β QuickHalfHull( π΄ , ππ )
Β» ππ΅ β QuickHalfHull( π΅ , ππ )Β» return ππ΄ βͺ π βͺ ππ΅
π
ππ
Convex Hull (2D)
(Recursion Level 0)
QuickHalfHull( π β β2, ππ β π Γ π ) if( π == β ) return β else
Β» π β Furthest( π , ππ )Β» π΄ β RightOf( π , ππ )
Β» π΅ β RightOf( π , ππ )Β» ππ΄ β QuickHalfHull( π΄ , ππ )
Β» πΈπ© β QuickHalfHull( π© , ππ )Β» return ππ΄ βͺ π βͺ ππ΅
π΅ππ΄
π
π
π
Convex Hull (2D)
(Recursion Level 1)
QuickHalfHull( π β β2, ππ β π Γ π ) if( π == β ) return β else
Β» π β Furthest( πΊ , ππ )Β» π΄ β RightOf( π , ππ )
Β» π΅ β RightOf( π , ππ )Β» ππ΄ β QuickHalfHull( π΄ , ππ )
Β» ππ΅ β QuickHalfHull( π΅ , ππ )Β» return ππ΄ βͺ π βͺ ππ΅
π
π
π
Convex Hull (2D)
(Recursion Level 1)
QuickHalfHull( π β β2, ππ β π Γ π ) if( π == β ) return β else
Β» π β Furthest( π , ππ )Β» π¨ β RightOf( πΊ , ππ )
Β» π© β RightOf( πΊ , ππ )Β» ππ΄ β QuickHalfHull( π΄ , ππ )
Β» ππ΅ β QuickHalfHull( π΅ , ππ )Β» return ππ΄ βͺ π βͺ ππ΅
π΅
π΄
π
π
π
Convex Hull (2D)
(Recursion Level 1)
QuickHalfHull( π β β2, ππ β π Γ π ) if( π == β ) return β else
Β» π β Furthest( π , ππ )Β» π΄ β RightOf( π , ππ )
Β» π΅ β RightOf( π , ππ )Β» πΈπ¨ β QuickHalfHull( π¨ , ππ )
Β» πΈπ© β QuickHalfHull( π© , ππ )Β» return ππ΄ βͺ π βͺ ππ΅
ππ΅
ππ΄
π
π
π
Convex Hull (2D)
(Recursion Level 1)
QuickHalfHull( π β β2, ππ β π Γ π ) if( π == β ) return β else
Β» π β Furthest( π , ππ )Β» π΄ β RightOf( π , ππ )
Β» π΅ β RightOf( π , ππ )Β» ππ΄ β QuickHalfHull( π΄ , ππ )
Β» ππ΅ β QuickHalfHull( π΅ , ππ )Β» return πΈπ¨ βͺ π βͺ πΈπ©
π
ππ
Convex Hull (2D)
(Recursion Level 0)
QuickHalfHull( π β β2, ππ β π Γ π ) if( π == β ) return β else
Β» π β Furthest( π , ππ )Β» π΄ β RightOf( π , ππ )
Β» π΅ β RightOf( π , ππ )Β» πΈπ¨ β QuickHalfHull( π¨ , ππ )
Β» πΈπ© β QuickHalfHull( π© , ππ )Β» return ππ΄ βͺ π βͺ ππ΅
ππ΅ππ΄
π
ππ
Convex Hull (2D)
(Recursion Level 0)
QuickHalfHull( π β β2, ππ β π Γ π ) if( π == β ) return β else
Β» π β Furthest( π , ππ )Β» π΄ β RightOf( π , ππ )
Β» π΅ β RightOf( π , ππ )Β» ππ΄ β QuickHalfHull( π΄ , ππ )
Β» ππ΅ β QuickHalfHull( π΅ , ππ )Β» return πΈπ¨ βͺ π βͺ πΈπ©
Convex Hull (2D)
QuickHull Complexity:
Like QuickSort: In the worst case, the complexity can be π(π2).
Convex Hull (2D)
QuickHull Complexity:
Like QuickSort: In the worst case, the complexity can be π(π2).
Convex Hull (2D)
QuickHull Complexity:
Like QuickSort: In the worst case, the complexity can be π(π2).
Convex Hull (2D)
QuickHull Complexity:
Like QuickSort: In the worst case, the complexity can be π(π2).
Convex Hull (2D)
QuickHull Complexity:
Like QuickSort: In the worst case, the complexity can be π(π2).
Convex Hull (2D)
QuickHull Complexity:
Like QuickSort: In the worst case, the complexity can be π(π2). In practice it is π π log π .
The implementation is output sensitive.
Does it extend to higher dimensions?
Outline
β’ Convex Hulls
β’ Algorithms NaΓ―ve Implementation(s)
Gift Wrapping
Quick Hull
Grahamβs Algorithm
Lower bound complexity
Convex Hull (2D)
Grahamβs Observation:
If π β β2 is a convex polygon and π β π is a point in
the interior of the polygon, then the angle of the line
segments between π and the ordered vertices of πis monotonic.
π
π
Convex Hull (2D)
Grahamβs Observation:
WLOG assume π and π£π lie on a vertical line with πbelow π£π.
Since the polygon is convex, π is to the left of π£ππ£π+1.
β π£π+1 is to the left of the vertical.
Since the polygon is convex, π is to the left of π£πβ1π£π.
β π£πβ1 is to the right of the vertical.
β The angles β ππ£πβ1, β ππ£π, β ππ£π+1
increase monotonically.
π
π£πβ1
π£π
π£π+1
Convex Hull (2D)
GrahamScan( π β β2 ) π β PointInHull( π ) π» β SortByAngle( π , π ) while( RemoveReflexVertex( π» ) ){ } return π»
GrahamScan( π β β2 ) π β PointInHull( π ) π» β SortByAngle( π , π ) while( RemoveReflexVertex( π» ) ){ } return π»
Note:
At every iteration, the vertices of π» are sorted by
angle relative to π.
β Hull vertices can never be removed because the
angle between the previous and next vertex is
always convex.
Convex Hull (2D)
π
π£πβ1
π£π+1
π£π
Convex Hull (2D)
GrahamScan( π β β2 ) π β PointInHull( π ) π» β SortByAngle( π , π ) while( RemoveReflexVertex( π» ) ){ } return π»
Correctness:
The output polygon has only convex vertices.
β Itβs convex.
β π» β β(π).
All hull vertices are in π».
β β π β π».
Convex Hull (2D)
GrahamScan( π β β2 )
Convex Hull (2D)
GrahamScan( π β β2 ) π β BottommostRightmost( π )
π
Convex Hull (2D)
GrahamScan( π β β2 ) π β BottommostRightmost( π )
π β SortByAngleAndLength( π , π β {π} )
π1
2
34
5
6
78
9
10
Convex Hull (2D)
GrahamScan( π β β2 ) π β BottommostRightmost( π )
π β SortByAngleAndLength( π , π β {π} ) if( angle(ππ) == angle(ππ+1) ): π β π β ππ
π
π1
π2
π3
π4
π6
π7
π5
Convex Hull (2D)
GrahamScan( π β β2 ) π β BottommostRightmost( π )
π β SortByAngleAndLength( π , π β {π} ) if( angle(ππ) == angle(ππ+1) ): π β π β ππ
π β 2
π β π, π1
π2
π3
π4
π6
π7
π5
π
π1
Note:
Since π is bottom-(right)-most,
vertices are sorted by angle in
0, π , and non-extreme points
are removed, ππ1 is on the hull.
Convex Hull (2D)
GrahamScan( π β β2 ) π β BottommostRightmost( π )
π β SortByAngleAndLength( π , π β {π} ) if( angle(ππ) == angle(ππ+1) ): π β π β ππ
π β 2
π β π, π1
while( π < π )
Β» if( Left( ππ , LastEdge( πΈ ) )β Push( ππ , πΈ )β π β π + π
Β»elseβ Pop( π )
π3
π4
π6
π7
π5
π2
π
π1
Convex Hull (2D)
GrahamScan( π β β2 ) π β BottommostRightmost( π )
π β SortByAngleAndLength( π , π β {π} ) if( angle(ππ) == angle(ππ+1) ): π β π β ππ
π β 2
π β π, π1
while( π < π )
Β» if( Left( ππ , LastEdge( πΈ ) )β Push( ππ , πΈ )β π β π + π
Β»elseβ Pop( π )
π3
π4
π6
π7
π5
π
π1
π2
Convex Hull (2D)
GrahamScan( π β β2 ) π β BottommostRightmost( π )
π β SortByAngleAndLength( π , π β {π} ) if( angle(ππ) == angle(ππ+1) ): π β π β ππ
π β 2
π β π, π1
while( π < π )
Β»if( Left( ππ , LastEdge( π ) )β Push( ππ , π )β π β π + 1
Β»elseβ Pop( πΈ )
π4
π6
π7
π5
π2
π
π1
π3
Convex Hull (2D)
GrahamScan( π β β2 ) π β BottommostRightmost( π )
π β SortByAngleAndLength( π , π β {π} ) if( angle(ππ) == angle(ππ+1) ): π β π β ππ
π β 2
π β π, π1
while( π < π )
Β» if( Left( ππ , LastEdge( πΈ ) )β Push( ππ , πΈ )β π β π + π
Β»elseβ Pop( π )
π4
π6
π7
π5
π
π3
π2
π1
Convex Hull (2D)
GrahamScan( π β β2 ) π β BottommostRightmost( π )
π β SortByAngleAndLength( π , π β {π} ) if( angle(ππ) == angle(ππ+1) ): π β π β ππ
π β 2
π β π, π1
while( π < π )
Β» if( Left( ππ , LastEdge( πΈ ) )β Push( ππ , πΈ )β π β π + π
Β»elseβ Pop( π )
π6
π7
π5
π
π2
π1
π4
π3
Convex Hull (2D)
GrahamScan( π β β2 ) π β BottommostRightmost( π )
π β SortByAngleAndLength( π , π β {π} ) if( angle(ππ) == angle(ππ+1) ): π β π β ππ
π β 2
π β π, π1
while( π < π )
Β»if( Left( ππ , LastEdge( π ) )β Push( ππ , π )β π β π + 1
Β»elseβ Pop( πΈ )
π6
π7
π5
π
π2
π1
π4
π3
Convex Hull (2D)
GrahamScan( π β β2 ) π β BottommostRightmost( π )
π β SortByAngleAndLength( π , π β {π} ) if( angle(ππ) == angle(ππ+1) ): π β π β ππ
π β 2
π β π, π1
while( π < π )
Β» if( Left( ππ , LastEdge( πΈ ) )β Push( ππ , πΈ )β π β π + π
Β»elseβ Pop( π )
π7
π5
π
π2
π1
π4
π3
π6
Convex Hull (2D)
GrahamScan( π β β2 ) π β BottommostRightmost( π )
π β SortByAngleAndLength( π , π β {π} ) if( angle(ππ) == angle(ππ+1) ): π β π β ππ
π β 2
π β π, π1
while( π < π )
Β» if( Left( ππ , LastEdge( πΈ ) )β Push( ππ , πΈ )β π β π + π
Β»elseβ Pop( π )
π6
π7
π
π2
π1
π5
π4
π3
Convex Hull (2D)
GrahamScan( π β β2 ) π β BottommostRightmost( π )
π β SortByAngleAndLength( π , π β {π} ) if( angle(ππ) == angle(ππ+1) ): π β π β ππ
π β 2
π β π, π1
while( π < π )
Β»if( Left( ππ , LastEdge( π ) )β Push( ππ , π )β π β π + 1
Β»elseβ Pop( π )
π6
π7
π
π2
π1
π5
π4
π3
Convex Hull (2D)
GrahamScan( π β β2 ) π β BottommostRightmost( π )
π β SortByAngleAndLength( π , π β {π} ) if( angle(ππ) == angle(ππ+1) ): π β π β ππ
π β 2
π β π, π1
while( π < π )
Β»if( Left( ππ , LastEdge( π ) )β Push( ππ , π )β π β π + 1
Β»elseβ Pop( π )
π(π)
π(π log π)
π(π)
π(π)
Outline
β’ Convex Hulls
β’ Algorithms NaΓ―ve Implementation(s)
Gift Wrapping
Quick Hull
Grahamβs Algorithm
Lower bound complexity
Lower Bound Complexity
Recall:
Sorting π numbers has lower bound complexity
π π log π .
Approach:
We will show that computing the 2D hull has the
same complexity by reducing sorting to the problem
of computing the convex hull.
Lower Bound Complexity
Sorting β Convex Hull Reduction (Shamos):
Given a set of points π₯π β β:
Choose a function π(π₯) w/ πβ²β² π₯ > 0
Lift the points onto the curve
Compute the convex hull
Return the points on the
lower hull, starting w/
the left-most.
π₯4π₯5π₯3π₯2 π₯2 π₯0
π¦ = π(π₯)
Lower Bound Complexity
Sorting β Convex Hull Reduction (Shamos):
Given a set of points π₯π β β:
Choose a function π(π₯) w/ πβ²β² π₯ > 0
Lift the points onto the curve
Compute the convex hull
Return the points on the
lower hull, starting w/
the left-most.
π₯4π₯5π₯3π₯2 π₯2 π₯0
π¦ = π(π₯)The reduction assumes that the hull
vertices are output in order.