Graham’s Scan

1 Introduction

A central problem in which has inspired much of the initial development of the subject is the problem of computing the of a finite set of points. The computation of the convex hull is of importance in several application areas.

2 Definitions:

A set S in Ed is said to be convex if the line segment joining any two of its points lies entirely inside the set.

The convex hull of S is the smallest convex set that contains S.

We focus on the case when S consists of a finite set of points. Intuitively, the structure of the hull is a . The following theorem confirms this intuition.

Theorem [McMullen-Shepherd]: A convex hull of a finite set of points S is a convex polytype; con- versely, a convex polytype is the convex hull of a finite set of points S.

In particular in two dimensions the convex hull is a . The vertices of this polygon are the extreme points; the points that lie inside the convex polygon are called interior points. The part of the boundary that goes clockwise from the left extreme point to the right extreme point is called the upper hull; the part that goes counterclockwise between the same two terminal points is called the lower hull (see Fig. 1).

Figure 1: The convex hull of a set of points

3 Algorithms in 2d

We first look at convex hull algorithms in two dimensions.

The goal of any convex hull algorithm is to determine the extreme vertices and output them in the order in which they appear on the hull boundary.

1 4 Brute-force algorithm

A brute-force approach is to consider all possible triangles formed by three input points and delete the points that lie in the interior of these triangles since these cannot lie on the hull boundary. Eventually we are left only with the extreme vertices which we sort in angular order about some point internal to the convex hull.

The worst-case complexity of this method is O(n4), since we may have to check for nC3(O(n3)) triangles and for each triangle on three points checking whether the other n − 3 points are interior to this triangle requires O(n) time.

4.1 Graham’s algorithm: Graham’s algorithm provides a more efficient way of identifying the extreme vertices. The steps of this algorithm are as below.

Step1 : Choose a point O, interior to the convex hull

This is done by taking a triangle of three points and letting O be the centroid of this triangle. A triangle of three points is found by choosing any two points and letting the third point be one that does not lie on the line joining the first two points. If we can’t find such a point then all the points are collinear and the convex hull is just a line segment. Let O be the origin of polar coordinates. Step2 : Sort the input points in angular order about O.

The sort is a lexicographic sort with respect to the polar coordinates r and θ. To resolve a comparison between two points no explicit angle comparison is necessary. If pi and pj are two points we only need check the sign of the area of the triangle △Opipj given by the determinant (except for a factor of 1/2)

10 0

1 p (x) p (y) i i 1 p (x) p (y) j j

Step3 : Do a Graham scan of the polygon obtained by joining the points in their sorted order (assuming that the first point in this list follows the last). The scan starts at a vertex, known to be an extreme point.

This step is at the heart of the algorithm. The idea is to eliminate all reflex vertices. The elimination procedure consists of a local test that takes three consequtive vertices and checks whether the three constitute a left turn or right. If the turn is is left (see Fig. 2) we retain the middle vertex as a tentative convex hull vertex.

pi pi+2

pi+1

Figure 2: A left turn at pi+1

2 If the turn is right (see Fig 3), we eliminate the middle vertex since it is reflex and then backtrack to check the status of the vertices that are tentative extreme vertices. Since the start vertex is an extreme vertex the backtrack does not go past this vertex.

pi+1

pi+2 pi

Figure 3: A right turn at pi+1

5 Data Structure and Pseudo code

To facilitate the forward and backward movement in the list of vertices, it is convenient to implement the list as a circular doubly-linked structure. In the following pseudo code, taken from the book by Preparata and Shamos, an additional Boolean variable is used to detect whether the start vertex has been reached while backtracking or at the completion of the scan.

Procedure GRAHAM-SCAN begin

v:=start; w:=pred[v]; f:=false;

while (next[v]!=start or f=false) do begin if (next[v]==w) then f=true; if (three points v,next(v),next(next(v)) form a left turn) then v:=next(v); else begin delete next(v); v:=pred(v); end;

end; end;

6 Complexity Analysis

The complexity of the first step is in O(n). That of the sorting step is in O(n log n). Graham’s scan takes time proportional to the number of vertices. Let’s see how.

3 In each iteration of loop, if the turn is left we charge the cost of the test to the vertex that is added to the tentative list of extreme vertices; if the turn is right we charge the cost to the vertex that gets deleted. A vertex that is deleted right away (that is, without being added to the tentative list of vertices) initiates a backtrack. Each step in the backrack, except the last, results in a deletion which we charge to the deleted vertex. The cost of the last step is charged to the deleted vertex that initiated the backtrack, which is thus charged twice. So every vertex is charged at most twice.

Thus the complexity of Graham’s scan is in O(n); and that of the algorithm is in O(n log n).

Problem 1 Why is it necessary to sort around a point O interior to the convex hull ? We can avoid this by sorting the points with respect to their x-coordinates and then doing a Graham scan from left to right to give us the upper hull and then from right to left to give us the lower hull.

Problem 2 Show that Graham’s scan cannot be used to compute the convex hull of an arbitrary . How would you compute the convex hull of a simple polygon ?

Problem 3 Let S be a set of n line segments in the plane. Prove that the convex hull of S is exactly the same as the convex hull of the 2n endpoints of the segmnents.

Problem 4 Show that a polygon of minimum perimeter that encloses a set of n points in the plane is necessarily convex. Argue that the minimum perimeter convex polygon that encloses S is identical with the convex hull of S. Does this result hold in 3 dimensions ?

7 Jarvis March

In the Jarvis march technique the idea is to wrap a thread around the points tightly in order to discover all the edges of the convex hull.

This is implemented as follows. Let p1 be a lowest point of the input point set. Consider a horizontal ray that goes from p to ∞. Let p2 be a point in P −{p1} such that that the segment p1p2 makes the smallest angle with the ray p1∞ (see Fig. 4). The point p2 is added to the set of convex hull vertices. We now repeat this with the ray p1∞ that is incident on p1p2. And so on. The process terminates when we return to p1.

                   p3                   p2                        

p1

Figure 4: Illustration of Jarvis March

4 7.1 Analysis The complexity of the algorithm is in O(nh) where h is the number of vertices of the convex hull. Since h can be as large as n, the worst-case complexity of Jarvis’s march is in O(n2).

8 Chan’s Algorithm

Chan’s algorithm is a very clever combination of Graham’s scan and Jarvis’s march whose complexity is in O(n log h). It is also a lot simpler than the “ultimate” convex hull algorithm of Seidel and Kirkpatrick [].

The main idea is simple and elegant. Imagine applying Jarvis’s march to disjoint “blobs” of points of the same size. Indeed Jarvis’s march applies to a single blob - namely, the entire point set.

The smart idea is to let the size of each blob of points be h, where h is the number of edges of the convex hull which is unknown! Assuming that we are clairovoyant enough to know this, let’s see how Jarvis’s march would work on the convex hulls of the n/h blobs of points, each of size h. We proceed exactly as before except that now we have to draw tangents to the convex hulls of the blobs and determine which one makes the smallest angle. Tangents to a convex hull of h points can be found in O(log h) time. Thus the total time that this takes is in O(n/h log h). If we do this h times in order to deterimne the h edges of the convex hull, the time this takes is in O(n log h). Add to this the time that it takes to construct n/h convex hulls each in O(h log h) time. Thus the complexity of the algorithm is in O(n log h).

t Unifortunately, we are not clairovoyant. So, we have to guess the value of h. Let h = 22 for t =1, 2 . . . so that it increases very rapidly. If for a given value of h the process doesn’t return to the starting vertex p1, we t=log log h 2t squre the previous value of h and repeat. Thus the complexity of the algorithm is in O(n Pt=1 n log2 ), log log h which is in O(n log22 ), which in turn is in O(n2log log h) which is in O(n log h).

5