Graham scan

Graham scan

The Graham scan is a method of computing the convex hull of a given set of points in the plane with time complexity O("n" log "n"). It is named after Ronald Graham, who published the original algorithm in 1972 [Graham, R.L. (1972). [http://www.sciencedirect.com/science?_ob=IssueURL&_tockey=%23TOC%235645%231972%23999989995%23299179%23FLP%23&_auth=y&view=c&_acct=C000050221&_version=1&_urlVersion=0&_userid=10&md5=5d4861b6aa0cc6f286e142e7d22047c1 An Efficient Algorithm for Determining the Convex Hull of a Finite Planar Set] . Information Processing Letters 1, 132-133] . The algorithm finds all vertices of the convex hull ordered along its boundary. It may also be easily modified to report all input points that lie on the boundary of their convex hull.

Algorithm

The first step in this algorithm is to find the point with the lowest y-coordinate. If there is a tie, the point with the lowest x-coordinate out of the tie breaking candidates should be chosen. Call this point "P". This step takes O("n"), where "n" is the number of points in question.

Next, the set of points must be sorted in increasing order of the angle they and the point "P" make with the x-axis. Any general-purpose sorting algorithm is appropriate for this, for example heapsort (which is O("n" log "n")). In order to speed up the calculations, it is not actually necessary to calculate the actual angle these points make with the x-axis; instead, it suffices to calculate the tangent of this angle, which can be done with simple arithmetic.

The algorithm proceeds by considering each of the points in the sorted array in sequence. For each point, it is determined whether moving from the two previously considered points to this point is a "left turn" or a "right turn". If it is a "right turn", this means that the second-to-last point is not part of the convex hull and should be removed from consideration. This process is continued for as long as the set of the last three points is a "right turn". As soon as a "left turn" is encountered, the algorithm moves on to the next point in the sorted array. (If at any stage the three points are collinear, one may opt either to discard or to report it, since in some applications it is required to find all points on the boundary of the convex hull.)

Again, determining whether three points constitute a "left turn" or a "right turn" does not require computing the actual angle between the two line segments, and can actually be achieved with simple arithmetic only. For three points (x_1,y_1), (x_2,y_2) and (x_3,y_3), simply compute the direction of the cross product of the two vectors defined by points (x_1,y_1), (x_2,y_2) and (x_1,y_1), (x_3,y_3), characterized by the sign of the expression (x_2-x_1)(y_3-y_1)-(y_2-y_1)(x_3-x_1). If the result is 0, the points are collinear; if it is positive, the three points constitute a "left turn", otherwise a "right turn".

This process will eventually return to the point at which it started, at which point the algorithm is completed and the array now contains the points on the convex hull in counterclockwise order.

Time complexity

Sorting the points has time complexity O("n" log "n"). While it may seem that the time complexity of the loop is O("n"2), because for each point it goes back to check if any of the previous points make a "right turn", it is actually O("n"), because each point is considered only once. Each point considered either terminates the inner loop, or it is removed from the array and thus never considered again. The overall time complexity is therefore O("n" log "n"), since the time to sort dominates the time to actually compute the convex hull.

Pseudocode

The result will be stored on Stack.

Find pivot P; Sort Points by angle (with points with equal angle further sorted by distance from P); # Points [1] is the pivot Stack.push(Points [1] ); Stack.push(Points [2] ); FOR i = 3 TO Points.length WHILE Stack.length >= 2 and Cross_product(Stack.second, Stack.top, Points [i] ) <= 0 Stack.pop; ENDWHILE Stack.push(Points [i] ); NEXT i FUNCTION Cross_product(p1, p2, p3) RETURN (p2.x - p1.x)*(p3.y - p1.y) - (p3.x - p1.x)*(p2.y - p1.y); ENDFUNCTION

Note 1: it may be necessary to check whether the last point added to the stack (stack.Top) does not fall inside the hull by comparing it to the next-to-last point (stack.Second) and to the point at the bottom of the stack.

Note 2: to handle the degenerate case of resulting hulls that have less than three points, only one point should be pushed onto the stack to start and if the stack ever has less than two (it will always have at least one) points on it then top should also be popped if the new point is equal to it. In other words the while condition should be

Stack.length >= 2 ? Cross_Product(Stack.second, Stack.top, Points [i] ) <= 0 : Stack.top = Points [i]

Notes

The same basic idea work also if the input is sorted on x-coordinate instead of angle, and the hull is computed in two steps producing the upper and the lower parts of the hull respectively.

References

*Introduction to Algorithms|2|chapter=33.3: Finding the convex hull|pages=pp. 949&ndash;955

External links

* [http://www.partow.net/projects/fastgeo/index.html C++ and Object Pascal Graham Scan Implementations]


Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • Graham Scan — Der Graham Scan (nach Ronald Graham 1972) ist ein effizienter Algorithmus zur Berechnung der konvexen Hülle einer endlichen Menge von Punkten in der Ebene. Bei n Punkten liegt seine asymptotische Laufzeit in . Inhaltsverzeichnis 1 Beschreibung… …   Deutsch Wikipedia

  • Graham — or Graeme is the name of a Scottish family, Clan Graham, which has given its name to many things:People*For people with Graham as a surname see Graham (surname) *For people with Graham as a given name see lookfrom|Graham *For people with Graeme… …   Wikipedia

  • Graham — ist unter anderem ein englischsprachiger Vor und Familienname, siehe Graham (Name), bezeichnet aber auch: Orte in den Vereinigten Staaten: Graham (Alabama) Graham (Arizona) Graham (Florida) Graham (Georgia) Graham (Indiana) Graham (Kalifornien)… …   Deutsch Wikipedia

  • Algorithme de Graham — Parcours de Graham Le parcours de Graham est un algorithme déterminant l enveloppe convexe d un ensemble de points. Son principal intérêt est sa complexité algorithmique en O(n log n). Cet algorithme doit son nom à Ronald Graham, qui a publié l… …   Wikipédia en Français

  • Parcours de graham — Le parcours de Graham est un algorithme déterminant l enveloppe convexe d un ensemble de points. Son principal intérêt est sa complexité algorithmique en O(n log n). Cet algorithme doit son nom à Ronald Graham, qui a publié l algorithme original… …   Wikipédia en Français

  • Parcours de Graham — Le parcours de Graham est un algorithme déterminant l enveloppe convexe d un ensemble de points. Son principal intérêt est sa complexité algorithmique en O(n log n). Cet algorithme doit son nom à Ronald Graham, qui a publié l algorithme original… …   Wikipédia en Français

  • Ronald Graham — Ronald Lewis Graham (born October 31, 1935) is a mathematician credited by the American Mathematical Society with being one of the principal architects of the rapid development worldwide of discrete mathematics in recent years [… …   Wikipedia

  • Ronald Graham — Ronald L. Graham (* 31. Oktober 1935 in Taft, Kalifornien) ist ein amerikanischer Mathematiker. Er leistete bahnbrechende Arbeiten auf dem Gebiet der diskreten Mathematik, insbesondere der Ramsey Theorie. Graham erlangte 1962 seinen Doctor of… …   Deutsch Wikipedia

  • Método de Graham — El método de Graham (Graham scan) es un método de cálculo computacional de la envolvente convexa de un grupo finito de puntos en el plano de complejidad O(nlogn). El nombre hace honor a Ronald Graham, quien publicó el algoritmo en 1972.[1] El… …   Wikipedia Español

  • James A. Graham (psychologist) — James A. Graham, Ph.D., is an associate professor at The College of New Jersey (TCNJ), Department of Psychology. He is a developmental psychologist whose work explores the social cognitive aspects of children’s relationships. Currently, he is the …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”