A topic in the Open Knowledge Graph — a free, open map of 15,495 topics and the order to learn them in.

Convex Hull Algorithms: Graham Scan and Gift Wrapping

Graduate Depth 98 in the knowledge graph ☐ I know this ☆ Set as goal
561prerequisites beneath it
See this on the map →
Asymptotic Notation: Big-O, Big-Omega, Big-ThetaStacks+3 more→
computational-geometry convex-hull graham-scan jarvis-march orientation-test output-sensitive

Core Idea

The convex hull of a finite set of points in the plane is the smallest convex polygon that contains them all; picture a rubber band snapped around nails. Computing it is a basic problem of computational geometry. Graham scan sorts the points by angle around a starting point and then sweeps through them with a stack, discarding any point that would create a clockwise (right) turn, for O(n log n) time overall. Jarvis's march, or gift wrapping, walks around the hull one vertex at a time in O(nh) time, where h is the number of hull vertices, so it is faster when the hull is small. Both rely on a constant-time orientation test computed with a cross product.

How It's Best Learned

Begin with the orientation test: compute the sign of the cross product for a few point triples by hand and connect the sign to left and right turns. Trace Graham scan on 8 to 10 points drawn on grid paper, writing the stack after each step, as in the computational geometry chapter of Cormen, Leiserson, Rivest and Stein. Then compare running times of Graham scan and gift wrapping on random points in a disk (many hull points) and in a square (few hull points).

Common Misconceptions

Explainer

Given n points in the plane, the convex hull is the smallest convex polygon that encloses them. Its vertices are a subset of the points. Hulls are used in collision detection, geographic mapping, pattern recognition and as a first step in many other geometric algorithms.

Every hull algorithm rests on one primitive: the orientation test. For points a, b and c, compute

(b - a) x (c - a) = (bx - ax)(cy - ay) - (by - ay)(cx - ax).

A positive value means a → b → c turns counterclockwise (left), negative means clockwise (right), and zero means the points are collinear. The test uses only multiplication and subtraction, so with integer coordinates it is exact.

Graham scan, published by Ronald Graham in 1972, works in two phases. First, pick the point with the lowest y-coordinate, breaking ties by x, as the anchor p0. Sort the other points by their polar angle around p0, comparing angles with the orientation test instead of trigonometry. Second, scan the sorted points with a stack. Push p0 and the first point. For each new point p, look at the top two stack entries. If they and p make a right turn, the top entry cannot be on the hull, because it lies inside or on the triangle formed by p0, the entry below it and p, so pop it and test again. When the turn is left, push p. At the end the stack holds the hull in counterclockwise order. Each point is pushed once and popped at most once, so the scan costs O(n). With the O(n log n) sort, the total is O(n log n).

Jarvis's march, or gift wrapping, published by R. A. Jarvis in 1973, takes a different route. Start at a point certain to be on the hull, such as the leftmost. Find the point that every other point lies to the left of, as seen from the current point, using one pass of orientation tests. That point is the next hull vertex. Repeat until you return to the start. Each step costs O(n), and there are h steps, so the total is O(nh). This is an output-sensitive bound: fast when the hull is small, but O(n2) when every point is on the hull.

Can we beat O(n log n) in general? Not in the standard algebraic decision tree model. Place numbers x1, ..., xn at points (xi, xi2) on a parabola. All of them are hull vertices, and reading the hull in order sorts the numbers. Sorting requires Omega(n log n) comparisons, so ordered hulls do too. Timothy Chan's 1996 algorithm reaches O(n log h), which is optimal when measured by both n and h.

Practice Questions 4 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Combining Small Groups Within 5 → Number Bonds Within 5 → Number Bonds to 10 → Tens and Ones Place Value → Reading and Writing Three-Digit Numbers → Comparing Three-Digit Numbers → Comparing and Ordering Three-Digit Quantities → Comparing and Ordering Whole Numbers → Comparing Numbers 1 to 20 → Ordering Numbers to 20 → Number Line 0 to 20 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Opposites and Additive Inverses → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Algorithm Design Basics → Asymptotic Notation: Big-O, Big-Omega, Big-Theta → Big-O Notation and Complexity Analysis → Time and Space Complexity → Binary Search → Divide and Conquer → Merge Sort → Sorting Lower Bounds and Non-Comparison Sorts → Convex Hull Algorithms: Graham Scan and Gift Wrapping

Longest path: 99 steps · 561 total prerequisite topics

Prerequisites (5)

Leads To (0)

No topics depend on this one yet.