Jarvis's March (Gift Wrapping)
Algorithm
Jarvis’s march (also known as the gift wrapping algorithm) computes the convex hull in time, where is the number of hull vertices. It is output-sensitive: fast when is small, slower than Graham’s scan when .
Intuition
Wrap a string around the set of points. Starting at the leftmost point, pull the string taut and rotate it counter-clockwise; the first point the string hits is the next hull vertex. Repeat until returning to the start.
Steps
-
Start point: Find the leftmost point (break ties by lowest ). This is guaranteed to be on the hull.
-
Find the next hull vertex: Given the current hull vertex and the previous hull vertex (for , imagine is a point with same as but , i.e. directly below), find the point that makes the smallest counter-clockwise angle from the directed segment .
-
Repeat until .
Finding the Next Point
For each candidate point in the set, compare against the current best candidate using the cross product:
Given current point and previous point , to determine whether is better than the current best:
If the orientation is positive, makes a larger CCW angle than best, so is better. If orientation is zero (collinear), choose the further point (the nearer one is interior to the hull edge).
This requires no trigonometry: the cross product comparator works because all searches are within half-plane ranges where is monotonic in (i.e. for angles between and ).
Right Chain and Left Chain
An alternative formulation splits the hull into two monotone chains:
- Right chain: From the leftmost point to the rightmost point (or topmost, depending on convention), always finding the point with the smallest polar angle.
- Left chain: From the rightmost/topmost point back to the leftmost, finding the point with the largest polar angle.
This ensures the polar angle search always stays in the range where cross product comparisons are valid monotonic replacements for angle.
Worked Example
Points: , , , , , , .
Step 1: Leftmost point (this is ).
Step 2 (first edge): Starting from , with a reference direction pointing straight down (so the first edge goes to the point with smallest CCW angle from downward). In practice, scan all points:
For each point : compute direction from to . The point with the smallest CCW angle from the imaginary downward direction is the one that is “most to the right” relative to looking downward.
This yields — the one that makes the smallest CCW turn from the downward reference.
Step 3: From and , find the point that maximizes the angle (i.e. makes the leftmost turn).
Scan all points, use to compare. The point with the largest CCW turn (or furthest if collinear) is chosen.
This process continues, always choosing the leftmost turn from the current edge:
| Step | From edge | Next hull point |
|---|---|---|
| 1 | (reference) → (-2,1) | (-1,3) |
| 2 | (-2,1) → (-1,3) | (1,4) |
| 3 | (-1,3) → (1,4) | (3,3) |
| 4 | (1,4) → (3,3) | (4,1) |
| 5 | (3,3) → (4,1) | (0,0) |
| 6 | (4,1) → (0,0) | (-2,1) |
Hull vertices: back. That’s hull vertices from points. Note that is interior and was never selected.
Complexity Analysis
- Each step scans all points: per step.
- There are steps (one per hull vertex).
- Total: .
If is small, this beats Graham’s . If , this is , worse than Graham.
When to Use Each
| Scenario | Preferred Algorithm |
|---|---|
| expected to be small () | Jarvis’s March |
| unknown or expected large | Graham’s Scan |
| Need asymptotically optimal | Prune and Search |
Summary
| Property | Detail |
|---|---|
| Name | Jarvis’s March / Gift Wrapping |
| Time | |
| Output-sensitive | Yes |
| Space | beyond input and output |
| Technique | Scan all points for next hull vertex |
| Comparison | Cross products, no trigonometry |
| Best when | |
| Worst when | → |