Title: Convex Sets
1Convex Sets Concave Sets
A planar region R is called convex if and only if
for any pair of points p, q in R, the line
segment pq lies completely in R.
Otherwise, it is called concave.
2An Example
4
1
2
3
3Convex Hull
The convex hull CH(Q) of a set Q is the smallest
convex region that contains Q.
Rubber band
When Q is finite, its convex hull is the unique
convex polygon whose vertices are from Q and
that contains all points of Q.
4The Convex Hull Problem
Input a set P p , p , , p of points
1 2 n
Output a list of vertices of CH(P) in
counterclockwise order.
Example
5Edges of a Convex Hull
For every edge both endpoints p, q ? P.
q
p
6Floating Arithmetic is not Exact!
Nearly colinear points p, q, r.
q
r
p
All three accepted as edges!
Not robust the algorithm could fail with small
numerical error.
7Polar Angle
y
q
polar angle
p
x
r
8An Example of Graham Scan
Sort by polar angle.
(with the minimum y-coordinate)
How to break a tie?
Labels are in the polar angle order.
(What if two points have the same polar angle?)
9Stack Initialization
p
p
6
9
p
p
7
4
p
11
p
p
8
p
5
10
p
3
p
p
2
1
p
0
10 p
p
6
9
p
p
7
4
p
11
S
p
p
8
p
5
10
p
p
3
3
p
1
p
p
2
1
p
0
p
0
11 p
p
6
9
p
p
7
4
p
11
S
p
p
8
p
5
10
p
p
4
3
p
1
p
p
2
1
p
0
p
0
12 p
p
6
S
9
p
p
7
4
p
11
p
p
p
5
8
p
5
10
p
p
4
3
p
1
p
p
2
1
p
0
p
0
13 p
p
6
S
9
p
p
7
4
p
11
p
p
6
p
8
p
5
10
p
p
4
3
p
1
p
p
2
1
p
0
p
0
14 S
p
p
8
p
6
9
p
p
p
7
4
7
p
11
p
p
6
p
8
p
5
10
p
p
4
3
p
1
p
p
2
1
p
0
p
0
15 S
p
p
6
9
p
p
p
7
4
7
p
11
p
p
6
p
8
p
5
10
p
p
4
3
p
1
p
p
2
1
p
0
p
0
16 S
p
p
10
p
6
9
p
p
4
9
p
p
11
7
p
p
6
p
8
p
5
10
p
p
4
3
p
1
p
p
2
1
p
0
p
0
17 S
p
p
11
p
6
9
p
p
4
9
p
p
11
7
p
p
6
p
8
p
5
10
p
p
4
3
p
1
p
p
2
1
p
0
p
0
18Finish
S
p
p
11
p
6
9
p
p
4
9
p
p
11
p
7
p
8
6
p
p
5
10
p
p
4
3
p
1
p
p
2
1
p
0
p
0
19Grahams Scan
candidates for vertices of CH(P)
vertices of CH(P) in the counter- clockwise orde
r.
20The Graham Scan Algorithm
Graham-Scan(P) let p be the point in P
with minimum y-coordinate let ? p , p ,
, p ? be the remaining points in P
sorted in counterclockwise order by polar
angle around p . TopS ? 0
Push(p , S) Push(p , S) Push(p ,
S) for i ? 3 to n ? 1 do
while p makes a nonleft turn from the line
segment determined by
Top(S) and Next-to-Top(S)
do Pop(S) Push(S, p )
return S
0
1 2 n1
0
0
1
2
i
i
21Proof of Correctness
Claim 1 Each point popped from stack S is not a
vertex of CH(P).
Two cases when p is popped
Proof
j
22 Claim 2 Graham-Scan maintains the invariant
that the points on stack S always form the
vertices of a convex polygon in
counterclockwise order.
Proof
Popping a point from S preserves the invariant.
The invariant still holds.
23Correctness of Grahams Scan
Theorem If Graham-Scan is run on a set P of at
least three points, then a point of P is on
the stack S at termination if and only if
it is a vertex of CH(P).
24Running time
operations time / operation total
Sorting 1 O(n lg n)
O(n lg n)
Push n O(1)
?(n)
Pop ? n ? 2 O(1)
O(n)
Why?
The running time of Grahams Scan is O(n lg n).
25Jarvis March
A package wrapping technique
Right chain
Left chain
26Running Time of Jarviss March
Let h be the number of vertices of the convex
hull.
For each vertex, finding the point with the
minimum Polar angle, that is, the next vertex,
takes time O(n).
Comparison between two polar angles can be
done using cross product.
Thus O(nh) time in total.