CS 7: Assignment Douglas R. Lanman 8 Ma Problem : Evaluating Conve Polgons This write-up presents several simple algorithms for determining whether a given set of twodimensional points defines a conve polgon (i.e., a conve hull). In Section., we introduce the notion of regular polgons and provide eamples of both conve and non-conve point sets. Section. presents an algorithm for ordering a set of points such that a counterclockwise traversal defines the interior of the conve hull. In Section., we appl this method to determine whether a given set of points are the vertices of a conve hull. Finall, in Section., we derive a lower bound for the worst-case running time of all such vete-ordering algorithms..: Eamples of Conve and Non-conve Polgons Give an eample of a conve polgon and a non-conve polgon with,, and vertices. A general n-sided polgon can be described b an ordered sequence of vertices P = {p, p,..., p n }. We define the interior of the polgon to be to the left of the line from p i mod n to p i+ mod n for i n. As a result, the order of vertices within P matters. For this problem, however, we will concentrate on a simple case for P consisting of the set of regular polgons. A regular n-sided polgon has vertices evenl-distributed around a circle of radius r as follows. p i = ( i, i ) = ( r cos ( πi n ), r sin ( πi n )), for i n, r > () Ever regular polgon is also a conve polgon. In general, a conve polgon is one whose verte list P satisfies the following condition: the angle inside P formed b (p i mod n, p i, p i+ mod n ) is strictl less than 8 degrees, i [, n ]. In other words, when traveling along the sequence (p i mod n, p i, p i+ mod n ) one alwas takes a left turn at p i. Eamples of regular, conve polgons for n = {,, } are shown in Figure. A non-conve polgon can be defined as one which contains right turns or internal angles greater than or equal to 8 degrees. In general, if we append the verte (, ) to an regular polgon P............ (a) square (b) pentagon (c) heagon Figure : Eamples of conve polgons
Assignment CS 7 Douglas R. Lanman............ (a) n = (b) n = (c) n = Figure : Eamples of non-conve polgons defined b Equation, then we will create a non-conve polgon P. Eamples of the resulting non-conve polgons are shown in Figure..: Finding a Counterclockwise Ordering Given a set of points {p, p,..., p n } which are the vertices of a conve polgon in arbitrar order, find an ordering σ of the points such that P = {p σ(), p σ(),..., p σ(n ) } is a counterclockwise traversal. Your algorithm should have a running time of O(n log n). High-level Description: While the problem statement originall called for a divide-and-conquer solution, such an approach is needlessl complicated as we can assume for this problem that the input consists of a set of conve points in a randoml-shuffled order. As a result (and with permission from the TA), I propose the following simple solution derived from Graham s Scan [, ]. The inputs to Order-Vertices are the verte coordinates (, ) and the output is the counterclockwise ordering σ. The pseudocode description is provided below. Order-Vertices(, ) Find bottom-left verte. n length[] i for j to n do if [j] < [i] then i j 7 if [j] = [i] and [j] < [i] 8 then i j 9 Determine polar angles. for j to n do if [j] = [i] then if [j] > [i] then θ[j] = π/ else θ[j] = π/ else θ[j] = arctan ( [j] [i] [j] [i] sort θ into monotonicall-increasing order to find σ 7 return σ )
Assignment CS 7 Douglas R. Lanman 9 9 8. 8. 7 7...... (a) input point set (b) ordered point set Figure : Eample of output produced b Order-Vertices for n = Proof of Correctness: Before we prove the overall correctness of Order-Vertices, let s verif the outcome of each section independentl. In the first section (on lines -8), we determine the left-most verte (breaking ties in favor of lower points). The correctness of this section is immediate a linear search through the coordinates returns the desired point. In the second section (on lines 9-), we determine the polar angles each verte makes with respect the the reference verte in the lower-left corner. In general, we can use the following simple trigonometric relationship to determine the angle θ (from the -ais) between points p i and p j. ( ) [j] [i] θ[j] = arctan () [j] [i] The correctness of this solution is also immediate. Similarl, we assume that the sorting routine (on line ) correctl arranges the polar angles into monotonicall-ascending order. Note that we handle the special case of the coordinates of the vertices being equal (e.g., Figure (b)) eplicitl on lines -. In conclusion, the individual sections of Order-Vertices are correct. To prove the overall correctness, consider the eample in Figure (a). If we draw a line from verte (the left-most verte) along the -ais, and if we rotate this line to the right, then it will touch each point in counterclockwise order. This is due to the fact that the input set is conve. As a result, the polar angles of each point uniquel determine their correct order in the output σ. Analsis of Running Time: The asmptotic worst-case running time of Order-Vertices is O(n log n). We can prove this directl b analzing the pseudocode. On lines -8 we search for the left-most verte (breaking ties in favor of lower points). Since this section onl involves constant time comparisons and assignments, then the running time is O(n) corresponding to the order of iterations of the for loop on line. Similarl, the section of code on lines 9- is used to determine the set of polar angles (with respect to the lower-left verte). Once again, this section involves constant time operations and has a running time of O(n) due to the for loop on line. In order to produce the desired counterclockwise traversal σ, we must sort the points b their polar angles. This can be accomplished using a variet of sorting algorithms presented in [] with a running time of O(n log n). As a result, the asmptotic worst-case running time of Order- Vertices is O(n log n). (QED)
Assignment CS 7 Douglas R. Lanman.: Determining if a Point Set is Conve Etend our algorithm to solve the decision problem: given a set of points {p, p,..., p n }, decide whether the are the vertices of a conve polgon. High-level Description: Initiall, one might tr solving this problem using the approach outlined in Problem.- in []. That is, after ordering the points using Order-Vertices, check that each consecutive pair of edges has an internal angle less than 8 degrees (i.e., that we alwas make a left turn when traversing the ordered verte list). While this would work for simple polgons with no self-intersections, one could imagine a comple polgon which intersects itself resulting in a non-conve polgon with all left turns. As a result, we must improve upon this naive approach. Recall the following classic result from planar geometr: the sum of the internal angles of a simple n-sided polgon is equal to (n )π radians for n []. Given this constraint, we can easil verif that a polgon is simple (i.e., is not self-intersecting) and, as a result, we can check that the internal angles are all less than 8 degrees (and that this constraint is satisfied). As a result, we propose Test-Conve below to determine if a set of points represents a conve polgon. The inputs to Test-Conve are the verte coordinates (, ) and the output is a boolean indicating whether or not the points define a conve polgon. (Note that subscript addition is performed modulo n in this pseudocode description.) Test-Conve(, ) σ Order-Vertices(, ) reorder (, ) using σ n length[] A for i to n ( ) ([i + ] [i]) ([i + ] [i]) do C det ([i + ] [i]) ([i + ] [i]) 7 if C 8 then return false 9 a ([i + ] [i]) + ([i + ] [i]) b ([i ] [i]) + ([i ] [i]) c ([i + ] ( [i ]) ) + ([i + ] [i ]) A A + arccos a +b c if A = (n )π then return true else return false ab Proof of Correctness: The correctness of Test-Conve follows from the definition of a conve polgon. That is, a conve polgon must have internal angles strictl less than 8 degrees and the sum of its internal angles must equal (n )π radians. As a result, we must prove that our method for determining the internal angles and their sum is correct. Recall that the cross product can be used to determine how consecutive line segments turn at a point p i, as shown in [] on pages 9 and 9. In general, the sign of the cross product C, where ( ) ([i + ] [i]) ([i + ] [i]) C = det () ([i + ] [i]) ([i + ] [i]) is positive if and onl if the edge turns to the left at p i []. As a result, we check this condition for each verte on lines -8 (returning false if this test fails).
Assignment CS 7 Douglas R. Lanman.. p i+ a c θ b p i.. (a) Law of Cosines illustration p i.8 9. 8. 7... (b) Eample used to prove Ω(n log n) Figure : Law of Cosines used in Test-Conve and the eample from Section.. Assuming that ever pair of consecutive edges makes a left turn, we still must verif that the polgon is simple. We can do this b storing the running sum of the internal angles. In order to compute the internal angles, we simpl appl the Law of Cosines. For the eample shown in Figure (a), the Law of Cosines requires c = a + b ab cos θ () As a result, the internal angle θ is given b ( a + b c ) θ = arccos ab () This operation is performed on lines 9- and the constraint that the sum of internal angles equals (n )π is verified on line. As a result, Test-Conve correctl determines if the input point set represents a conve polgon. Analsis of Running Time: The running time of Test-Conve is clearl O(n log n). We can prove this b direct inspection. Lines,, - all involve constant time operations. Similarl, lines - also involve constant time operations (assuming arccos is implemented b a constant-time lookup table). As a result, the asmptotic worst-case running time for lines - is determined b the for loop on line and is O(n). Since the reordering on line can be accomplished in O(n) given σ, the overall asmptotic worst case running time is due to the call to Order-Vertices on line and is O(n log n). (QED).: A Lower Bound on all Verte-Ordering Algorithms Prove that ever verte-ordering algorithm whose comparison operations are binar (i.e., an test performed has onl two outcomes) must have compleit Ω(n log n) in the worst case. A simple eample illustrates that an algorithm which determines the order of vertices in a conve polgon (e.g., the approach presented in Section.) must have compleit Ω(n log n) in the worst case. Consider the point set shown in Figure (b). In this eample, we have ten points on the parabola given b =. The desired counterclockwise ordering is given b sorting the vertices b their coordinate. Recall Theorem 8. from []: an comparison sort requires Ω(n log n) in
Assignment CS 7 Douglas R. Lanman the worst case. As a result, the verte-ordering task must also have a worst-case compleit of Ω(n log n). As presented in Chaper 8 of [], this lower bound can be understood b considering the decision tree for the verte-ordering task. Unlike one-dimensional (i.e., arra) sorting algorithms, the solution of the verte-ordering task is not unique; that is, the sorted vertices can appear in an cclic permutation. Regardless, we can alwas select a verte to be first in the list. Afterwards, there remaining n vertices which must be ordered. As with arra sorting, the decision tree must contain all (n )! permutations as a leaf. Following the derivation of [], the height h of the decision tree must satisf the following relationship. h (n )! h log ((n )!) = Ω(n log n) () In conclusion, we have demonstrated that all verte-ordering algorithms must have worst-case compleit Ω(n log n) b the eample in Figure (b). It is important to note that this eample was first proposed b Shamos and Preparata []. (QED) Conclusion This write-up reviewed the notion of conve polgons and presented several algorithms for determining whether a given point set represents one. The algorithm presented in Section. (and its analsis in Section.) is of general utilit, however this document has not eplored the larger issue of determining a conve hull for an arbitrar point set. This more general problem is of central importance to the field of computation geometr. Interested readers are referred to Shamos and Preparata [] for a detailed presentation of conve hull algorithms. In addition, see [] for a divide-and-conquer solution to Problem.. References [] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. The MIT Press and McGraw-Hill,. [] Jeff Erickson. Conve hulls. http://compgeom.cs.uiuc.edu/ jeffe/teaching/7/notes/ -convehull.pdf,. [] Michael I. Shamos and Franco P. Preparata. Computational Geometr: An Introduction. Springer-Verlag, 98. [] Wikipedia. Polgon. http://en.wikipedia.org/wiki/polgon,.