Scatter a handful of points on a page and ask for the convex hull — the tightest rubber band that wraps them all — and you need a plan for which points to keep and in what order to connect them. In 1972, mathematician Ronald Graham published a strikingly clean plan, now called the Graham scan.
The idea is almost sneaky in its simplicity: pick the lowest point as an anchor, sort every other point by the angle it makes with that anchor, and then walk through the sorted list carrying a stack. Each time the last three points on the stack would force a right turn, the middle one cannot be on the hull — pop it and check again. Keep going until only left turns remain.
No angles are ever recomputed mid-walk, no point is revisited more than a couple of times, and the sort dominates the running time: O(n log n), and provably as fast as any comparison-based algorithm can do this job.
Comments
Loading comments...