



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: ADV LG DATA PROCESSG; Subject: COMPUTER SCIENCE AND INFORMATION SYSTEMS; University: University of Florida; Term: Fall 2002;
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!




We are given a set P of n points in the plane. We want to compute something called the convex hull of P. Intuitively, the convex hull is what you get by driving a nail into the plane at each point and then wrapping a piece of string around the nails. More formally, the convex hull is the smallest convex polygon containing the points:
We can also define the convex hull as the largest convex polygon whose vertices are all points in P , or the unique convex polygon that contains P and whose vertices are all points in P. Notice that P might have interior points that are not vertices of the convex hull.
A set of points and its convex hull. Convex hull vertices are black; interior points are white.
Just to make things concrete, we will represent the points in P by their Cartesian coordinates, in two arrays X[1 .. n] and Y [1 .. n]. We will represent the convex hull as a circular linked list of vertices in counterclockwise order. if the ith point is a vertex of the convex hull, next[i] is index of the next vertex counterclockwise and pred[i] is the index of the next vertex clockwise; otherwise, next[i] = pred[i] = 0. It doesn’t matter which vertex we choose as the ‘head’ of the list. The decision to list vertices counterclockwise instead of clockwise is arbitrary. To simplify the presentation of the convex hull algorithms, I will assume that the points are in general position, meaning (in this context) that no three points lie on a common line. This is just like assuming that no two elements are equal when we talk about sorting algorithms. If we wanted to really implement these algorithms, we would have to handle colinear triples correctly, or at least consistently. This is fairly easy, but definitely not trivial.
Computing the convex hull of a single point is trivial; we just return that point. Computing the convex hull of two points is also trivial. For three points, we have two different possibilities — either the points are listed in the array in clockwise order or counterclockwise order. Suppose our three points are (a, b), (c, d), and (e, f ), given in that order, and for the moment, let’s also suppose that the first point is furthest to the
left, so a < c and a < f. Then the three points are in counterclockwise order if and only if the line ←−−−−−−→ (a, b)(c, d) is less than the slope of the line
(a, b)(e, f ):
counterclockwise ⇐⇒ d − b c − a
f − b e − a
Since both denominators are positive, we can rewrite this inequality as follows:
counterclockwise ⇐⇒ (f − b)(c − a) > (d − b)(e − a)
This final inequality is correct even if (a, b) is not the leftmost point. If the inequality is reversed, then the points are in clockwise order. If the three points are colinear (remember, we’re assuming that never happens), then the two expressions are equal.
(a,b)
(c,d)
(e,f)
Three points in counterclockwise order.
Another way of thinking about this counterclockwise test is that we’re computing the cross- product of the two vectors (c, d) − (a, b) and (e, f ) − (a, b), which is defined as a 2 × 2 determinant:
counterclockwise ⇐⇒
c − a d − b e − a f − b
We can also write it as a 3 × 3 determinant:
counterclockwise ⇐⇒
1 a b 1 c d 1 e f
All three boxed expressions are algebraically identical. This counterclockwise test plays exactly the same role in convex hull algorithms as comparisons play in sorting algorithms. Computing the convex hull of of three points is analogous to sorting two numbers: either they’re in the correct order or in the opposite order.
Perhaps the simplest algorithm for computing convex hulls simply simulates the process of wrapping a piece of string around the points. This algorithm is usually called Jarvis’s march, but it is also referred to as the gift-wrapping algorithm. Jarvis’s march starts by computing the leftmost point , i.e., the point whose x-coordinate is smallest, since we know that the left most point must be a convex hull vertex. Finding clearly takes linear time.
of p, including p itself, and the points to the right of p, by comparing x-coordinates. Recursively compute the convex hulls of L and R. Finally, merge the two convex hulls into the final output. The merge step requires a little explanation. We start by connecting the two hulls with a line segment between the rightmost point of the hull of L with the leftmost point of the hull of R. Call these points p and q, respectively. (Yes, it’s the same p.) Actually, let’s add two copies of the segment pq and call them bridges. Since p and q can ‘see’ each other, this creates a sort of dumbbell-shaped polygon, which is convex except possibly at the endpoints off the bridges.
p
q
p
q
p
q
q
p p
q q
p
Merging the left and right subhulls.
We now expand this dumbbell into the correct convex hull as follows. As long as there is a clockwise turn at either endpoint of either bridge, we remove that point from the circular sequence of vertices and connect its two neighbors. As soon as the turns at both endpoints of both bridges are counter-clockwise, we can stop. At that point, the bridges lie on the upper and lower common tangent lines of the two subhulls. These are the two lines that touch both subhulls, such that both subhulls lie below the upper common tangent line and above the lower common tangent line. Merging the two subhulls takes O(n) time in the worst case. Thus, the running time is given by the recurrence T (n) = O(n) + T (k) + T (n − k), just like quicksort, where k the number of points in R. Just like quicksort, if we use a na¨ıve deterministic algorithm to choose the pivot point p, the worst-case running time of this algorithm is O(n^2 ). If we choose the pivot point randomly, the expected running time is O(n log n). There are inputs where this algorithm is clearly wasteful (at least, clearly to us). If we’re really unlucky, we’ll spend a long time putting together the subhulls, only to throw almost everything away during the merge step. Thus, this divide-and-conquer algorithm is not output sensitive.
A set of points that shouldn’t be divided and conquered.
Our third convex hull algorithm, called Graham’s scan, first explicitly sorts the points in O(n log n) and then applies a linear-time scanning algorithm to finish building the hull.
We start Graham’s scan by finding the leftmost point , just as in Jarvis’s march. Then we sort the points in counterclockwise order around. We can do this in O(n log n) time with any comparison-based sorting algorithm (quicksort, mergesort, heapsort, etc.). To compare two points p and q, we check whether the triple , p, q is oriented clockwise or counterclockwise. Once the points are sorted, we connected them in counterclockwise order, starting and ending at. The result is a simple polygon with n vertices.
l
A simple polygon formed in the sorting phase of Graham’s scan.
To change this polygon into the convex hull, we apply the following ‘three-penny algorithm’. We have three pennies, which will sit on three consecutive vertices p, q, r of the polygon; initially, these are and the two vertices after. We now apply the following two rules over and over until a penny is moved forward onto `:
The ‘three-penny’ scanning phase of Graham’s scan.
Whenever a penny moves forward, it moves onto a vertex that hasn’t seen a penny before (except the last time), so the first rule is applied n − 2 times. Whenever a penny moves backwards, a vertex is removed from the polygon, so the second rule is applied exactly n − h times, where h is as usual the number of convex hull vertices. Since each counterclockwise test takes constant time, the scanning phase takes O(n) time altogether.
The last algorithm I’ll describe is an output-sensitive algorithm that is never slower than either Jarvis’s march or Graham’s scan. The running time of this algorithm, which was discovered by
for some integer k. We can rewrite this as a geometric series:
O(n log 3 + 2n log 3 + 4n log 3 + · · · + 2kn log 3).
Since any geometric series adds up to a constant times its largest term, the total running time is a constant times the time taken by the last iteration, which is O(n log h). So Chan’s algorithm runs in O(n log h) time overall, even without the little birdie.