Skip to content
Part IA Lent Term

Jarvis's March (Gift Wrapping)

Algorithm

Jarvis’s march (also known as the gift wrapping algorithm) computes the convex hull in Θ(nh)\Theta(nh) time, where hh is the number of hull vertices. It is output-sensitive: fast when hh is small, slower than Graham’s scan when h=Θ(n)h = \Theta(n).

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.

Jarvis march wrapping

Steps

  1. Start point: Find the leftmost point p0p_0 (break ties by lowest yy). This is guaranteed to be on the hull.

  2. Find the next hull vertex: Given the current hull vertex pip_i and the previous hull vertex pi1p_{i-1} (for i=0i=0, imagine p1p_{-1} is a point with same xx as p0p_0 but y=p0.y1y = p_0.y - 1, i.e. directly below), find the point pi+1p_{i+1} that makes the smallest counter-clockwise angle from the directed segment pi1pip_{i-1} \to p_i.

  3. Repeat until pi+1=p0p_{i+1} = p_0.

Finding the Next Point

For each candidate point qq in the set, compare qq against the current best candidate pnextp_{\text{next}} using the cross product:

Given current point pip_i and previous point pi1p_{i-1}, to determine whether qq is better than the current best:

orient(pi1,pi,q)>0or(orient=0 and q is further)\text{orient}(p_{i-1}, p_i, q) > 0 \quad \text{or} \quad (\text{orient} = 0 \text{ and } q \text{ is further})

If the orientation is positive, qq makes a larger CCW angle than best, so qq 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 sinθ\sin \theta is monotonic in θ\theta (i.e. for angles between π/2-\pi/2 and π/2\pi/2).

Right Chain and Left Chain

An alternative formulation splits the hull into two monotone chains:

  1. Right chain: From the leftmost point to the rightmost point (or topmost, depending on convention), always finding the point with the smallest polar angle.
  2. 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 [π/2,π/2)[-\pi/2, \pi/2) where cross product comparisons are valid monotonic replacements for angle.

Worked Example

Points: (0,0)(0,0), (4,1)(4,1), (3,3)(3,3), (1,4)(1,4), (1,3)(-1,3), (2,1)(-2,1), (2,2)(2,2).

Step 1: Leftmost point =(2,1)= (-2,1) (this is p0p_0).

Step 2 (first edge): Starting from p0=(2,1)p_0 = (-2,1), 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 qq: compute direction from p0p_0 to qq. The point with the smallest CCW angle from the imaginary downward direction is the one that is “most to the right” relative to p0p_0 looking downward.

This yields p1=(1,3)p_1 = (-1,3) — the one that makes the smallest CCW turn from the downward reference.

Step 3: From p0=(2,1)p_0 = (-2,1) and p1=(1,3)p_1 = (-1,3), find the point qq that maximizes the angle p0p1q\angle p_0 p_1 q (i.e. makes the leftmost turn).

Scan all points, use orient(p0,p1,q)\text{orient}(p_0, p_1, q) 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:

StepFrom edgeNext 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: (2,1)(1,3)(1,4)(3,3)(4,1)(0,0)(-2,1) \to (-1,3) \to (1,4) \to (3,3) \to (4,1) \to (0,0) \to back. That’s h=6h = 6 hull vertices from n=7n = 7 points. Note that (2,2)(2,2) is interior and was never selected.

Complexity Analysis

  • Each step scans all nn points: O(n)O(n) per step.
  • There are hh steps (one per hull vertex).
  • Total: Θ(nh)\Theta(nh).

If hh is small, this beats Graham’s Θ(nlogn)\Theta(n \log n). If h=Θ(n)h = \Theta(n), this is Θ(n2)\Theta(n^2), worse than Graham.

When to Use Each

ScenarioPreferred Algorithm
hh expected to be small (hnh \ll n)Jarvis’s March Θ(nh)\Theta(nh)
hh unknown or expected largeGraham’s Scan Θ(nlogn)\Theta(n \log n)
Need asymptotically optimalPrune and Search O(nlogh)O(n \log h)

Summary

PropertyDetail
NameJarvis’s March / Gift Wrapping
TimeΘ(nh)\Theta(nh)
Output-sensitiveYes
SpaceO(1)O(1) beyond input and output
TechniqueScan all points for next hull vertex
ComparisonCross products, no trigonometry
Best whenhnh \ll n
Worst whenh=Θ(n)h = \Theta(n)Θ(n2)\Theta(n^2)