Computational Geometry Part 3: Ternary Search, Angle Sweep, and - - PowerPoint PPT Presentation

computational geometry
SMART_READER_LITE
LIVE PREVIEW

Computational Geometry Part 3: Ternary Search, Angle Sweep, and - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Computational Geometry

Part 3: Ternary Search, Angle Sweep, and Rotating Calipers

Lucca Siaudzionis and Jack Spalding-Jamieson 2020/03/03

University of British Columbia

slide-2
SLIDE 2

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

slide-3
SLIDE 3

Ternary Search: Intro

Input: 1 ≤ n ≤ 105 points in the 2D plane. Output: The point p0 on the x-axis that minimises the sum of euclidean distances to all other points, i.e. p0 = argminp′=(x,0):x∈R

  • input points p

d(p′, p)

2

slide-4
SLIDE 4

Ternary Search: Intuition

If we think about the total distance as we move the x-coordinate of point p0 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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

Ternary Search: Implementation (Continuous)

Find the minimum of strictly unimodular function f : R → R

1

def ternary_search(l, r, f):

2

while r - l > EPS:

3

mid1 = l + (r-l)/3

4

mid2 = l + 2*(r-l)/3

5

if f(mid1) < f(mid2): r = mid2

6

else: l = mid1

7

return (l+r)/2 Time complexity: O(log r−l

ε ) 5

slide-7
SLIDE 7

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!).

1

def ternary_search(l, r, f):

2

while l < r:

3

set mid1 = (l+r)/2

4

set mid2 = mid1+1

5

if f(mid1) < f(mid2): r = mid2

6

else: l = mid1

7

return l Time complexity: O(log(r − l))

6

slide-8
SLIDE 8

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

slide-9
SLIDE 9

Discussion Problem 1

Input: A set of 1 ≤ n ≤ 105 points in d-dimensional space (d ≤ 5). Output: A point p0 placed anywhere that minimises the sum of distances to input points.

8

slide-10
SLIDE 10

Discussion Problem 1 – Insight

9

slide-11
SLIDE 11

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(logd n) This solves a surprisingly large number of convex optimisation problems very well in practice.

10

slide-12
SLIDE 12

Angle Sweep: Intro

Input: A set S of 1 ≤ n ≤ 105 points on the 2D plane, where n is even, and a special point p0, guaranteed not to be co-linear with any pair of points in S. Output: A line L going through the point p0 that divides S perfectly in half.

11

slide-13
SLIDE 13

Angle Sweep: Solution

We’re going to consider every possible angle of the line in order: Imagine rotating the line clockwise around p0. 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

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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

slide-17
SLIDE 17

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

slide-18
SLIDE 18

Rotating Calipers: Demo (1)

Imagine having two lines denoting the current width, and rotate them around the polygon.

17

slide-19
SLIDE 19

Rotating Calipers: Demo (2)

Imagine having two lines denoting the current width, and rotate them around the polygon.

18

slide-20
SLIDE 20

Rotating Calipers: Demo (3)

Imagine having two lines denoting the current width, and rotate them around the polygon.

19

slide-21
SLIDE 21

Discussion Problem 3

Input: A point-set of size 1 ≤ n ≤ 105. 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

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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

  • ptimum is at extrema of movement.

Figure 2: Rectangle vertices move along semicircles of convex hull chords

22

slide-24
SLIDE 24

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