Top Banner
Convex Hulls (2D) O’Rourke, Chapter 3
79

Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

May 21, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hulls (2D)

O’Rourke, Chapter 3

Page 2: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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)

Page 3: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Outline

β€’ Convex Hulls

β€’ Algorithms NaΓ―ve Implementation(s)

Gift Wrapping

Quick Hull

Graham’s Algorithm

Lower bound complexity

Page 4: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convexity

A set 𝑆 is convex if for any two points 𝑝, π‘ž ∈ 𝑆 the

line segment π‘π‘ž βŠ‚ 𝑆.

p

q

Not convex

SS

Convex?

Page 5: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convexity

A set 𝑆 is convex if it is the intersection of (possibly

infinitely many) half-spaces.

p

q

Not convex

SS

Convex?

Page 6: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.

Page 7: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

𝑝𝑛

Page 8: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.

𝑝

Page 9: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull

Claim:

If 𝑃 βŠ‚ ℝ2 is a polygon whose vertices are all

convex, then 𝑃 is convex.

Page 10: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull

Proof (by induction):

Otherwise, we could add a diagonal.

β‡’ By induction, each half is convex.

Page 11: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

Page 12: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

Page 13: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

𝑝𝑛

Page 14: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

𝑝𝑛

Page 15: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.

Page 16: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.

Page 17: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Outline

β€’ Convex Hulls

β€’ Algorithms NaΓ―ve Implementation(s)

Gift Wrapping

Quick Hull

Graham’s Algorithm

Lower bound complexity

Page 18: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

Page 19: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.

Page 20: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.

Page 21: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.

Page 22: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.

Page 23: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.

Page 24: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Outline

β€’ Convex Hulls

β€’ Algorithms NaΓ―ve Implementation(s)

Gift Wrapping

Quick Hull

Graham’s Algorithm

Lower bound complexity

Page 25: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.

Page 26: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.

Page 27: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.

Page 28: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Outline

β€’ Convex Hulls

β€’ Algorithms NaΓ―ve Implementation(s)

Gift Wrapping

Quick Hull

Graham’s Algorithm

Lower bound complexity

Page 29: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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 π‘π‘Ž.

π‘Ž

𝑏

𝑐

Page 30: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.

π‘Ž

𝑏

𝑐

Page 31: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.

Page 32: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull (2D)

QuickHull( 𝑆 βŠ‚ ℝ2 ) (π‘Ž, 𝑏) ← HorizontalExtrema( 𝑆 )

𝐴 ← RightOf( 𝑆 , π‘Žπ‘ )

𝐡 ← RightOf( 𝑆 , π‘π‘Ž )

𝑄𝐴 ← QuickHalfHull( 𝐴 , π‘Žπ‘ )

𝑄𝐡 ← QuickHalfHull( 𝐡 , π‘π‘Ž ) return π‘Ž βˆͺ 𝑄𝐴 βˆͺ 𝑏 βˆͺ 𝑄𝐡

Page 33: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull (2D)

QuickHull( 𝑆 βŠ‚ ℝ2 ) (𝒂, 𝒃) ← HorizontalExtrema( 𝑺 )

𝐴 ← RightOf( 𝑆 , π‘Žπ‘ )

𝐡 ← RightOf( 𝑆 , π‘π‘Ž )

𝑄𝐴 ← QuickHalfHull( 𝐴 , π‘Žπ‘ )

𝑄𝐡 ← QuickHalfHull( 𝐡 , π‘π‘Ž ) return π‘Ž βˆͺ 𝑄𝐴 βˆͺ 𝑏 βˆͺ 𝑄𝐡

π‘π‘Ž

Page 34: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull (2D)

QuickHull( 𝑆 βŠ‚ ℝ2 ) (π‘Ž, 𝑏) ← HorizontalExtrema( 𝑆 )

𝑨 ← RightOf( 𝑺 , 𝒂𝒃 )

𝑩 ← RightOf( 𝑺 , 𝒃𝒂 )

𝑄𝐴 ← QuickHalfHull( 𝐴 , π‘Žπ‘ )

𝑄𝐡 ← QuickHalfHull( 𝐡 , π‘π‘Ž ) return π‘Ž βˆͺ 𝑄𝐴 βˆͺ 𝑏 βˆͺ 𝑄𝐡

𝐴

𝐡

π‘π‘Ž

Page 35: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull (2D)

QuickHull( 𝑆 βŠ‚ ℝ2 ) (π‘Ž, 𝑏) ← HorizontalExtrema( 𝑆 )

𝐴 ← RightOf( 𝑆 , π‘Žπ‘ )

𝐡 ← RightOf( 𝑆 , π‘π‘Ž )

𝑸𝑨 ← QuickHalfHull( 𝑨 , 𝒂𝒃 )

𝑸𝑩 ← QuickHalfHull( 𝑩 , 𝒃𝒂 ) return π‘Ž βˆͺ 𝑄𝐴 βˆͺ 𝑏 βˆͺ 𝑄𝐡

𝑄𝐡

𝑄𝐴

π‘π‘Ž

Page 36: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull (2D)

QuickHull( 𝑆 βŠ‚ ℝ2 ) (π‘Ž, 𝑏) ← HorizontalExtrema( 𝑆 )

𝐴 ← RightOf( 𝑆 , π‘Žπ‘ )

𝐡 ← RightOf( 𝑆 , π‘π‘Ž )

𝑄𝐴 ← QuickHalfHull( 𝐴 , π‘Žπ‘ )

𝑄𝐡 ← QuickHalfHull( 𝐡 , π‘π‘Ž ) return 𝒂 βˆͺ 𝑸𝑨 βˆͺ 𝒃 βˆͺ 𝑸𝑩

Page 37: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

π‘π‘Ž

Convex Hull (2D)

(Recursion Level 0)

QuickHalfHull( 𝑆 βŠ‚ ℝ2, π‘Žπ‘ ∈ 𝑆 Γ— 𝑆 ) if( 𝑆 == βˆ… ) return βˆ… else

Β» 𝑐 ← Furthest( 𝑆 , π‘Žπ‘ )Β» 𝐴 ← RightOf( 𝑆 , π‘Žπ‘ )

Β» 𝐡 ← RightOf( 𝑆 , 𝑐𝑏 )Β» 𝑄𝐴 ← QuickHalfHull( 𝐴 , π‘Žπ‘ )

Β» 𝑄𝐡 ← QuickHalfHull( 𝐡 , 𝑐𝑏 )Β» return 𝑄𝐴 βˆͺ 𝑐 βˆͺ 𝑄𝐡

Page 38: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

𝑐

π‘π‘Ž

Convex Hull (2D)

(Recursion Level 0)

QuickHalfHull( 𝑆 βŠ‚ ℝ2, π‘Žπ‘ ∈ 𝑆 Γ— 𝑆 ) if( 𝑆 == βˆ… ) return βˆ… else

Β» 𝒄 ← Furthest( 𝑺 , 𝒂𝒃 )Β» 𝐴 ← RightOf( 𝑆 , π‘Žπ‘ )

Β» 𝐡 ← RightOf( 𝑆 , 𝑐𝑏 )Β» 𝑄𝐴 ← QuickHalfHull( 𝐴 , π‘Žπ‘ )

Β» 𝑄𝐡 ← QuickHalfHull( 𝐡 , 𝑐𝑏 )Β» return 𝑄𝐴 βˆͺ 𝑐 βˆͺ 𝑄𝐡

Page 39: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

𝑐

π‘π‘Ž

Convex Hull (2D)

(Recursion Level 0)

QuickHalfHull( 𝑆 βŠ‚ ℝ2, π‘Žπ‘ ∈ 𝑆 Γ— 𝑆 ) if( 𝑆 == βˆ… ) return βˆ…

else

Β» 𝑐 ← Furthest( 𝑆 , π‘Žπ‘ )Β» 𝑨 ← RightOf( 𝑺 , 𝒂𝒄 )

Β» 𝑩 ← RightOf( 𝑺 , 𝒄𝒃 )Β» 𝑄𝐴 ← QuickHalfHull( 𝐴 , π‘Žπ‘ )

Β» 𝑄𝐡 ← QuickHalfHull( 𝐡 , 𝑐𝑏 )Β» return 𝑄𝐴 βˆͺ 𝑐 βˆͺ 𝑄𝐡

𝐡𝐴

Page 40: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

π‘Ž

𝑐

𝑏

Convex Hull (2D)

(Recursion Level 0)

QuickHalfHull( 𝑆 βŠ‚ ℝ2, π‘Žπ‘ ∈ 𝑆 Γ— 𝑆 ) if( 𝑆 == βˆ… ) return βˆ… else

Β» 𝑐 ← Furthest( 𝑆 , π‘Žπ‘ )Β» 𝐴 ← RightOf( 𝑆 , π‘Žπ‘ )

Β» 𝐡 ← RightOf( 𝑆 , 𝑐𝑏 )Β» 𝑸𝑨 ← QuickHalfHull( 𝑨 , 𝒂𝒄 )

Β» 𝑄𝐡 ← QuickHalfHull( 𝐡 , 𝑐𝑏 )Β» return 𝑄𝐴 βˆͺ 𝑐 βˆͺ 𝑄𝐡

𝐴

Page 41: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

π‘Ž

𝑏

Convex Hull (2D)

(Recursion Level 1)

QuickHalfHull( 𝑆 βŠ‚ ℝ2, π‘Žπ‘ ∈ 𝑆 Γ— 𝑆 ) if( 𝑺 == βˆ… ) return βˆ… else

Β» 𝑐 ← Furthest( 𝑆 , π‘Žπ‘ )Β» 𝐴 ← RightOf( 𝑆 , π‘Žπ‘ )

Β» 𝐡 ← RightOf( 𝑆 , 𝑐𝑏 )Β» 𝑄𝐴 ← QuickHalfHull( 𝐴 , π‘Žπ‘ )

Β» 𝑄𝐡 ← QuickHalfHull( 𝐡 , 𝑐𝑏 )Β» return 𝑄𝐴 βˆͺ 𝑐 βˆͺ 𝑄𝐡

Page 42: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

𝑐

π‘π‘Ž

Convex Hull (2D)

(Recursion Level 0)

QuickHalfHull( 𝑆 βŠ‚ ℝ2, π‘Žπ‘ ∈ 𝑆 Γ— 𝑆 ) if( 𝑆 == βˆ… ) return βˆ… else

Β» 𝑐 ← Furthest( 𝑆 , π‘Žπ‘ )Β» 𝐴 ← RightOf( 𝑆 , π‘Žπ‘ )

Β» 𝐡 ← RightOf( 𝑆 , 𝑐𝑏 )Β» 𝑄𝐴 ← QuickHalfHull( 𝐴 , π‘Žπ‘ )

Β» 𝑸𝑩 ← QuickHalfHull( 𝑩 , 𝒄𝒃 )Β» return 𝑄𝐴 βˆͺ 𝑐 βˆͺ 𝑄𝐡

𝐡𝑄𝐴

Page 43: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

𝑏

π‘Ž

𝑐

Convex Hull (2D)

(Recursion Level 1)

QuickHalfHull( 𝑆 βŠ‚ ℝ2, π‘Žπ‘ ∈ 𝑆 Γ— 𝑆 ) if( 𝑆 == βˆ… ) return βˆ… else

Β» 𝒄 ← Furthest( 𝑺 , 𝒂𝒃 )Β» 𝐴 ← RightOf( 𝑆 , π‘Žπ‘ )

Β» 𝐡 ← RightOf( 𝑆 , 𝑐𝑏 )Β» 𝑄𝐴 ← QuickHalfHull( 𝐴 , π‘Žπ‘ )

Β» 𝑄𝐡 ← QuickHalfHull( 𝐡 , 𝑐𝑏 )Β» return 𝑄𝐴 βˆͺ 𝑐 βˆͺ 𝑄𝐡

Page 44: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

𝑏

π‘Ž

𝑐

Convex Hull (2D)

(Recursion Level 1)

QuickHalfHull( 𝑆 βŠ‚ ℝ2, π‘Žπ‘ ∈ 𝑆 Γ— 𝑆 ) if( 𝑆 == βˆ… ) return βˆ… else

Β» 𝑐 ← Furthest( 𝑆 , π‘Žπ‘ )Β» 𝑨 ← RightOf( 𝑺 , 𝒂𝒄 )

Β» 𝑩 ← RightOf( 𝑺 , 𝒄𝒃 )Β» 𝑄𝐴 ← QuickHalfHull( 𝐴 , π‘Žπ‘ )

Β» 𝑄𝐡 ← QuickHalfHull( 𝐡 , 𝑐𝑏 )Β» return 𝑄𝐴 βˆͺ 𝑐 βˆͺ 𝑄𝐡

𝐡

𝐴

Page 45: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

𝑏

π‘Ž

𝑐

Convex Hull (2D)

(Recursion Level 1)

QuickHalfHull( 𝑆 βŠ‚ ℝ2, π‘Žπ‘ ∈ 𝑆 Γ— 𝑆 ) if( 𝑆 == βˆ… ) return βˆ… else

Β» 𝑐 ← Furthest( 𝑆 , π‘Žπ‘ )Β» 𝐴 ← RightOf( 𝑆 , π‘Žπ‘ )

Β» 𝐡 ← RightOf( 𝑆 , 𝑐𝑏 )Β» 𝑸𝑨 ← QuickHalfHull( 𝑨 , 𝒂𝒄 )

Β» 𝑸𝑩 ← QuickHalfHull( 𝑩 , 𝒄𝒃 )Β» return 𝑄𝐴 βˆͺ 𝑐 βˆͺ 𝑄𝐡

𝑄𝐡

𝑄𝐴

Page 46: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

𝑏

π‘Ž

𝑐

Convex Hull (2D)

(Recursion Level 1)

QuickHalfHull( 𝑆 βŠ‚ ℝ2, π‘Žπ‘ ∈ 𝑆 Γ— 𝑆 ) if( 𝑆 == βˆ… ) return βˆ… else

Β» 𝑐 ← Furthest( 𝑆 , π‘Žπ‘ )Β» 𝐴 ← RightOf( 𝑆 , π‘Žπ‘ )

Β» 𝐡 ← RightOf( 𝑆 , 𝑐𝑏 )Β» 𝑄𝐴 ← QuickHalfHull( 𝐴 , π‘Žπ‘ )

Β» 𝑄𝐡 ← QuickHalfHull( 𝐡 , 𝑐𝑏 )Β» return 𝑸𝑨 βˆͺ 𝒄 βˆͺ 𝑸𝑩

Page 47: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

𝑐

π‘π‘Ž

Convex Hull (2D)

(Recursion Level 0)

QuickHalfHull( 𝑆 βŠ‚ ℝ2, π‘Žπ‘ ∈ 𝑆 Γ— 𝑆 ) if( 𝑆 == βˆ… ) return βˆ… else

Β» 𝑐 ← Furthest( 𝑆 , π‘Žπ‘ )Β» 𝐴 ← RightOf( 𝑆 , π‘Žπ‘ )

Β» 𝐡 ← RightOf( 𝑆 , 𝑐𝑏 )Β» 𝑸𝑨 ← QuickHalfHull( 𝑨 , 𝒂𝒄 )

Β» 𝑸𝑩 ← QuickHalfHull( 𝑩 , 𝒄𝒃 )Β» return 𝑄𝐴 βˆͺ 𝑐 βˆͺ 𝑄𝐡

𝑄𝐡𝑄𝐴

Page 48: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

𝑐

π‘π‘Ž

Convex Hull (2D)

(Recursion Level 0)

QuickHalfHull( 𝑆 βŠ‚ ℝ2, π‘Žπ‘ ∈ 𝑆 Γ— 𝑆 ) if( 𝑆 == βˆ… ) return βˆ… else

Β» 𝑐 ← Furthest( 𝑆 , π‘Žπ‘ )Β» 𝐴 ← RightOf( 𝑆 , π‘Žπ‘ )

Β» 𝐡 ← RightOf( 𝑆 , 𝑐𝑏 )Β» 𝑄𝐴 ← QuickHalfHull( 𝐴 , π‘Žπ‘ )

Β» 𝑄𝐡 ← QuickHalfHull( 𝐡 , 𝑐𝑏 )Β» return 𝑸𝑨 βˆͺ 𝒄 βˆͺ 𝑸𝑩

Page 49: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull (2D)

QuickHull Complexity:

Like QuickSort: In the worst case, the complexity can be 𝑂(𝑛2).

Page 50: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull (2D)

QuickHull Complexity:

Like QuickSort: In the worst case, the complexity can be 𝑂(𝑛2).

Page 51: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull (2D)

QuickHull Complexity:

Like QuickSort: In the worst case, the complexity can be 𝑂(𝑛2).

Page 52: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull (2D)

QuickHull Complexity:

Like QuickSort: In the worst case, the complexity can be 𝑂(𝑛2).

Page 53: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull (2D)

QuickHull Complexity:

Like QuickSort: In the worst case, the complexity can be 𝑂(𝑛2).

Page 54: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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?

Page 55: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Outline

β€’ Convex Hulls

β€’ Algorithms NaΓ―ve Implementation(s)

Gift Wrapping

Quick Hull

Graham’s Algorithm

Lower bound complexity

Page 56: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.

𝑝

𝑃

Page 57: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

Page 58: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull (2D)

GrahamScan( 𝑆 βŠ‚ ℝ2 ) 𝑝 ← PointInHull( 𝑆 ) 𝐻 ← SortByAngle( 𝑆 , 𝑝 ) while( RemoveReflexVertex( 𝐻 ) ){ } return 𝐻

Page 59: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

𝑣𝑖

Page 60: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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 𝐻.

β‡’ β„‹ 𝑆 βŠ‚ 𝐻.

Page 61: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull (2D)

GrahamScan( 𝑆 βŠ‚ ℝ2 )

Page 62: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull (2D)

GrahamScan( 𝑆 βŠ‚ ℝ2 ) 𝑝 ← BottommostRightmost( 𝑆 )

𝑝

Page 63: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull (2D)

GrahamScan( 𝑆 βŠ‚ ℝ2 ) 𝑝 ← BottommostRightmost( 𝑆 )

𝑆 ← SortByAngleAndLength( 𝑝 , 𝑆 βˆ’ {𝑝} )

𝑝1

2

34

5

6

78

9

10

Page 64: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull (2D)

GrahamScan( 𝑆 βŠ‚ ℝ2 ) 𝑝 ← BottommostRightmost( 𝑆 )

𝑆 ← SortByAngleAndLength( 𝑝 , 𝑆 βˆ’ {𝑝} ) if( angle(𝑝𝑖) == angle(𝑝𝑖+1) ): 𝑆 ← 𝑆 βˆ’ 𝑝𝑖

𝑝

𝑝1

𝑝2

𝑝3

𝑝4

𝑝6

𝑝7

𝑝5

Page 65: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.

Page 66: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

Page 67: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

Page 68: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

Page 69: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

Page 70: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

Page 71: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

Page 72: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

Page 73: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

Page 74: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

Page 75: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Convex Hull (2D)

GrahamScan( 𝑆 βŠ‚ ℝ2 ) 𝑝 ← BottommostRightmost( 𝑆 )

𝑆 ← SortByAngleAndLength( 𝑝 , 𝑆 βˆ’ {𝑝} ) if( angle(𝑝𝑖) == angle(𝑝𝑖+1) ): 𝑆 ← 𝑆 βˆ’ 𝑝𝑖

𝑖 ← 2

𝑄 ← 𝑝, 𝑝1

while( 𝑖 < 𝑆 )

Β»if( Left( 𝑝𝑖 , LastEdge( 𝑄 ) )– Push( 𝑝𝑖 , 𝑄 )– 𝑖 ← 𝑖 + 1

Β»else– Pop( 𝑄 )

𝑂(𝑛)

𝑂(𝑛 log 𝑛)

𝑂(𝑛)

𝑂(𝑛)

Page 76: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

Outline

β€’ Convex Hulls

β€’ Algorithms NaΓ―ve Implementation(s)

Gift Wrapping

Quick Hull

Graham’s Algorithm

Lower bound complexity

Page 77: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.

Page 78: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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

𝑦 = 𝑓(π‘₯)

Page 79: Convex Hulls (2D)misha/Spring16/06.pdfConvex Hull (2D) NaΓ―ve Algorithm (𝑛3): For each directed edge βˆˆπ‘†Γ—π‘†, check if half-space to the right of is empty of points (and there

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.