13.7 Convex Hulls 739
Divide-and-Conquer Method
A standard paradigm in computer science is divide and conquer. The idea is to take
a problem, divide it into two smaller problems of the same type, solve the smaller
problems, and merge the results to construct the solution to the original problem. If
the problem has n inputs and T
n
is the time it takes to solve the problem, the division
into two smaller problems, each with half the inputs, leads to the recursion formula
T
n
= 2T
n/2
+ M
n
. Each smaller problem has (approximately) n/2 inputs and takes
T
n/2
time to solve (by definition of T
k
). The quantity M
n
represents the time it takes
to merge the solution to the two smaller problems. If M
n
takes linear time, O(n),
then it can be shown that the solution T
n
is O(n log n). Recurrences of this form are
discussed in detail in Cormen, Leiserson, and Rivest (1990). The divide-and-conquer
method applied to convex hulls turns out to have a linear-time merge, so the convex
hull algorithm for a set of n points takes O(n log n) time.
As in the incremental construction of the convex hull, the input points are sorted
according to the same scheme: (x
0
, y
0
)<(x
1
, y
1
) when x
0
<x
1
or when x
0
= x
1
and
y
0
<y
1
. We also make no assumptions about the structure of the point set. Just as
in the incremental hull algorithm, the input points are sorted and duplicates are
removed. The initial pseudocode is
ConvexPolygon DividAndConquerHull(int n, Point V[n])
{
Sort(n, V);
RemoveDuplicates(n, V); // n can decrease, V has contiguous elements
ConvexPolygon hull;
GetHull(0,n-1,V,hull);
return hull;
}
The recursive construction occurs in GetHull. Its structure is shown below. The
values
i0 and i1 are the first and last indices of a subset of points whose convex hull
must be computed.
void GetHull(int i0, int i1, Point V[], ConvexPolygon& hull)
{
int quantity = i1 - i0 + 1;
if (quantity > 1) {
// middle index of input range
int mid = (i0 + i1) / 2;
// find hull of subsets (mid - i0+1>=i1-mid)
ConvexPolygon LHull, RHull;
GetHull(i0, mid, V, LHull);