Convex Hull Algorithms Christian Atwater Levente Dojcsak

CS581 Algorithms April 7, 2020 Test Questions

1. Which algorithm was published first, Jarvis March or Graham Scan?

2. When is the Jarvis March faster than O(n log n) algorithms like the Graham Scan?

3. What is a called in n dimensions? About Me - Christian Atwater

From Knoxville Went to University of Alabama for undergrad Math, CS double major About Me - Christian Atwater

Traveled some Best place I’ve been to is Switzerland Like to run, play sports, bass guitar About Me - Levente Dojcsak

• From: Forró, Hungary • PhD Student (Math → CS) • Got Married 1 Year Ago • Lived in Hungary, Michigan, St. Louis, Chicago, Knoxville • Favorite Place I’ve Lived: Chicago • Love to Cook • Hobbies: Magic the Gathering, Pokemon Go About Me - Levente Dojcsak Outline

Overview History Algorithm 1: Jarvis March Algorithm 2: Graham Scan Implementation Applications Open Issues References Discussion Overview

• Topic in • Given a set of finite points in n-dimensional space, we want to find a convex hull of the set. • Convex Set: A subset of Euclidean space (Rn), such that a line connecting any two points within the set is contained entirely in the subset. • Focus: Two dimensional space (R2). • Convex Hull: List of vertices ordered along the boundary of a set that contains all of the points of the set according to the above definition. • Mathematical definition: The convex hull of a set M, is the minimal convex set containing M. Overview (cont.)

Convex Concave Overview (cont.)

• Example of Convex vs. Concave Hull Overview (cont.)

• Existence of a unique and minimal Convex Hull is guaranteed. • Simple (non-mathematical) proof: Take any set of points and think of them as spikes in the ground. Then stretch a rubber band around the outside of the spikes so that all of the spikes are inside of the rubber band. • Must have a right turn around each point. History

• Newton Polygon: Given a set of points from an nth degree polynomial, a Newton Polygon is the lower half of the Convex hull of the set of points. In general, it’s a way to find a convex set for a polynomial. • First mentioned in a letter from Isaac Newton to Henry Oldenburg in 1676. • Example: History

• Modern algorithms were developed in the 1970’s. • Often called the first published modern algorithm designed to find Convex Hulls in 2 dimensions is the Jarvis March (1973). • It’s not actually the first algorithm, Graham’s Scan was actually published a year earlier in 1972, but the Jarvis March is the simplest so often comes first in the literature. • Designed by R. (Ray) A. Jarvis - Australian National University. • Not the first mention of convex hulls. Referred to as convex hulls, convex envelopes, or convex closures. The term convex hull was first used by Garrett Birkhoff in 1935. Types of Algorithms

• We can divide algorithms into two types of problems: • The points of the finite input set are known. • The points of the finite input set are generated/deleted dynamically as the algorithm is running. Types of Algorithms

• Graham Scan (1972) - Sort by angle measures • Jarvis March (1973, Gift Wrapping) - Wrap the set CW/CCW • Divide and Conquer (1977) • Quickhull (1977/1978) - Divide and conquer by splitting the set using the farthest available points • Other Algorithms - Chan’s Algorithm (1996) is one of the current best 2D algorithms. Combination of multiple simpler algorithms. Types of Algorithms (Complexity)

Time complexity of algorithms is measured in the number of vertices n and the number of hull vertices h. • Jarvis March = O(nh) • Graham Scan = O(n log n) • Quickhull = O(n log n) → O(n2) worst case • Divide & Conquer = O (n log n) • Best algorithms = O (n log h) Algorithm I - Jarvis March (2D)

Also known as the Gift Wrapping Algorithm. One of the first modern algorithms for finding Convex Hulls, published in 1973. Designed by R. (Ray) A. Jarvis - Australian National University.

Algorithm Outline: • Take as input a set of points S, and begin with an empty ordered hull set H = ∅. • Take the leftmost point in S and put it in the set H. • Pick a second point in S and draw a line between the two points. • Iterate through the remaining points in S until you find the leftmost line. All points in the set S are to the right of the line. Put the point in the set H. • Repeat the last two steps until you reach the starting hull point. Step 1: Pick the leftmost point Step 2: Pick the “leftmost line” Step 2: Pick the “leftmost line” Step 3: Repeat Step 3: Repeat Step 3: Repeat Step 3: Repeat Step 3: Repeat Step 3: Repeat Step 3: Repeat until you add p0 Algorithm I - Jarvis March (2D)

• Multiple Leftmost Points - Add all leftmost points. Starting point is the lowest point since we are calculating clockwise. • Calculating the “Leftmost Line”: • Minimum Exterior Angle / Maximum Interior Angle • Smallest right turn. Calculate the resulting vector and then the cross product. Negative cross product = counter-clockwise / left turn. Positive cross product = clockwise / right turn. • Cross product of new possible vectors. Always take positive cross product. • Collinear Points (zero cross product) - Add all collinear points. Calculate the magnitude of the vectors and add them from smallest to largest. Summary of Jarvis March

: Iterate through the set |S|=n, as many times as you have hull points |H|=h. Therefore, the time complexity is O(nh). Worst Case Complexity: If |H|=n, then O(n2). • Space Complexity: O(n) • Can be expanded to multiple dimensions. In 3 dimensions: • Pick 3 points that form a face and act as a leftmost plane. • Pick half planes in a similar fashion to picking leftmost lines. Algorithm II - Graham Scan

Published in 1972 by , one year before the Jarvis March. Ronald Graham has an Erdős number = 1 More complex than the Jarvis March, but has a much better worst case time complexity. Average Case Time Complexity = O(n log n) Can be improved even further by pre-sorting to get O(n) complexity. Algorithm Outline: • Take as input a set of points S, and begin with an empty stack H. • Take the lowest point in S, call it p0, and add it to the stack H. • Take a horizontal line L through p0 and sort the set S by the angle from L. • Push points onto the stack if they make a left turn from the previous point. • Pop the previous point from the stack if the next point would make a right turn. Step 1: Pick the lowest point

● ● ● ● ● ● ● ● ● ● P0 STACK Step 2: Sort the points by angles

p7 p4 ● ● p8 ● p10 p5 ● p9 ● ● p3 ● ● p2 ● p11 p6 ● P0 STACK p1 Step 3: Push points onto the stack Pop on a right turn

p7 p4 ● ● p8 ● p10 p5 ● p9 ● ● p3 ● ● p2 ● P1 (left) p11 p6 ● P0 STACK p1 Step 3: Push points onto the stack Pop on a right turn

p7 p4 ● ● p8 ● p10 p5 ● p9 ● ● p3 ● ● P2 (left) p2 ● P1 p11 p6 ● P0 STACK p1 Step 3: Push points onto the stack Pop on a right turn

p7 p4 ● ● p8 ● p10 p5 ● p9 ● ● p3 ● P3 (left) ● P2 p2 ● P1 p11 p6 ● P0 STACK p1 Step 3: Push points onto the stack Pop on a right turn

p7 p4 ● ● p8 ● p10 p5 ● p9 ● ● (P4) (right) p3 ● P3 (pop) ● P2 p2 ● P1 p11 p6 ● P0 STACK p1 Step 3: Push points onto the stack Pop on a right turn

p7 p4 ● ● p8 ● p10 p5 ● p9 ● ● p3 ● --- ● P2 p2 ● P1 p11 p6 ● P0 STACK p1 Step 3: Push points onto the stack Pop on a right turn

p7 p4 ● ● p8 ● p10 p5 ● p9 ● ● p3 ● P4 (left) ● P2 p2 ● P1 p11 p6 ● P0 STACK p1 Step 3: Push points onto the stack Pop on a right turn

p7 p4 ● ● p8 ● p10 p5 ● p9 ● ● P5 (left) p3 ● P4 ● P2 p2 ● P1 p11 p6 ● P0 STACK p1 Step 3: Push points onto the stack Pop on a right turn

p7 p4 ● ● p8 ● p10 p5 ● p9 ● P6 (left) ● P5 p3 ● P4 ● P2 p2 ● P1 p11 p6 ● P0 STACK p1 Step 3: Push points onto the stack Pop on a right turn

p7 p4 ● ● p8 ● p10 p5 ● (P7) (right) p9 ● P6 (pop) ● P5 p3 ● P4 ● P2 p2 ● P1 p11 p6 ● P0 STACK p1 Step 3: Push points onto the stack Pop on a right turn

p7 p4 ● ● p8 ● p10 p5 ● p9 ● --- ● P5 p3 ● P4 ● P2 p2 ● P1 p11 p6 ● P0 STACK p1 Step 3: Push points onto the stack Pop on a right turn

p7 p4 ● ● p8 ● p10 p5 ● p9 ● (P7) (right) ● P5 (pop) p3 ● P4 ● P2 p2 ● P1 p11 p6 ● P0 STACK p1 Step 3: Push points onto the stack Pop on a right turn

p7 p4 ● ● p8 ● p10 p5 ● p9 ● ● --- p3 ● P4 ● P2 p2 ● P1 p11 p6 ● P0 STACK p1 Step 3: Push points onto the stack Pop on a right turn

p7 p4 ● ● p8 ● p10 p5 ● p9 ● ● P7 (left) p3 ● P4 ● P2 p2 ● P1 p11 p6 ● P0 STACK p1 Summary of Graham Scan

• Multiple Lowest Points - Pick the leftmost point first. • Sorting Multiple Equal Angles - Pick the point that is farthest from P0. • Time Complexity: O(n log n) If the set is pre-sorted (by angle or vertices) complexity is O(n). • Generally faster than Jarvis March. Jarvis’s is faster if h < log n. • Space Complexity: O(n) • Cannot be expanded to multiple dimensions. • Chan’s Algorithm - Combination of Graham Scan, or another O(n log n) algorithm, with the Jarvis March Implementation

Implemented Gram Scan, QuickHull, and Jarvis March Written in Webgl and javascript Live Demo Applications

• Driving Sensors: Calculate the convex hull of a car to avoid obstacles. Faster to make sure that the convex hull avoids obstacles. • Image Processing • Optimization Problems: Smallest rectangle/cube that encloses a 2D/3D object where at least one edge/face is part of the object. • Calculate the width of a set of points. • Pattern Recognition • Geographic Information Systems • Game Theory/Statistics - Territory control in sports. • Phase Diagrams • Graphics - Video game physics, used to simplify collisions. Open Issues

• Polytopes - Convex Hulls in Rn. • Dynamic Planar Convex Hull in O(log(n)) time. • Many convex hull algorithms are output-sensitive. • What is the best output sensitive algorithm for n points and d dimensions? • Many theoretical unsolved problems. • Maximum number of neighboring polyhedra with a finite number of vertices? References

• Jarvis, R. A. (1973). "On the identification of the convex hull of a finite set of points in the plane". Information Processing Letters. 2: 18–21. doi:10.1016/0020-0190(73)90020-3. • https://www.ti.inf.ethz.ch/ew/courses/CG13/lecture/Chapter%203.pdf • https://www.encyclopediaofmath.org/index.php/Convex_hull • https://mathworld.wolfram.com/ConvexHull.html • https://www.sciencedirect.com/science/article/abs/pii/0020019073900203 (Original Jarvis Paper) • https://iq.opengenus.org/gift-wrap-jarvis-march-algorithm-convex-hull/ (Applications) • https://algorithmtutor.com/Computational-Geometry/Determining-if-two-consecutive-segments-turn-left-or-right/ (Left/Right) • https://upload.wikimedia.org/wikipedia/commons/d/de/Jarvis_march_convex_hull_algorithm_diagram.svg (Diagram) • http://www.personal.kent.edu/~rmuhamma/Compgeometry/MyCG/ConvexHull/convexHull.htm (Picture) • http://cs.smith.edu/~jorourke/TOPP/ (Open Problems) • https://www.biography.com/scientist/isaac-newton (Newton) • https://en.wikipedia.org/wiki/Ronald_Graham (Graham) • Algorithms, 3rd Edition, Cormen et. al. 2009 Discussion

Questions? Comments? Concerns? Test Questions

1. Which algorithm was published first, Jarvis March or Graham Scan?

2. When is the Jarvis March faster than O(n log n) algorithms like the Graham Scan?

3. What is a convex hull called in n dimensions?