Lec.2

Computational Geometry:
Convex hull II
Panos Giannopoulos, Wolfgang Mulzer, Lena Schlipf
AG TI
SS 2013
Outline
Graham’s scan (Andrew’s variant)
Chan’s algorithm
Assignment 2
,
FU Berlin, Computational Geometry:, SS 2013
2
Convex hull
Convex hull
For any subset of the plane (set of
points, rectangle, simple polygon),
its convex hull is the smallest convex
set that contains that subset
CGAL Demo
Computational Geometry
Lecture 1: Introduction and Convex Hulls
Anybody wants programming exercises ?
,
FU Berlin, Computational Geometry:, SS 2013
3
Convex hull problem
Convex hull problem
Give an algorithm that computes the
convex hull of any given set of n
points in the plane efficiently
Give an algorithm that computes
the
hull2nofcoordinates,
any given set
Theconvex
input has
so
of
n
points
in
the
plane
efficiently
O(n) size
Question: Why can’t we expect to
do any better than O(n) time?
Lower bound: Any convex hull algorithm requires Ω(n log n)
operations in the quadratic decision tree
model
[Yao,
1981]Lecture 1: Introduction and Convex Hulls
Computational
Geometry
,
FU Berlin, Computational Geometry:, SS 2013
4
Graham’s scan (Andrew’s variant)
Approach: incremental, from left to right
First compute the upper boundary (hull) of the convex hull this
way (property: on the upper hull, points appear in x-order)
Main idea: sort the points from left to right (= by x-coordinate)
Then, insert the points in this order, and maintain the upper hull
so far
Then, compute the lower hull analogously (from right to left)
Finally, append lower hull to upper hull
,
FU Berlin, Computational Geometry:, SS 2013
5
More on convex hulls
Algorithm development
Algorithm analysis
The
idea... an algorithm
Developing
Observation: from left to
right, there are only right turns
on the upper hull
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
6
More on convex hulls
Algorithm development
Algorithm analysis
The
idea... an algorithm
Developing
Initialize by inserting the
leftmost two points
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
6
More on convex hulls
Algorithm development
Algorithm analysis
The
idea... an algorithm
Developing
If we add the third point there
will be a right turn at the
previous point, so we add it
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
6
More on convex hulls
Algorithm development
Algorithm analysis
The
idea... an algorithm
Developing
If we add the fourth point we
get a left turn at the third
point
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
6
More on convex hulls
Algorithm development
Algorithm analysis
The
idea... an algorithm
Developing
. . . so we remove the third
point from the upper hull
when we add the fourth
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
6
More on convex hulls
Algorithm development
Algorithm analysis
The
idea... an algorithm
Developing
If we add the fifth point we get
a left turn at the fourth point
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
6
More on convex hulls
Algorithm development
Algorithm analysis
The
idea... an algorithm
Developing
. . . so we remove the fourth
point when we add the fifth
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
6
More on convex hulls
Algorithm development
Algorithm analysis
The
idea... an algorithm
Developing
If we add the sixth point we
get a right turn at the fifth
point, so we just add it
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
6
More on convex hulls
Algorithm development
Algorithm analysis
The
idea... an algorithm
Developing
We also just add the seventh
point
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
6
More on convex hulls
Algorithm development
Algorithm analysis
The
idea... an algorithm
Developing
When adding the eight point
. . . we must remove the
seventh point
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
6
More on convex hulls
Algorithm development
Algorithm analysis
The
idea... an algorithm
Developing
. . . we must remove the
seventh point
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
6
More on convex hulls
Algorithm development
Algorithm analysis
The
idea... an algorithm
Developing
. . . and also the sixth point
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
6
More on convex hulls
Algorithm development
Algorithm analysis
The
idea... an algorithm
Developing
. . . and also the fifth point
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
6
More on convex hulls
Algorithm development
Algorithm analysis
The
idea... an algorithm
Developing
After two more steps we get:
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
6
More on convex hulls
Algorithm development
Algorithm analysis
The algorithm (for the upper hull..)
The pseudo-code
Algorithm ConvexHull(P)
Input. A set P of points in the plane.
Output. A list containing the vertices of CH(P) in clockwise order.
1. Sort the points by x-coordinate, resulting in a sequence
p1 , . . . , pn .
2. Put the points p1 and p2 in a list Lupper , with p1 as the first
point.
3. for i ← 3 to n
4.
do Append pi to Lupper .
5.
while Lupper contains more than two points and the
last three points in Lupper do not
make a right turn
6.
do Delete the middle of the last three points from
Lupper .
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
7
..and for the lower hull..
7. Put the points pn and pn−1 in a list Llower , with pn as the first
point.
8. for i ← n − 2 down to 1
9.
10.
11.
do Append pi to Llower .
while Llower contains more than 2 points and the last
three points in Llower do not make a right turn
do Delete the middle of the last three points from Llower
12. Remove the first and last point from Llower to avoid
duplication of the points where the upper and lower hull meet.
13. Append Llower to Lupper , and call the resulting list L.
14. return L
,
FU Berlin, Computational Geometry:, SS 2013
8
More on convex hulls
Algorithm development
Algorithm analysis
..and for the lower hull.
The pseudo-code
p1 , p2 , p10 , p13 , p14
Then we do the same for the
lower convex hull, from right
to left
We remove the first and last
points of the lower convex hull
. . . and concatenate the two
lists into one
p14 , p12 , p8 , p4 , p1
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
9
Convex hulls
More on convex hulls
Algorithm analysis
Algorithm development
Algorithm analysis
Algorithm analysis
Algorithm analysis generally has two components:
proof of correctness
efficiency analysis, proof of running time
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
10
More on convex hulls
Algorithm development
Algorithm analysis
Correctness
Correctness
Are the general observations on which the algorithm is based
correct?
Does the algorithm handle degenerate cases correctly?
Here:
Does the sorted order matter if two or more points have the
same x-coordinate?
What happens if there are three or more collinear points, in
particular on the convex hull?
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
11
Convex hulls
More on convex hulls
Running time
Algorithm development
Algorithm analysis
Efficiency
Identify of each line of pseudo-code how much time it takes, if it is
executed once (note: operations on a constant number of
constant-size objects take constant time)
Consider the loop-structure and examine how often each line of
pseudo-code is executed
Sometimes there are global arguments why an algorithm is more
efficient than it seems, at first
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
12
Convex hulls
More on convex hulls
Running time
Algorithm development
Algorithm analysis
The pseudo-code
Algorithm ConvexHull(P)
Input. A set P of points in the plane.
Output. A list containing the vertices of CH(P) in clockwise order.
1. Sort the points by x-coordinate, resulting in a sequence
p1 , . . . , pn .
2. Put the points p1 and p2 in a list Lupper , with p1 as the first
point.
3. for i ← 3 to n
4.
do Append pi to Lupper .
5.
while Lupper contains more than two points and the
last three points in Lupper do not
make a right turn
6.
do Delete the middle of the last three points from
Lupper .
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
12
Convex hulls
More on convex hulls
Algorithm development
Algorithm analysis
Running time
Efficiency
The sorting step takes O(n log n) time
Adding a point takes O(1) time for the adding-part. Removing
points takes constant time for each removed point. If due to an
addition, k points are removed, the step takes O(1 + k) time
Total time:
n
O(n log n) + ∑ O(1 + ki )
i=3
if ki points are removed when adding pi
Since ki = O(n), we get
n
O(n log n) + ∑ O(n) = O(n2 )
i=3
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
12
Convex hulls
More on convex hulls
Running time
Algorithm development
Algorithm analysis
Efficiency
Global argument: each point can be removed only once from the
upper hull
This gives us the fact:
n
∑ ki ≤ n
i=3
Hence,
n
O(n log n) + ∑ O(1 + ki ) = O(n log n) + O(n) = O(n log n)
i=3
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
12
More on convex hulls
Algorithm development
Algorithm analysis
Theorem
Final result
The convex hull of a set of n points in the plane can be computed
in O(n log n) time, and this is optimal
Computational Geometry
Lecture 1: Introduction and Convex Hulls
,
FU Berlin, Computational Geometry:, SS 2013
13
Chan’s algorithm
Divide & Conquer (or Graham’s scan + Jarvis march)
and the power of super-exponential search
,
FU Berlin, Computational Geometry:, SS 2013
14
it! We do not know the value of h in advance, so it would seem that we are stuck bef
Partition
et started.
We will (divide)
deal with this conundrum later, but, just to get the ball rolling, supp
at we had an estimate for h, call it h∗ , whose value is at least as large as h, but not too
say h ≤ h∗ ≤ h2 ). If we run the above partitioning process using h∗ rather than h, th
g time to compute all the mini-hulls is O(n log h∗ ) = O(n log h).
P
Original point set
(a)
mini-hulls
Partition (h∗ = 8) and mini-hulls
(b)
Figure 1: Partition and mini-hulls.
titioning of the points is done by any arbitrary method (e.g., just break the input up into ,
FU Berlin,
Computational
Geometry:,
SS 2013
15 Fi
roughly
h∗ ).
Of course,
the
resulting mini-hulls might overlap one another (see
K
Merge mini-hulls
Dave Mount
CMSC 754
kth stage of Jarvis’s
algorithm
k-th step
Jarvis’s algorithm on mini-hulls
q3
tan
q2
bi
se
q1
q4
pk
pk−1
(c)
merge the mini-hulls.
(a)
Figure 2: Using Jarvis’s algori
,
FU Berlin, Computational Geometry:, SS 2013
16
Assignment 2
http:
//page.mi.fu-berlin.de/panos/cg13/exercises/u02.pdf
Due: 22/04!
,
FU Berlin, Computational Geometry:, SS 2013
17