Up: Reductions


8.2.1 reduce sorting to convex hull

Definition: A sequence of points Si = (xi, yi), for i = 1 to k is a convex polygon (in counterclockwise order) if, in the sequence T of size k+2 consisting S1, ..., Sk, S1, S2, each adjacent triple of points Ti, Ti+1, Ti+2 represents a a left turn, for i = 1 to k. The convex hull of a point set P is a subset S of P such that S is a convex polygon and every point of P-S is interior to S.

Problem Convex Hull(S)
Input: S is a sequence of points (xi,yi) in the plane.
Output: permute S and return k such that S1, ... Sk is the convex hull of S.

The reduction of Sorting problem to Convex Hull problem:
Reduction sortByConvexHull(S){// S is a sequence of numbers.

  1. for i in 1..n, set P[i] = point(S[i], S[i]2); /* in other words, set P = { (x, x2) | x in S } */
  2. k = convexHull(P); /* We know in advance that k will be size(P).*/
  3. for i in 1..n, set S[i] = P[i].first; /* first = the x of a (x, x2) pair. */
}