Skip to content
Ahmet Çelik

Chapter 3: Convex Hull

COMP305 8 minute read

Source files used: Convex_hull (Lecture 2 notes, pages 1 to 7A and Example A), MIT6_046JS15_pset1 (Problem 1-2 as practice); textbook Section 33.3 (sec33.3) as support. No worked examples are included.

1. Big Picture

Convex hull is the lecture’s first full divide-and-conquer algorithm.

The Master Theorem guide asked: how do you solve T(n)=aT(n/b)+f(n)T(n)=aT(n/b)+f(n)? This guide asks: how do you design the merge step so that f(n)=Θ(n)f(n)=\Theta(n) instead of Θ(n2)\Theta(n^2)?

Split by x, recurse, merge with two tangents in Θ(n)⇒Θ(nlg⁡n).\boxed{\text{Split by }x\text{, recurse, merge with two tangents in }\Theta(n)\Rightarrow\Theta(n\lg n).}

New ideas:

  • brute force is O(n3)O(n^3),
  • the merge step is “find the upper and lower tangents, then cut and paste”,
  • the two-finger tangent search makes the merge linear,
  • the textbook adds Graham’s scan and Jarvis’s march as alternatives.

2. Core Ideas

2.1 Problem Statement

Given nn points in the plane:

S={(xi,yi)∣i=1,2,…,n}S=\{(x_i,y_i)\mid i=1,2,\dots,n\}

For convenience the lectures assume no two points share an xx coordinate, no two share a yy coordinate, and no three are on a line.

Convex hull CH(S)CH(S): the smallest polygon containing all points in SS.

Picture: the points are nails, and CH(S)CH(S) is the shape of a rubber band stretched around all of them.


2.2 Representation

The lectures represent CH(S)CH(S) as the sequence of boundary points in clockwise order, stored as a doubly linked list:

p↔q↔r↔s↔t↔pp\leftrightarrow q\leftrightarrow r\leftrightarrow s\leftrightarrow t\leftrightarrow p

The textbook algorithms output vertices in counterclockwise order. Follow the lectures’ clockwise convention in exam answers unless told otherwise.


2.3 Divide and Conquer Plan

  1. Sort points by xx coordinate once and for all: O(nlg⁡n)O(n\lg n).
  2. Divide SS into a left half AA and a right half BB by xx coordinate.
  3. Recursively compute CH(A)CH(A) and CH(B)CH(B).
  4. Merge the two hulls.

All the difficulty is in step 4.


2.4 Tangents

The merged hull keeps part of CH(A)CH(A), part of CH(B)CH(B), and two new bridging edges:

  • upper tangent (ai,bj)(a_i,b_j): the bridge across the top,
  • lower tangent (ak,bm)(a_k,b_m): the bridge across the bottom.

An upper tangent does not mean aia_i or bjb_j is the highest point of its hull (Example A in the notes shows this).


2.5 Why the Two-Finger Search Works (Intuition Only)

a1a_1 is the rightmost point of CH(A)CH(A) and b1b_1 is the leftmost point of CH(B)CH(B). The search moves anticlockwise from a1a_1 and clockwise from b1b_1. If (ai,bj)(a_i,b_j) is a pair where moving either finger decreases y(i,j)y(i,j), then no points lie above the line through aia_i and bjb_j.

The lectures state that the formal proof is involved and is not covered.


3. Main Governing Equations and Formulas

3.1 Brute Force

Test every segment between two points: it is a hull edge if all other points lie on one side of it.

O(n2) segments×O(n) tests=O(n3)O(n^2)\text{ segments}\times O(n)\text{ tests}=O(n^3)

Use as the baseline that divide and conquer beats.


3.2 Tangent Criterion

Let LL be the vertical line separating AA and BB. Define

y(i,j)=the y-coordinate where segment (ai,bj) crosses Ly(i,j)=\text{the }y\text{-coordinate where segment }(a_i,b_j)\text{ crosses }L

Claim from the lectures:

(ai,bj) is the upper tangent  ⟺  (i,j) maximizes y(i,j)(a_i,b_j)\text{ is the upper tangent}\iff (i,j)\text{ maximizes }y(i,j)

If y(i,j)y(i,j) is not maximal, there are points on both sides of (ai,bj)(a_i,b_j), so it cannot be a tangent. The lower tangent minimizes y(i,j)y(i,j).


Check all (ai,bj)(a_i,b_j) pairs: O(n2)O(n^2) per merge.

T(n)=2T(n/2)+Θ(n2)=Θ(n2)T(n)=2T(n/2)+\Theta(n^2)=\Theta(n^2)

Use to explain why the merge step must be linear.


3.4 Two-Finger Upper Tangent: Θ(n)\Theta(n)

CH(A)=(a1,…,ap)CH(A)=(a_1,\dots,a_p) with a1a_1 of maximum xx; CH(B)=(b1,…,bq)CH(B)=(b_1,\dots,b_q) with b1b_1 of minimum xx.

i = 1
j = 1
while (y(i, j+1) > y(i, j) or y(i-1, j) > y(i, j)):
    if y(i, j+1) > y(i, j):        # move right finger
        j = j + 1  (mod q)
    else:                          # move left finger
        i = i - 1  (mod p)
return (a_i, b_j) as upper tangent

Each finger only moves forward around its hull, so the loop runs O(p+q)=O(n)O(p+q)=O(n) times.

The lower tangent is found the same way, minimizing y(i,j)y(i,j) with the fingers moving the other way.


3.5 Cut and Paste Merge: Θ(n)\Theta(n)

With upper tangent (ai,bj)(a_i,b_j) and lower tangent (ak,bm)(a_k,b_m):

  1. Link aia_i to bjb_j.
  2. Go down the bb list until you see bmb_m, and link bmb_m to aka_k.
  3. Continue along the aa list until you return to aia_i.

Lecture example: CH(A)=(a1,…,a5)CH(A)=(a_1,\dots,a_5), CH(B)=(b1,b2,b3)CH(B)=(b_1,b_2,b_3), upper tangent (a4,b2)(a_4,b_2), lower tangent (a3,b3)(a_3,b_3), merged hull (a4,b2,b3,a3)(a_4,b_2,b_3,a_3).


3.6 Divide and Conquer Running Time

T(n)=2T(n/2)+Θ(n)=Θ(nlg⁡n)T(n)=2T(n/2)+\Theta(n)=\Theta(n\lg n)

by case 2 of the master theorem. The one-time sort is also O(nlg⁡n)O(n\lg n), so the total is Θ(nlg⁡n)\Theta(n\lg n).


3.7 Textbook Alternative: Graham’s Scan, O(nlg⁡n)O(n\lg n)

  1. Let p0p_0 be the point with minimum yy (leftmost on a tie). It is a hull vertex.
  2. Sort the rest by polar angle counterclockwise around p0p_0 (keep only the farthest point for equal angles).
  3. Push p0,p1,p2p_0,p_1,p_2 onto a stack.
  4. For each next point pip_i: while next-to-top, top, pip_i makes a nonleft turn, pop. Then push pip_i.

The stack ends with the hull in counterclockwise order. Sorting is O(nlg⁡n)O(n\lg n). The loop is O(n)O(n) total by aggregate analysis: each point is pushed once, so it is popped at most once.


3.8 Textbook Alternative: Jarvis’s March, O(nh)O(nh)

Gift wrapping: start at the lowest point; the next vertex has the smallest polar angle from the current one. Build the right chain up to the highest point, then the left chain back down.

O(nh),h=number of hull verticesO(nh),\quad h=\text{number of hull vertices}

Faster than Graham’s scan when h=o(lg⁡n)h=o(\lg n).


4. Problem-Solving Workflow

  1. State the input assumptions (distinct xx, distinct yy, no three collinear) and the output format (clockwise linked list).
  2. Pre-sort by xx once: O(nlg⁡n)O(n\lg n).
  3. Divide at the median xx into AA and BB.
  4. Recurse on both halves.
  5. Find the upper tangent with the two-finger search (Section 3.4).
  6. Find the lower tangent the same way.
  7. Cut and paste the two hulls (Section 3.5).
  8. Write T(n)=2T(n/2)+Θ(n)T(n)=2T(n/2)+\Theta(n) and solve it with the master theorem.
  9. If asked about correctness, use the y(i,j)y(i,j) criterion (Section 3.2) and the intuition in Section 2.5.

Practice: Problem Set 1, Problem 1-2 (radio stations within distance 1) uses the same pattern: presorted lists LxL_x and LyL_y, divide and conquer, and an O(nlg⁡n)O(n\lg n) target, so the combine step has to be linear.


5. Decision Rules / Decision Trees

5.1 Which Hull Algorithm?

Need the lecture algorithm or a D&C design? → divide and conquer, Θ(nlg⁡n)\Theta(n\lg n)

Need a simple stack-based method? → Graham’s scan, O(nlg⁡n)O(n\lg n)

Hull is known to have very few vertices (h=o(lg⁡n)h=o(\lg n))? → Jarvis’s march, O(nh)O(nh)

Only asked for a baseline? → brute force, O(n3)O(n^3)

5.2 Moving the Fingers (Upper Tangent)

y(i,j+1)>y(i,j)y(i,j+1)>y(i,j)? → move right finger: j=j+1(modq)j=j+1 \pmod q

Else y(i−1,j)>y(i,j)y(i-1,j)>y(i,j)? → move left finger: i=i−1(modp)i=i-1 \pmod p

Neither increases yy? → stop, (ai,bj)(a_i,b_j) is the upper tangent

5.3 Merge Cost Decides the Total

Merge is Θ(n2)\Theta(n^2) (try all pairs)? → T(n)=Θ(n2)T(n)=\Theta(n^2)

Merge is Θ(n)\Theta(n) (two fingers)? → T(n)=Θ(nlg⁡n)T(n)=\Theta(n\lg n)


6. Important Tables / Correlations Needed

6.1 Algorithm Summary

MethodIdeaTimeSource
Brute forcetest all segmentsO(n3)O(n^3)lecture
D&C, naive mergeall tangent pairsΘ(n2)\Theta(n^2)lecture
D&C, two-finger mergelinear tangent searchΘ(nlg⁡n)\Theta(n\lg n)lecture
Graham’s scanpolar sort + stackO(nlg⁡n)O(n\lg n)textbook
Jarvis’s marchgift wrappingO(nh)O(nh)textbook
Prune-and-searchdiscard a constant fractionO(nlg⁡h)O(n\lg h)textbook (mentioned only)

6.2 Merge Step Costs

StepCost
Find upper tangentΘ(n)\Theta(n)
Find lower tangentΘ(n)\Theta(n)
Cut and pasteΘ(n)\Theta(n)
Whole mergeΘ(n)\Theta(n)

7. Key Takeaways

  • CH(S)CH(S) is the smallest polygon containing all points: the rubber band around the nails.
  • The lectures store the hull clockwise in a doubly linked list; the textbook outputs counterclockwise.
  • Brute force is O(n2)O(n^2) edges times O(n)O(n) tests =O(n3)=O(n^3).
  • D&C: sort by xx once, split into halves, recurse, merge with two tangents.
  • Upper tangent maximizes y(i,j)y(i,j), the crossing height on the dividing line LL.
  • The upper tangent’s endpoints need not be the highest points.
  • Two-finger search: right finger j+1j+1, left finger i−1i-1, stop when neither raises y(i,j)y(i,j).
  • Merge is Θ(n)\Theta(n), so T(n)=2T(n/2)+Θ(n)=Θ(nlg⁡n)T(n)=2T(n/2)+\Theta(n)=\Theta(n\lg n).
  • A Θ(n2)\Theta(n^2) merge would make the whole algorithm Θ(n2)\Theta(n^2).
  • Textbook alternatives: Graham’s scan O(nlg⁡n)O(n\lg n), Jarvis’s march O(nh)O(nh).