for i = 3 to n do
------------------------------- 33 ------------------------------ Hull(p0, p1, ..., pi) 33-2
Computational Geometry
P2
33.3 Finding the convex hull P1
P0
The convex hull of a set Q of points is the smallest
convex polygon P for which each point in Q is Stack
either on the boundary of P or in its interior. Hull(p0, p1, p2)
P3
P1
P0
Hull(p0, p1, p2, p3)
* A simple O(n3) solution: if ab is a convex edges,
all other points are at the same side. P4
Graham’s scan (Output the vertices in
counterclockwise order.) P3
always make P1
counter-
clockwise a left-turn P0
33-1a
Hull(p0, p1, ..., p4)
33-3 33-4
P8
P7
P6
P5 P5
P3 P3
P1 P1
P0 P0
Hull(p0, p1, ..., p5)
P6 P9
P5 P6
P3 P5
P1 P3
P0 P1
Hull(p0, p1, ..., p6) P0
P7
P6
P5 P10
P3 P3
P1 P1
P0 P0
Hull(p0, p1, ..., p7)
33-5 33-6
O(n)
O(nlg n)
(How? See 33-1b)
function compare(p0, a, b)
0: a b
1: a > b
O(n)
multi-pop + push (amortized)
Top Pi
Next-to-top
(How? See 33-1a)
Time complexity:
Line 1: O(n) find P0
Line 2: O(nlg n) P1, P2, P3, ..., Pm (m n-1)
Line 3~6: O(1)
Line 8~9: O(n) Amortized
(at most n PUSH at most n POP)
Line 10: O(n) * if the sorted order * take any as p0:
O(nlg n) is given : O(n) 33-6x
* Correctness: See textbook.
The end!
33-7 33-8
Jarvis’s March (using a technique known as 33-7x
package wrapping or gift wrapping) left chain right chain
(Find a correct edge each time !)
Step 1: Find the lowest point x and the highest
point y. Smallest
Step 2: Compute the right chain negative x-axis
(p0=x, p1, …, pk=y).
Step 3: Compute the left chain
(pk=y, pk+1, …, ph=x).
33-8x
* In O(1) time, we can compare the polar angles of
two points. (How ??? See Section 33.1) 0: a b
function compare(p0, a, b): 1: >
a b
* In O(n) time, we can determine the point with Smallest
smallest (or largest) polar angle with respect to
a given point. (How? See 33-1b)
* Since each computation of pi take O(n) time,
T(n)=O(nh).
# of convex hull vertices
Homework: None. conclude
* In the worst case, h=n and thus T(n)=O(n2).
* Jarvis’s march is better than Graham’s scan if
h=o(lg n).