
Graham's scan is a method of finding the
convex hull of a finite set of points in the plane with
time complexity
In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by ...
O(''n'' log ''n''). It is named after
Ronald Graham, who published the original algorithm in 1972.
The algorithm finds all vertices of the convex hull ordered along its boundary. It uses a
stack
Stack may refer to:
Places
* Stack Island, an island game reserve in Bass Strait, south-eastern Australia, in Tasmania’s Hunter Island Group
* Blue Stack Mountains, in Co. Donegal, Ireland
People
* Stack (surname) (including a list of people ...
to detect and remove concavities in the boundary efficiently.
Algorithm
The first step in this algorithm is to find the point with the lowest y-coordinate. If the lowest y-coordinate exists in more than one point in the set, the point with the lowest x-coordinate out of the 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
In computer science, a sorting algorithm is an algorithm that puts elements of a list into an order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending. Efficient sorting is importan ...
is appropriate for this, for example
heapsort (which is O(''n'' log ''n'')).
Sorting in order of angle does not require computing the angle. It is possible to use any function of the angle which is monotonic in the
interval