divide and conquer for convex hull

Upload: journal-of-computing

Post on 14-Apr-2018

242 views

Category:

Documents


1 download

TRANSCRIPT

  • 7/30/2019 Divide and Conquer For Convex Hull

    1/8

    Divide and Conquer For Convex HullNgonidzashe Zanamwe

    Abstrac t The divide and concqur approach has many applications in computer science. With this approach a large problem is

    broken down into small and manageable subproblems and each is solved then the solutions are combined to give the grand

    solution to the problem. The problem of finding the convex hull of a polygon can be viewed as one problem that can be solved

    using the divide and conqurer approach. This paper therefore presents a discussion on the divide and conquer for the Convex

    Hull. The paper looks at generic divide and conquer approach before using the approach in finding the convex hull of a polygon.

    The paper presents a step by step procedure for finding the convex hull using the divide and conqurer approach. Also, the

    paper discusses running times of the Convex Hull divide-and-conquer algorithm, advantages and disadvantages of the divide

    and conquers approach for convex hull. The paper concludes by giving other algorithms for solving the convex hull problem

    apart from the divide and conquer.

    Index Termsdivide and conqurer, convex hull, polygon, divide and conqurer for convex hull.

    1 INTRODUCTIONHIS paper presents a discussion on the divide andconquer for Convex Hull (hereinafter referred to asCH). The paper begins by looking at the divide and

    conquer in general before focusing on its applications incomputational geometry, notably to the CH problem.This paper looks at two distinct ways of computing theconvex hull using the divide and conquer approach. Also,the paper discusses running times of Convex Hull divide-and-conquer algorithm, advantages and disadvantages ofthe divide and conquers approach for convex hull. Thepaper concludes by giving other algorithms for solving

    the convex hull problem apart from the divide and con-quer approach.

    1.1 Divide and Conquer algorithms

    Naher and Schmitt [1] note that divide and conquer algo-rithms solve problems by dividing them into instances,solving each instance recursively and merging the corre-sponding results to a complete solution. Further, [1] as-serts that all instances have exactly the same structure asthe original problem and can be solved independentlyfrom each other, and so can easily be distributed over anumber of parallel processes or threads. The divide andconquer approach is presented in Fig. 1 below.

    Figure 1: The divide and conquer approach

    The above diagram shows that if one is faced with a prob-lem of size n, one begins by dividing it into two sub-problems of size n/2 each and then finds a solution to

    each sub-problem then merges the solutions to sub-problems. Similar assertions were made by [5], hepresents the following divide and conquer algorithm:if trivial (small), solve it brute forceelse{ divide into a number of sub-problems;solve each sub-problem recursively;combine solutions to sub-problems;

    }

    1.2 Polygon

    A polygon is defined as a closed shape with more thantwo sides or line segments. A simple polygon has no twoconsecutive edges that are intersecting.

    Ngonidzashe Zanamwe is with the University of Zimbabwe, Zimbabwe

    T

    JOURNAL OF COMPUTING, VOLUME 5, ISSUE 3, MARCH 2013, ISSN (Online) 2151-9617

    https://sites.google.com/site/journalofcomputing

    WWW.JOURNALOFCOMPUTING.ORG 1

  • 7/30/2019 Divide and Conquer For Convex Hull

    2/8

    1.3 Convexity

    A convex is a polygon in which any line joining twopoints within the polygon lies within the polygon. Alex-ander Kolesnikov [7] gives similar definitions of convexi-ty, the first is that, a subset set S of the plane is calledconvex if and only if for any pair of points P, QS theline segment PQ is completely contained in S. The secondgoes as, a set S is convex if it is exactly equal to the inter-section of all the half planes containing it. The figuresbelow show convex and non-convex shapes.

    Figure 2: The convex and non-convex shapes

    1.3. Convex Hull

    Mohammed Nadeem Ahmed and Raghavendra Kyatha[2] establish that the Convex Hull of a set Q of points isthe smallest convex polygon P, for which each point in Qis either on the boundary of P or in its interior. In linewith the above, [4] asserts that the convex hull of a set Sof points, denoted hull(S) is the smallest polygon P forwhich each point of S is either on the boundary or in theinterior of P. Similarly, [7] establishes that the convex hullCH(P) of a finite point set P is the smallest convex poly-gon that contains P. Figure 3 below shows a convex hullP.

    Figure 3: Convex Hull P

    1.4 Intuition:

    If each point is represented by a nail sticking out from aboard and you take a rubber band and lower it over thenails, so as to completely encompass the set of nails, andthen let the rubber band naturally contract, the rubberband will give you the edges of the convex hull of the setof points, and those nails that correspond to a change inslope of the rubber band represent the extreme points ofthe convex hull [4].

    1.5 Convex Hull Problem

    From the above definition it can thus be said that a con-vex hull problem is one where given a set of points orcoordinates one is asked to come up with the smallest

    polygon in which all points are either inside or on theedge of the polygon.

    1.6 Finding the convex hull using divide andconquer

    Souvaine [3]establishes that in order to find the convexhull using a divide-and-conquer approach, one has tofollow these steps:sort points (p1, p2, . . . , pn) by their x-coordinaterecursively find the convex hull ofp1 throughpn/2recursively find the convex hull ofpn/2 +1 throughpnmerge the two convex hulls

    A more detailed algorithm for finding a Convex Hull ispresented in [6]. The algorithm is as follows:

    Hull(S) :If |S|

  • 7/30/2019 Divide and Conquer For Convex Hull

    3/8

    Figure 4: The Points

    2.1.2 Dividing points

    This step involves partitioning the point set S into twosets A and B, where A consists of half the points with thelowest x coordinates and B consists of half of the points

    with the highest x coordinates. For instance, in our exam-ple set A will consist points P1 through P7 whereas set Bwill consist points P8 to P14. This partition is illustratedin figure 5 below.

    Figure 5: The Partition

    Each subset in the above figure has 7 elements. We nowsubdivide each subset into two subsets one with four el-ements and the other with three elements. The algorithmabove stipulates that once the number of points in eachpartition is 3 or less you construct the convex hull, other-wise continue dividing. The final partitions are in figure 6

    below.

    Figure 6: Final Partitions

    2.1.3 Computation of convex hulls

    This involves constructing convex hulls by joining allpoints in each subset in the diagram above. For instance,join P2, P3 and P4 to form triangle P2P3P4, then join P5,P6 and P7 to form triangle P5P6P7, join P8P9, join P10P11and lastly join P12, P13 and P14 to form triangleP12P13P14. This goes on until the points in the last parti-tion are joined as shown in the shown in the diagram be-low. The computed convex hulls are shown in figure 7below.

    Figure 7: Computed Convex Hull

    The convex hulls in the figure above must be combinedby removing the partitions in the reverse order in whichthey were placed; this implies that, remove lines L4 andL5 first. If we remove L4, we then join P1 to hull P2P3P4.If we remove L5, we then join the two lines as illustratedin Figure 8 below.

    Figure 8: Two Lines Joined

    The next stage is to remove lines L2 and L3, if we removeL2, then we join P4 to P7 and P2 to P6. If we remove L 2,then we join P10 to P13 and P9 to P12. After that, we re-move all non-convex edges such as P5P6, P2P3, P10P11and so on. This leaves one with two convex hulls HA andHB as shown in figure 9 below.

    JOURNAL OF COMPUTING, VOLUME 5, ISSUE 3, MARCH 2013, ISSN (Online) 2151-9617

    https://sites.google.com/site/journalofcomputing

    WWW.JOURNALOFCOMPUTING.ORG 3

  • 7/30/2019 Divide and Conquer For Convex Hull

    4/8

    Figure 9: Convex Hulls HA and HB2.1.4 Merging convex hulls

    This step involves merging the two convex hulls into asingle convex hull. Also, this involves computing thelower and upper tangents.

    2.1.4.1 Computing the lower tangent

    One thing that simplifies the process of computing thetangents is that the two point sets HA and HB are sepa-rated from each other by a vertical line (assuming no du-plicate x coordinates). The algorithm for computing thelower tangent operates by a simple ``walking'' procedure.We initialize x to be the rightmost point of convex hullHA and y is the leftmost point of convex hull HB. (Thesecan be found in linear time.)

    Lower tangency is a condition that can be tested locallyby an orientation test of the two vertices and neighbour-ing vertices on the hull. We iterate the following two

    loops, which march x and y down, until they reach thepoints lower tangency. In our example the rightmostpoint of HA is P6 and the leftmost point of HB is P8. Theactual algorithm for computing the tangent is given be-low:LowerTangent(HA , HB ){ x = the rightmost point of HA;

    y = the leftmost point of HB;While (xy != a lower tangent for HA and HB) do{ While (xy != a lower tangent to HA) do

    x = x 1; //move x clockwiseWhile (xy != a lower tangent to HB) do

    y = y + 1; //move y counter-clockwise}

    return xy; }

    In implementing the above algorithm, start with tangentP6P8. This is not a lower tangent for both HA and HB.Move y counter-clockwise and the tangent constructed isP6P9. This is a lower tangent for HA but not even a tan-gent for HB. Move y counter-clockwise and the tangentconstructed is P6P12. This is shown in figure 10 below.

    Figure 10: Computing the Lower Tangent

    The tangent is P6P12 is both a lower tangent for HA andHB, so eliminate the other two tangents to remain withthe figure 11 below.

    Figure 11: Computing the Lower Tangent

    2.1.4.2 Computing the upper tangentThe algorithm for computing the upper tangent againoperates by a simple ``walking'' procedure. We initialize xto be the rightmost point of convex hull HA and y is theleftmost point of convex hull HB. Upper tangency is acondition that can be tested locally by an orientation testof the two vertices and neighbouring vertices on the hull.We iterate the following two loops, which march x and yup, until they reach the points upper tangency. In ourexample the rightmost point of HA is P6 and the leftmostpoint of HB is P8. This is shown in figure 12 below. Theactual algorithm for computing the tangent is given be-

    low:

    UpperTangent(HA , HB ){ x = the rightmost point of HA; // P6

    y = the leftmost point of HB; // P8While (xy != a upper tangent for HA and HB) do{ While (xy != a upper tangent to HA) do

    x = x - 1; //move x counter-clockwiseWhile (xy != a upper tangent to HB) do

    y = y + 1; //move y clockwise}return xy;}

    JOURNAL OF COMPUTING, VOLUME 5, ISSUE 3, MARCH 2013, ISSN (Online) 2151-9617

    https://sites.google.com/site/journalofcomputing

    WWW.JOURNALOFCOMPUTING.ORG 4

  • 7/30/2019 Divide and Conquer For Convex Hull

    5/8

    Figure 12: Computing the Upper Tangent

    After implementing the algorithm for computing the up-per tangent, it was established that the upper tangent forboth HA and HB is P4P13 as shown above. If one discardsthe other tangents you are left with figure 13 below.

    Figure 13: After Discarding other Tangents

    We now have the upper and lower tangents so we cannow merge our two convex hulls and eliminate non-convex hull edges. The final convex hull is shown in fig-ure 14 below.

    Figure 14: Final Convex Hull

    The asymptotic running time of the algorithm for compu-ting the lower tangent can be expressed by a recurrence.Given an input of size n, consider the time needed to per-

    form all the parts of the procedure, ignoring the recursivecalls. 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 thehull vertices.

    2.2. Approach B - QuickHul l

    The second approach is commonly known as theQuickHull. The main idea is that, we are given a set ofpoints P, and line segment lr is a chord of the convex hull

    CH(P): a chord goes from the leftmost highest to therightmost lowest point in the set.

    1. Among the given points, find the one which is farthestfrom lr.

    Call the point z.2. The points inside the triangle lrc cannot be on the hull.Put them in a set S0.3. Put the points which lie outside edge lz in set A, andpoints outside edge rz in set B.Once the partition is done, we recursively invokeQuickHull procedure on sets S1 and S2.

    The algorithm is as follows:

    findHull (S, l, r)if S={ } then return ()else if S={l, r} then return (l, r) //single convex hull edgeelsez = index of a point that is furthest (max dist.) from xy.A = set containing points strictly right of (x, z);B = set containing points strictly right of (z, y);return {findHull (A, x, z) U (z) U findHull (B, z, y)}

    2.2.1 Implementation of the Algori thm

    Use shall be made of the above example where we aregiven the following set of 14 points sorted in increasingorder of their x-coordinates with ties resolved by increas-ing order of y coordinates. S = {P1(2,12), P2(4,6), P3(4,17),P4(7,20), P4(8,9), P6(9,1), P7(10,19), P8(12,15), P9(14,7),P10(15,19), P11(16,10), P12(18,2) , P13(18,20), P14(20,11)}.The algorithm above computes the upper hull. The lowerhull can be computed in a similar fashion. In this paperwe compute both the lower and the upper hulls simulta-neously as we shall see in the sections below.

    Step 1: findHull(S, r, l).Here S is the set of the above points, r is the rightmostlowest point and l is leftmost highest point and thesepoints are P1 and P14 as shown in the figure 15 below.

    JOURNAL OF COMPUTING, VOLUME 5, ISSUE 3, MARCH 2013, ISSN (Online) 2151-9617

    https://sites.google.com/site/journalofcomputing

    WWW.JOURNALOFCOMPUTING.ORG 5

  • 7/30/2019 Divide and Conquer For Convex Hull

    6/8

    Figure 15: the rightmost lowest point and leftmost highest point

    Step 2: if S = {} return ().In this case S is not null, so we proceed to the next step.

    Step 3: if S ={l,r} return (r,l)In this instance set S has more than two elements so we

    proceed to the next step

    Step4: z, y = indices of points that are furthest (max dist.)from lr.This step involves finding two extreme points one on theright and another on the left of line P1P14. The furthestpoints are P6 and P4. These points are our labelled z andy as shown in the diagram below.

    2.2.2 Selecting the furthest points

    How should the furthest points z and y be selected? Thereare a number of possible selection criteria that one mightthink of. The method that is most often proposed is to letz and y be the points that maximize the perpendiculardistance from the line lr. (For example, another possiblechoice might be the point that maximizes the angle zlrorzrl OR ylr or yrl. It turns out that these can be are verypoor choices because they tend to produce imbalancedpartitions of the remaining points.).

    Step5: Construct triangles P1P6P14 and P1P4P14 and dis-card all points inside these two triangles as shown in thefigure 16 below.

    Figure 16: Constructing Triangles

    Step 6: A = set containing points strictly right of (l, z)(l,z) is equated in the diagram above to the line or chordP1P4 and the points that lie to the right of this line arecontained in set A, where A= {P3}

    Step 7: B = set containing points strictly right of (z, r).(z,r) is equivalent to line P4P14 and the points that lie tothe right of this line are contained in set B where B ={P7,P10, P13}

    Step 8: C = set containing points strictly right of (y, r).

    (y,r) is equivalent to line P6P14 and the points that lie tothe right of this line are contained in set C where C ={P12}

    Step 9: D = set containing points strictly right of (z, r).

    (z,r) is equivalent to line P1P6 and the points that lie tothe right of this line are contained in set D where D ={P2}

    Step 10: Now we need to find the hull of each of theabove sets A through D. The hulls of A,C and D are easy to compute because each of the sets hasa single element. The hull of each set computed by justjoining the point in each set to the respective edges forinstance hull of A is the triangle P1P3P4, hull for C is tri-angle P1P2P6 and hull for D is P6P12P14. As for hull of Bwe have to repeat step 4 and step 5 above. As for B weneed to find the furthest point from line (z,r) or P4P14.The furthest point is P13 so we construct hull P4P13P14and there is no any point on the right of any edge now.Figure 17 below is a result of implementation of step 6 tostep 10.

    Figure 17:Implementation of step 6 to step 10.

    Step 11: return, union of all convex hulls in the figureabove.This involves elimination of non-convex edges such asP1P14, P4P14 and so on to remain with the final convexhull as shown in figure 18 below.

    Figure 18: Final Convex Hull

    The QuickHull algorithm runs in O(n log n) time for fa-vourable inputs but can take as long as O(n2) time for un-favourable inputs. The QuickHull tends to perform verywell in practice. The intuition is that in many applications

    most of the points lie in the interior of the hull. For exam-ple, if the points are uniformly distributed in a unitsquare, then it can be shown that the expected number ofpoints on the convex hull is O(log n).

    JOURNAL OF COMPUTING, VOLUME 5, ISSUE 3, MARCH 2013, ISSN (Online) 2151-9617

    https://sites.google.com/site/journalofcomputing

    WWW.JOURNALOFCOMPUTING.ORG 6

  • 7/30/2019 Divide and Conquer For Convex Hull

    7/8

    3. EFFICIENCY OF QUICKHULL ALGORITHM

    Worst case: ( n2)Average case: ( n log n)If points are not initially sorted by x-coordinate value, thiscan be accomplished in ( n log n)

    3.1 Analysis of divide and conquer

    Initial sorting takes O(N log N) timeRecurrence T(N) = 2T(N/2) + O(N)O(N) for merging (computing tangents)Recurrence solves to T(N) = O(N log N)

    4. CONVEX HULLAPPLICATIONS

    Convex hulls have many applications in robotics, shapeanalysis and line fitting.

    5. RUNNING TIMES OF CONVEX HULL DIVIDE-AND-

    CONQUERALGORITHM

    5.1 Sequential Algorithm:

    Sorting requires O(nlog n) time.However, it is critical to note that the sort only needs tobe performed once at the beginning of the algorithm as apre-processing step and not every time through the recur-sion.The stitch step requires (log n) time to determine thelines of support (i.e., the common tangent lines) and (n)time to reorder (compress) the data.So, there is (nlog n) pre-processing time and T(n) = 2T(n

    / 2) + O(n) = O(nlog n) running time.Therefore, the running time for the algorithm is optimalat (nlog n).

    5.2 Mesh Algori thm:

    Given n points, arbitrarily distributed one point per pro-cessor on a mesh of size n, the convex hull of the set S ofpoints can be determined in ( ) time. First, sort thepoints into shuffled row major order so that the followingholds (geometric points on left side of figure mapped tomesh quadrants as shown in right side of figure 19):

    Figure 19: Mesh Algorithm

    5.3 Binary Search

    Binary search from S1 to S2 and from S2 to S1, where aftereach iteration, the remaining data is compressed together

    so that the running time of the binary search is linear inthe edge length of the sub-mesh that the points initiallyreside in.Broadcast the 4 tangent points.

    Eliminate points inside of quadrilateral and renumber.Running time of binary search is B(n) = B(n / 2) +( ) = ( ).So, the running time of the divide-and-conquer convexhull algorithm on a mesh of size n is T(n) = T(n / 4) + B(n)= T(n / 4) +(n1/2 ) = ( ),plus ( ) for the initial sort, which is ( ).

    6. ADVANTAGES OF THE DIVIDE AND CONQUERAPPROACH FOR FINDING THE CONVEX HULL

    6.1 Algorithm efficiency

    The QuickHull recursively subdivides point set S, andassembles the convex hull H(S) by merging the sub-problem results or partial solutions. The base case re-quires O(1), constant-bounded time. Thus the divide andconquer algorithm will have O(n log n) complexity.

    6.2 Subdivision allows Parallelism

    Divide-and-conquer algorithms are adapted for executionin multi-processor machines, especially shared memorysystems as in the testing of robots using convex hulls;where the communication of data between processorsdoes not need to be planned in advance. Thus distinctsub-problems can be executed on different processors.

    Ideal for solving difficult and complex problemsDivide and conquer is a powerful tool for solving concep-tually difficult problems, such as the classic Tower of Ha-noi puzzle: all it requires is a way of breaking the prob-

    lem into sub-problems, of solving the trivial cases and ofcombining sub-problems to the original problem.

    Memory accessDivide-and-conquer algorithms naturally tend to makeefficient use of memory caches. The reason is that once asub-problem is small enough, it and all its sub-problemscan, in principle, be solved within the cache, without ac-cessing the slower main memory. An algorithm designedto exploit the cache in this way is called cache oblivious,because it does not contain the cache size(s) as an explicitparameter.

    7. DISADVANTAGES OF THE DIVIDE AND CONQUERAPPROACH

    Recursion which is the basis of divide and conquer isslow, the overhead of the repeated subroutine calls, alongwith that of storing the call stack.Inability to control or guarantee sub-problem size resultsin sub-optimum worst case time performance.

    Requires a lot of memory for storing intermediate resultsof sub-convex hulls to be combined to form the completeconvex hull.

    The use of divide and conquer is not ideal if the points tobe considered are too close to each other such that otherapproaches to convex hull will be ideal.

    JOURNAL OF COMPUTING, VOLUME 5, ISSUE 3, MARCH 2013, ISSN (Online) 2151-9617

    https://sites.google.com/site/journalofcomputing

    WWW.JOURNALOFCOMPUTING.ORG 7

  • 7/30/2019 Divide and Conquer For Convex Hull

    8/8

    Is complicated if a large base cases are to be implementedfor performance reasons

    8. ALGORITHMS FOR CONVEX HULL PROBLEM

    Table 1 shows algorithms for the convex hull problem.

    Table 1: Algorithms for Convex Hull problem

    Algorithm Speed Discovered by

    Brute Force O(n ) [Anon, the darka es

    Gift Wrapping O(nh) [Chand &Kapur, 1970]

    Graham Scan O(n log n) [Graham, 1972]

    Jarvis March O(nh) [Jarvis, 1973]

    QuickHull O(nh) [Eddy, 1977],B kat, 1978

    Divide-and-Con uer

    O(n log n) [Preparata &Hon 1977

    Monotone Chain O(n log n) [Andrew, 1979]

    Incremental O(n log n) [Kallay, 1984]

    Marriage-before-Con uest

    O(n log h) [Kirkpatrick &Seidel, 1986

    REFERENCES

    [1] Naher S. and Schmitt D. (2008) A Framework for Multi-Core Imple-mentations of Divide and Conquer Algorithms and its Application tothe Convex Hull Problem, CCCG, Montreal, Quebec, August 1315

    [2] Mohammed Nadeem Ahmed and Raghavendra Kyatham (n.d.) Com-putational Geometry - Part II.

    [3] Souvaine D. (2005) Dynamic Convex Hull and Order DecomposableProblems, Tufts University.

    [4] Miller, R. (1996) Computational Geometry (Divide and Conquer)[5] Simonas altenis (2004), Advanced Algorithm

    Design and Analysis[6] David M. Mount (2000), Computational Geometry. University of

    Maryland, College Park. Available at:http://www.cs.wustl.edu/~pless/506/l3.html

    [7] Alexander Kolesnikov (n.d.) Design of Spatial Information Systems:Convex Hull, University of Joensuu, Joensuu, Finland

    Ngonidzashe Zanamwe holds BSc and MSc degrees in Computer

    Science from the University of Zimbabwe. He is currently doing hisPhD in Computer Scince with the University of Zimbabwe. He iscurrently employeed as a lecturer in Computer Science with theUniversity of Zimbabwe. He has published five papers in internation-al journals and has presented papers at international conference.Hisresearch interests are in algorithms, e-learning and e-commerce.

    JOURNAL OF COMPUTING, VOLUME 5, ISSUE 3, MARCH 2013, ISSN (Online) 2151-9617

    https://sites.google.com/site/journalofcomputing

    WWW.JOURNALOFCOMPUTING.ORG 8