Course of Graham

The course of Graham is a Algorithme determining the convex Enveloppe of a whole of points. Its principal interest is its algorithmic Complexité out of O (N log N). This algorithm owes its name with Ronald Graham, published the original algorithm in 1972.

Algorithm

The first stage of this algorithm consists in seeking the point of smaller Ordonnée. If there is equality between one or more points, the algorithm chooses among them the point of smaller Abscisse. Let us name P this point. Complexity in times of this stage is out of O (N), N being the number of points of the unit.

The whole of the points (P included/understood) is then sorted according to the angle that each one among them and the point P make with the x-axis. Any algorithm of sorting is appropriate for that, for example the Tri by heap (which has a complexity of O (N log N)). With an aim of accelerating calculations, one can exempt oneself to calculate the real angle that these points make with the x-axis. It is indeed enough to calculate the Tangente this angle, which is realizable by arithmetic simple. At the conclusion of this stage, one has a table T containing the points thus sorted.

The algorithm considers then successively the sequences of three contiguous points in table T, seen like two successive couples. For each one of these pairs of couples, it decides if to pass from the first couple to the second a “turning constitutes on the left” or a “turning on the right”. If it is “revolving on the right”, that means that before last point considered (the second of the three) does not form part of the convex envelope, and that it must be rejected T. This analysis is repeated then, as long as the whole of the last three points is “revolving on the right”. As soon as one meets a “turning on the left”, the algorithm passes at the following point of T. (If one meets three points Colinéaire S, to some stage that it is, one can choose to preserve or reject the point considered, with the choice, according to the definition which one chooses for the convex envelope: indeed certain applications require that all the points on the envelope are included/understood in the envelope).

Here still, to determine if three points constitute a “turning on the left” or “turning on the right” does not require to calculate the real angle between the two Segment S, and can be carried out by simple arithmetic. For three points (x1, y1), (x2, y2) and (x3, y3), it is enough to calculate the direction of the vector Product of both Vecteur S defined by the points (x1, y1), (x2, y2) and (x1, y1), (x3, y3), given by the sign of the expression (x2-x1) (there < sub > 3 there 1) - (there < sub > 2 there 1) (x3-x1). If the result is null, the points are colinéaires. If it is positive, the three points constitute a “turning on the left”, in the contrary case it is “revolving on the right”.

This process will turn over finally to the point to which it started, in which case the algorithm will be finished, and T will contain the points then forming the convex envelope, in the inverse order of the needles of a watch.

Algorithmic complexity

The sorting of the points can be done with a complexity in time out of O (N log N). The complexity of the principal loop can seem to be O (N ²), because the algorithm retrogresses at each point to evaluate if one of the preceding points is “revolving on the right”. But it is in fact out of O (N), because each point is considered only once. Thus, each analyzed point or finishes the subloop, or is withdrawn from T and is thus considered never again. The total complexity of the algorithm is thus out of O (N log N), since the complexity of the sorting dominates the complexity of the effective calculation of the convex envelope.

Pseudo code

That is to say Points the whole of points to be examined, in the shape of a table indexed from one, and Crushes a pile which will contain the end result.

To find the pivot P;
To sort the points by angle (the points of equal angle will be sorted compared to their X-coordinate);
  
  1. Points is the pivot, Points also (fine of the circuit)
Pile.empiler (Points); Pile.empiler (Points); FOR I = 3 has Points.longueur As long as Produit_vectoriel (Pile.second, Pile.haut, Points) <= 0 and Pile.hauteur >= 2 Pile.dépiler; END AS LONG AS Pile.entrer (Points); END FOR FUNCTION Produit_vectoriel (p1, p2, p3) TO RETURN (p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y); FINE FUNCTION

Note: to manage the degenerated cases where the convex envelope has less than three points, only one point should be entered the pile at the beginning, and if ever the pile has less than two points (it will have at least always one of them), then the top of the pile should be also left if the new point is the point considered. In other words, the condition of “as long as” should be:

Pile.hautor >= 2? Produit_vectoriel (Pile.second, Pile.haut, Points<= 0: Pile.haut == Points.

Notes and references of the article

Random links:Mahavishnu Orchestrated | Administrative court of call of Nancy | Parish of Avoyelles | Vincent Malone | Harry_Furniss