CMSC 754 Computational Geometry1

David M. Mount Department of Computer Science University of Maryland Fall 2005

1Copyright, David M. Mount, 2005, Dept. of Computer Science, University of Maryland, College Park, MD, 20742. These lecture notes were prepared by David Mount for the course CMSC 754, Computational Geometry, at the University of Maryland. Permission to use, copy, modify, and distribute these notes for educational purposes and without fee is hereby granted, provided that this copyright notice appear in all copies.

Lecture Notes 1 CMSC 754 Proof: the points according to x-coordinates can be done by any efficient sorting in O(n log n) time. Let Di denote the number of points that are popped (deleted) on processing pi. Because each orien- tation test takes O(1) time, the amount of time spent processing pi is O(Di + 1). (The extra +1 is for the last point tested, which is not deleted.) Thus, the total running time is proportional to

n n

(Di + 1) = n + Di. i=1 i=1 ! !

To bound i Di, observe that each of the n points is pushed onto the stack once. Once a point is deleted it can never be deleted again. Since each of n points can be deleted at most once, D n. Thus i i ≤ after sorting," the total running time is O(n). Since this is true for the lower hull as well, the total time is O(2n) = O(n). "

Convex Hull by Divide-and-Conquer: As with sorting, there are many different approaches to solving the convex hull problem for a planar point set P . Next we will consider another O(n log n) algorithm, which is based on the divide-and-conquer design technique. It can be viewed as a generalization of the famous MergeSort (see the text by Cormen, Leiserson, Rivest and Stein, CLRS). Here is an outline of the algorithm. It begins by sorting the points by their x-coordinate, in O(n log n) time. The remainder of the algorithm is shown in the code section below.

Divide-and-Conquer Convex Hull (1) If P 3, then compute the convex hull by brute force in O(1) time and return. | | ≤ (2) Otherwise, partition the point set P into two sets A and B, where A consists of half the points with the lowest x-coordinates and B consists of half of the points with the highest x-coordinates.

(3) Recursively compute HA = conv(A) and HB = conv(B).

(4) Merge the two hulls into a common convex hull, H, by computing the upper and lower tangents for HA and HB and discarding all the points lying between these two tangents.

upper tangent b

B a B A A

lower tangent (a) (b)

Figure 12: Computing the lower tangent.

The asymptotic running time of the algorithm can be expressed by a recurrence. Given an input of size n, consider the time needed to perform all the parts of the procedure, ignoring the recursive calls. This includes the time to partition the point set, compute the two tangents, and return the final result. Clearly the first and third of these steps can be performed in O(n) time, assuming a linked list representation of the hull vertices. Below we will show that the tangents can be computed in O(n) time. Thus, ignoring constant factors, we can describe the running time by the following recurrence.

1 if n 3 T (n) = ≤ n + 2T (n/2) otherwise. #

Lecture Notes 15 CMSC 754 This is the same recurrence that arises in Mergesort. It is easy to show that it solves to T (n) O(n log n) (see ∈ CLRS). All that remains is showing how to compute the two tangents. One thing that simplifies the process of computing the tangents is that the two point sets A and B are separated from each other by a vertical line (assuming no duplicate x-coordinates). Let’s concentrate on the lower tangent, since the upper tangent is symmetric. The algorithm operates by a simple “walking” procedure. We initialize a to be the rightmost point of HA and b is the leftmost point of HB. (These can be found in linear time.) Lower tangency is a condition that can be tested locally by an orientation test of the two vertices and neighboring vertices on the hull. (This is a simple exercise.) We iterate the following two loops, which march a and b down, until they reach the points lower tangency.

Finding the Lower Tangent

LowerTangent(HA, HB ) :

(1) Let a be the rightmost point of HA.

(2) Let b be the leftmost point of HB .

(3) While ab is not a lower tangent for HA and HB do

(a) While ab is not a lower tangent to HA do a = a 1 (move a clockwise). − (b) While ab is not a lower tangent to HB do b = b + 1 (move b counterclockwise). (4) Return ab.

Proving the correctness of this procedure is a little tricky, but not too bad. Check O’Rourke’s book out for a careful proof. The important thing is that each vertex on each hull can be visited at most once by the search, and hence its running time is O(m), where m = H + H A + B . This is exactly what we needed to get | A| | B| ≤ | | | | the overall O(n log n) running time.

Computational Complexity of Planar Convex Hulls: We have seen two planar convex hull algorithms that take O(n log n) time. This suggests the question of whether this is optimal? Recall that sorting a sequence of n numbers requires O(n log n) time, assuming that the sorting algorithm bases its decisions on comparisons. (Recall that there are sorting algorithms, radix sort for example, which work faster, but use tricks based on the numeric representations rather than comparisons.) Assuming that the convex hull is to be output as a cyclic order of vertices on the hull, it is not hard to prove that there is a linear-time reduction from sorting to the convex hull problem. (You should think about this.) Therefore, assuming that the convex hull algorithm bases its decisions on discrete binary tests (such as orientation tests), the Ω(n log n) lower bound for sorting holds for convex hulls as well. This argument is based on the assumption that there are indeed Ω(n) points that appear on the hull. What if there are fewer? Let h n denote the actual number of points on the convex hull. Can we devise a faster algorithm ≤ whose running time is sensitive to h? Indeed, there exists an O(n log h) time algorithm. (There are actually two such algorithms, one by Kirkpatrick and Seidel, which is relatively complex and a much simpler one which was discovered much later by Chan.) Kirkpatrick and Seidel proved that this algorithm is asymptotically optimal under the assumption that the algo- rithm bases its output on binary decisions, such as orientation tests. Thus, this is in some sense the “ultimate” planar convex hull algorithm.

Lecture 4: Line Segment Intersection

Reading: Chapter 2 in the 4M’s.

Geometric intersections: One of the most basic problems in computational geometry is that of computing intersec- tions. Intersection computation in 2- and 3-space is basic to many different application areas.

Lecture Notes 16 CMSC 754