Computational Geometry Part 3: Ternary Search, Angle Sweep, and Rotating Calipers Lucca Siaudzionis and Jack Spalding-Jamieson 2020/03/03 University of British Columbia
Announcements • A4 will be released tomorrow (probably). • (Section 204) Thursday’s lecture will probably be taught by Lucca. • Project topic selection is due today! 1
Ternary Search: Intro Input : 1 ≤ n ≤ 10 5 points in the 2D plane. Output : The point p 0 on the x -axis that minimises the sum of euclidean distances to all other points, i.e. � d ( p ′ , p ) p 0 = argmin p ′ =( x , 0): x ∈ R input points p 2
Ternary Search: Intuition If we think about the total distance as we move the x -coordinate of point p 0 left and right, we end up with a function that looks like this (in red): The function has been plotted over top of the original problem for demonstration. We want to find the minimum of this function! 3
Ternary Search: Algorithm The solution to this problem is to use Ternary Search: • Start with a left and right bound, similar to binary search. • Split the range into thirds by querying the function at the additional dividing points. • Deduce that one of the two outer thirds does not contain the minimum/maximum, and remove it from consideration. How do we deduce in the third step? We use a special property of the function: unimodularity/convexity. The outer third adjacent to the point whose query was less optimal can be thrown out. 4
Ternary Search: Implementation (Continuous) Find the minimum of strictly unimodular function f : R → R def ternary_search(l, r, f): 1 while r - l > EPS: 2 mid1 = l + (r-l)/3 3 mid2 = l + 2*(r-l)/3 4 if f(mid1) < f(mid2): r = mid2 5 else: l = mid1 6 return (l+r)/2 7 Time complexity: O (log r − l ε ) 5
Ternary Search: Implementation (Discrete) In practice (for discrete cases), we need not exactly split into thirds. We can simply consider two adjacent points in the centre (which is faster by a coefficient too!). def ternary_search(l, r, f): 1 while l < r: 2 set mid1 = (l+r)/2 3 set mid2 = mid1+1 4 if f(mid1) < f(mid2): r = mid2 5 else: l = mid1 6 return l 7 Time complexity: O (log( r − l )) 6
Ternary Search: Explanation Binary search can find points on a monotonically increasing function. In this case, we want to maximize/minimize a ”hill” function. This type of function is called convex, or unimodal. Note that unlike binary search, the change needs to be strict (”strictly convex”). There can be no flat areas in our function! The following depicts the cases when ternary search works: 7
Discussion Problem 1 Input : A set of 1 ≤ n ≤ 10 5 points in d -dimensional space ( d ≤ 5). Output : A point p 0 placed anywhere that minimises the sum of distances to input points. 8
Discussion Problem 1 – Insight 9
Discussion Problem 1 – Insight Use d -dimensional ternary search. Recurse d times to find the optimal solution to a d -dimensional convex function! Runtime: O (log d n ) This solves a surprisingly large number of convex optimisation problems very well in practice. 10
Angle Sweep: Intro Input : A set S of 1 ≤ n ≤ 10 5 points on the 2D plane, where n is even, and a special point p 0 , guaranteed not to be co-linear with any pair of points in S . Output : A line L going through the point p 0 that divides S perfectly in half. 11
Angle Sweep: Solution We’re going to consider every possible angle of the line in order: Imagine rotating the line clockwise around p 0 . Keep an integer count of the difference between point counts on each side of the line. Whenever we hit a new point on one side, we increment the counter, and decrement the counter when we hit a new point on the other side. 12
Angle Sweep: General In general, angle sweep is very similar to line sweep: • Create a “line” or similar structure that will be rotated (likely around a point). In general, the “line” may actually be many other structures, such as: • Two lines (one for in, one for out). • A circle. • A unimodular function. • Figure out at what angle each of your “events” will intersect the line (in this case, events are points). • As you do your sweep, update some data structure (e.g. set, segment tree, integer) according to the type and information of your events. 13
Discussion Problem 2 Input : A set of 1 ≤ n ≤ 1000 line segments (none of which intersect), and a set of 1 ≤ m ≤ 1000 points. Output : Is it possible to connect any two of the points without intersecting any of the existing line segments? 14
Discussion Problem 2 – Insight Do an angle sweep from each point: • The “line” will be a ray starting at the point (and going to infinity). • The events will be the endpoints of the line segments and the other points. • The data structure will be a set of the current line segments. Using a similar technique to last time, it is possible to query for whether there are line segments in a range using two std::lower bound calls. Runtime: O (( n + m ) 2 ) 15
Rotating Calipers: Intro Input : A convex polygon. Output : The diameter of the convex polygon (the length of the longest line you can draw across the polygon). 16
Rotating Calipers: Demo (1) Imagine having two lines denoting the current width, and rotate them around the polygon. 17
Rotating Calipers: Demo (2) Imagine having two lines denoting the current width, and rotate them around the polygon. 18
Rotating Calipers: Demo (3) Imagine having two lines denoting the current width, and rotate them around the polygon. 19
Discussion Problem 3 Input : A point-set of size 1 ≤ n ≤ 10 5 . Output : The four coordinates of a rectangle enclosing all points of minimum area. Note: The rectangle does not have to be axis-aligned. Figure 1: A possible (rectangular) bounding box 20
Discussion Problem 3 – Insight Use two pairs of rotating calipers simultaneously: one for the width, one for the height Use this to keep track of a current rectangle. Take the final rectangle with minimum area of all those seen. 21
Discussion Problem 3 – Insight Observation: box must line up with one edge on the hull. Suppose not, then we can wiggle the rectangle without changing which vertices it touch. Now, how does the area change? Notice rectangle vertex moves on a semi-circle ⇒ “extra” area is concave-down function so optimum is at extrema of movement. Figure 2: Rectangle vertices move along semicircles of convex hull chords 22
Discussion Problem 3 – Insight How to iterate through all rectangles quickly? Two pairs of calipers! Algorithm: initialize 4 perpendicular calipers, then rotate them, events are when any caliper hits an edge. Compute rectangle area at every event and take the min. Time complexity: every caliper has at most n events, so O ( n ) after convex hull. Figure 3: Iterating through bounding boxes 23
Recommend
More recommend