Convex Hulls (2D) -Jarvis March - Graham Scan - Chan’s Algorithm What is a convex hull? • Wikipedia: “the minimal convex set containing X, where X is a set of points in a real vector space V” • In 2D: Convex polygon of smallest area containing all given points • In 3D: Convex polyhedron of smallest volume containing all given points Output-sensitive algorithms • Any algorithm with a runtime dependent not only on the input size, but on the output size as well • Occasionally pop up in problems where you can compute parts of the output one by one Jarvis march • Start at the leftmost point in the set (if there are multiple, take the closest to the bottom) • For all other points, compute the angle measured ccw with down being 0o • Take point with smallest angle • Repeat, except for further steps, measure angles ccw with the line segment from current to previously selected point being 0o Jarvis march Can we do better? • Jarvis performs reasonably well if output size is small – Calculate angles for every point each time it progresses to a new point, and it progressed to a new point once for every point in the output – Runtime O(nh), where n = # of points, h = # of output points. • If k is large (read: O(lg n)), Jarvis is slow, can be as bad as O(n2) Graham scan • Take starting point to be bottom-most point (if tied, take left-most) • Sort all points by angle with right (+x) being 0o • Iterate over points in order – If the next point forms a “left turn” with the previous two, add it to the list and continue – If “right turn”, remove the previous point until the angle becomes a left turn Graham scan Can we do even better? Graham scan O(n lg n) Jarvis march O(nh) O(n lg h) ? Yup. • The “ultimate convex hull algorithm” published by Kirkpatrick and Seidel in 1986 achieves O(n lg h) – Uses reversal of divide-and-conquer, referred to as “marriage-before-conquest” by authors – Requires median-finding in O(n) • Chan’s algorithm, published in 1996, also achieves O(n lg h), but more simple – As a lazy CS major, I’ll use this Chan’s Algorithm • Uses both Graham scan and Jarvis march as subroutines • Partitions points into smaller chunks, then derives an overall convex hull by exploiting the smaller precomputed hulls Chan’s Algorithm • Suppose we know h, the size of the output • We can partition (arbitrarily) n points into 1+n/h subsets, each with h or h-1 points • Use Graham scan on each of subsets; must be done 1 + n/h times, and each takes O(h lg h), so overall runtime = (1+n/h)O(h lg h) = O(n lg h) • Finish computing convex hull from parts… Chan’s Algorithm Chan’s Algorithm • Problem: In general, we don’t know the size of the convex hull before we compute, so h is unknown; thus, this algorithm will not work • Instead, iterate over the algorithm with different guesses for h to converge to the answer Chan’s Algorithm t 2 min(2 , • For step t, let the guess for h be m = n) • If hull is not completed in m steps, abort • Thus, each step takes O(n lg m) = O(n∙2t) • Starting t at 0, we are guaranteed to find the solution by t = • Thus, total runtime is Chan’s Algorithm • Looks complicated • But, we know • Thus, • So we have a working O(n lg h) algorithm, yay Other optimizations • Discard points found to be inside smaller convex hulls prior to merge step on each iteration of algorithm (if a point lies in any convex polygon, it cannot be in convex hull) t 2 • Alternatives to 2