Professional Documents
Culture Documents
Gram Scan IMP
Gram Scan IMP
Gram Scan IMP
2
Homework
HW9, HW10C due Thursday, December 5, 11pm
• Graphs, Reductions
• Written (LaTeX)
3
Final Exam
Monday, December 9, 7pm in Olsson 120
• Practice exam coming soon
• Review session likely the weekend before
• SDAC: Please sign-up for a time on December 9
4
Reductions
Problem 𝑨 Map instances of problem Problem 𝑩
𝑨 to instances of 𝑩
𝐴 𝐵 Algorithm for 𝑩
𝑋 𝑌
Reduction
𝐴 reduces to 𝐵
Problem A Problem B
𝑌 yields
𝑋
Algorithm for B Algorithm for A
𝐴 reduces to 𝐵
Problem A Problem B
𝑌 yields
𝑋
If we know that 𝐴 cannot be Then problem 𝐵 also cannot
Algorithm for B Algorithm for A
solved in polynomial time be solved in polynomial time
𝐴 reduces to 𝐵
Problem A Problem B
𝑌 yields
𝑋
Then, if there is a 𝑂 𝑇 𝑛
If we know that 𝐴 cannot be
Algorithm for B Algorithmreduction
for A from 𝐴 to 𝐵, then 𝐵
solved in 𝑂 𝑇 𝑛 time
cannot be solved in O 𝑇 𝑛 time
Problem: find the smallest convex polygon that bounds a shape (or more generally, a
collection of points)
Example application: collision detection in computer graphics; also useful for solving
other problems, especially in computational geometry (e.g., furthest pair of points)
9
The Convex Hull Problem
Convex polygon: all interior Equivalently: line drawn through
angles are less than 180∘ polygon will intersect exactly twice
Problem: given a set of 𝑛 points, find the smallest convex polygon such that
every point is either on the boundary or the interior of the polygon 10
The Convex Hull Problem
Convex polygon: all interior Equivalently: line drawn through
angles are less than 180∘ Not convex! polygon will intersect exactly twice
> 180∘
> 180∘
Problem: given a set of 𝑛 points, find the smallest convex polygon such that
every point is either on the boundary or the interior of the polygon 11
The Convex Hull Problem
Rubber band
Rubber band analogy: imagine the points are nails sticking out of a board and
wrapping a rubber band to encompass the nails; convex hull is resulting shape 12
The Convex Hull Problem
Observation: every point on the convex hull is one of the input points 13
A Brute Force Approach
14
A Brute Force Approach
Observation: if there are points on
both sides of the line, then the pair
cannot be an edge in the convex hull
Run-time: 𝑂 𝑛1
Brute force approach: for every pair of points, check if
15
all other points are on the same side of the line
Graham’s Algorithm
Observation: Extremal points must be part of the convex hull (e.g., bottom-
most point, left-most point, etc.) 16
Graham’s Algorithm
Polar Angle
Consider the (polar) angle formed between base point 𝑢 and every other point
17
Graham’s Algorithm
Polar Angle
Scan the points in
H
F order of angle
𝜃
J
G
C
I D
E
K
B
𝑢 A
E
K
B
𝑢 A
E
K
B
𝑢 A
E
K
B
𝑢 A
E
K
B
𝑢 A
E
K
B
𝑢 A
E
K
B
𝑢 A
E
K
B
𝑢 A
Idea: Try extending the convex hull from the previous vertex
if we are unable to extend from the current one 25
Graham’s Algorithm
Polar Angle
Scan the points in
H
F order of angle
𝜃
J
G
C
I D
E
K
B
𝑢 A
Idea: Try extending the convex hull from the previous vertex
if we are unable to extend from the current one 26
Graham’s Algorithm
Polar Angle Observe: since points are sorted by
angle, backtracking will never remove
H
F points from the convex hull
𝜃
J
G
C
I D
E
K
B
𝑢 A
Idea: Try extending the convex hull from the previous vertex
if we are unable to extend from the current one 27
Graham’s Algorithm
Polar Angle
Scan the points in
H
F order of angle
𝜃
J
G
C
I D
E
K
B
𝑢 A
E
K
B
𝑢 A
E
K
B
𝑢 A
E
K
B
𝑢 A
E
K
B
𝑢 A
E
K
B
𝑢 A
E
K
B
𝑢 A
E
K
B
𝑢 A
Correctness?
See CLRS 33.3
38
Running Time of Graham’s Algorithm
1. Let 𝑝5 be the point with the smallest 𝑦-coordinate (and smallest 𝑥- 𝑂(𝑛)
coordinate if multiple points have the same minimum-𝑦 coordinate)
2. Add 𝑝5 to the convex hull 𝐶 (represented as a stack)
𝑂(1)
3. Sort all of the points based on their angle relative to 𝑝5 𝑂(𝑛 log 𝑛)
4. For each of the points 𝑝9 in sorted order:
• Try adding 𝑝9 to the convex hull 𝐶 𝑂(1)
• If adding 𝑝9 makes 𝐶 non-convex, then remove the last component of
𝐶 and repeat this check
𝑂 𝑛
𝑂 𝑛 log 𝑛
Running time of Graham’s algorithm: same as best sorting algorithm
Can we do better (without going through sorting)? 42
Running Time of Graham’s Algorithm
1. Let 𝑝5 be the point with the smallest 𝑦-coordinate (and smallest 𝑥- 𝑂(𝑛)
coordinate if multiple points have the same minimum-𝑦 coordinate)
2. Add 𝑝5 to the convex hull 𝐶 (represented as a stack)
𝑂(1)
3. Sort all of the points based on their angle relative to 𝑝5 𝑂(𝑛 log 𝑛)
4. For each of the points 𝑝9 in sorted order:
• Try adding 𝑝9 to the convex hull 𝐶 𝑂(1)
• If adding 𝑝9 makes 𝐶 non-convex, then remove the last component of
𝐶 and repeat this check
𝐴 reduces to 𝐵
Problem A Problem B
𝑌 yields
𝑋
Algorithm for B Algorithm for A
𝐴 reduces to 𝐵
Problem A Problem B
𝑌 yields
𝑋
Then, if there is a 𝑂 𝑇 𝑛
If we know that 𝐴 cannot be
Algorithm for B reduction
Algorithm for A from 𝐴 to 𝐵, then 𝐵
solved in 𝑂 𝑇 𝑛 time
cannot be solved in 𝑂 𝑇 𝑛 time
Can we use
6 5
this to sort?
7
4
8
3
1 2
Can we use
6 5
this to sort?
7
4
8
To get full sorted ordering, convex Want order of3 points in convex hull
hull should contain all of the points to be the order of elements in
1 2
(i.e., values in the set) sorted order
(−3,9) (3,9)
(−2,4) (2,4)
(−1,1) (1,1)
(0,0)
𝑥 ↦ 𝑥, 𝑥 M
−3 −2 −1 0 1 2 3
Goal: need a way to map list of (numeric) values onto a convex hull instance 49
Sorting to Convex Hull Reduction
Lighting a fire
Opening a door
(−3,9) (3,9)
Idea: Map points onto the
parabola (convex shape)
−3 −2 −1 0 1 2 3
Goal: need a way to map list of (numeric) values onto a convex hull instance 50
Sorting to Convex Hull Reduction
Lighting a fire
Opening a door
(−3,9) (3,9)
Idea: Map points onto the
parabola (convex shape)
−3 −2 −1 0 1 2 3
Conclusion: If we can solve convex hull, then we can sort numeric values 51
Convex Hull to Sorting Reduction
sorting convex hull
Map instances of problem
𝑨 to instances of 𝑩
−2 1 −3 0 2 3 −1
𝑂 𝑛
𝑂 𝑛 reduction
𝐴 reduces to 𝐵
sorting numeric values convex hull
Conclusion: a lower bound for sorting translates into one for convex hull
Ω(𝑛 log 𝑛) lower bound for sorting also holds in an “algebraic decision tree model”
(i.e., decisions can be an algebraic function of inputs)
Implies Ω 𝑛 log 𝑛 lower bound for computing convex hull in this model
53
Lower Bound for Convex Hull
Lighting a fire
Opening a door
𝑂 𝑛 reduction
𝐴 reduces to 𝐵
sorting numeric values convex hull
Conclusion: a lower bound for sorting translates into one for convex hull
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
55
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
56
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
57
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
58
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
59
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
60
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
61
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
62
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
63
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
64
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
65
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
66
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
67
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
68
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
69
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
70
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
71
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
72
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
73
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
74
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
75
Jarvis’ Algorithm (Gift Wrapping Method)
Idea: Start with extremal point and “wrap” points in counter-clockwise fashion
76
Jarvis’ Algorithm (Gift Wrapping Method)
Can find the “next” point using a linear scan (i.e., point with largest angle)
Number of iterations: number of points on convex hull
77
Run time: 𝑂(𝑛ℎ) where ℎ is the number of points on the convex hull
Jarvis’ Algorithm (Gift Wrapping Method)
79
Chan’s Algorithm
𝑞M
𝑞5
maximize angle
𝑝QR5
𝑝Q 𝑞1