CPSC 490: Problem Solving in Computer Science Select a presentation - - PowerPoint PPT Presentation

cpsc 490 problem solving in computer science
SMART_READER_LITE
LIVE PREVIEW

CPSC 490: Problem Solving in Computer Science Select a presentation - - PowerPoint PPT Presentation

Lecture 17: Rotating calipers Henry Xia, Brandon Zhang based on CPSC 490 slides from 2014-2018 2019-03-19 University of British Columbia CPSC 490: Problem Solving in Computer Science Select a presentation topic by Friday, March 22.


slide-1
SLIDE 1

CPSC 490: Problem Solving in Computer Science

Lecture 17: Rotating calipers

Henry Xia, Brandon Zhang

based on CPSC 490 slides from 2014-2018

2019-03-19

University of British Columbia

slide-2
SLIDE 2

Announcements

  • Assignment 5 is due tonight at 23:59.
  • Select a presentation topic by Friday, March 22.

1

slide-3
SLIDE 3

Warm-up – Point in polygon

Given a simple polygon (not necessarily convex) and a point, determine whether it lies inside or outside the polygon.

Figure 1: Points inside and outside

2

slide-4
SLIDE 4

Warm-up – Solution 1

Draw a ray from the point out to infinity. The point is inside ⇔ the ray intersects the polygon an odd number of times. Watch out for: point on boundary, ray intersecting a vertex

3

slide-5
SLIDE 5

Warm-up – Solution 2

Walk around the polygon and compute the total angle subtended by each side of the polygon. This total angle will be a multiple of 2π. The point is inside ⇔ the total angle is nonzero.

4

slide-6
SLIDE 6

Warm-up 2 – Cut polygon

Given a convex polygon, output the polygon obtained by cutting ofg everything to the leħt

  • f the line ⃗

a → ⃗ b.

Figure 2: Cutting a polygon

5

slide-7
SLIDE 7

Warm-up 2 – Solution

Since the polygon is convex, it is cut at most twice. Walk around the polygon to find the cut points (or binary search). Watch out for the line intersecting the polygon in a single point, or not at all.

6

slide-8
SLIDE 8

Problem 1 – Convex hull again

Find the two points that are farthest apart in Euclidean distance.

Figure 3: Finding the pair of points that are farthest apart

7

slide-9
SLIDE 9

Problem 1 – Possible solutions

Observation: answer is on the convex hull. Brute force: try all pairs of points on convex hull, O(n2) What about trying every possible end point and ternary search for the other point? No! Adjacent points on hull could have same distance from your start point.

8

slide-10
SLIDE 10

Problem 1 – Possible solutions

Observation: answer is on the convex hull. Brute force: try all pairs of points on convex hull, O(n2) What about trying every possible end point and ternary search for the other point? No! Adjacent points on hull could have same distance from your start point.

8

slide-11
SLIDE 11

Problem 1 – The greedy solution

Simple “2-pointer” greedy:

  • 1. Initialize 2 pointers on two adjacent points on hull
  • 2. Advance the second pointer as far as possible while distance increases
  • 3. Advance the first pointer once and go back to 2
  • 4. End once the first pointer has traveled full circle around hull

Time complexity: O(n) aħter getting convex hull Why does this work? When pointer 1 advances, can use geometry to prove that it is never

  • ptimal to move pointer 2 back.

9

slide-12
SLIDE 12

Rotating calipers

10

slide-13
SLIDE 13

Rotating calipers

10

slide-14
SLIDE 14

Rotating calipers

10

slide-15
SLIDE 15

Rotating calipers

10

slide-16
SLIDE 16

Rotating calipers

10

slide-17
SLIDE 17

Rotating calipers

10

slide-18
SLIDE 18

Rotating calipers

10

slide-19
SLIDE 19

Rotating calipers

10

slide-20
SLIDE 20

Rotating calipers

10

slide-21
SLIDE 21

Problem 2 – Bounding box

Find the smallest bounding box of these points. Rectangle could be rotated.

Figure 4: A possible (rectangular) bounding box

11

slide-22
SLIDE 22

Problem 2 – Solution

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 5: Rectangle vertices move along semicircles of convex hull chords

12

slide-23
SLIDE 23

Problem 2 – Solution

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) aħter convex hull.

Figure 6: Iterating through bounding boxes

13

slide-24
SLIDE 24

Problem 3 – Merging convex hulls, again

We learned how to merge two convex hulls separated by dividing line. What about the general case?

Figure 7: Merging two convex hulls

14

slide-25
SLIDE 25

Problem 3 – Solution

Initialize 2 calipers pointing up at leħtmost point of each hull. Events when any caliper hits edge. Add edge when

  • Calipers switch relative order
  • Outer caliper hits an edge

Time complexity: O(n)

15

slide-26
SLIDE 26

Problem 3 – Solution

Initialize 2 calipers pointing up at leħtmost point of each hull. Events when any caliper hits edge. Add edge when

  • Calipers switch relative order
  • Outer caliper hits an edge

Time complexity: O(n)

15

slide-27
SLIDE 27

Problem 3 – Solution

Initialize 2 calipers pointing up at leħtmost point of each hull. Events when any caliper hits edge. Add edge when

  • Calipers switch relative order
  • Outer caliper hits an edge

Time complexity: O(n)

15

slide-28
SLIDE 28

Problem 3 – Solution

Initialize 2 calipers pointing up at leħtmost point of each hull. Events when any caliper hits edge. Add edge when

  • Calipers switch relative order
  • Outer caliper hits an edge

Time complexity: O(n)

15

slide-29
SLIDE 29

Problem 3 – Solution

Initialize 2 calipers pointing up at leħtmost point of each hull. Events when any caliper hits edge. Add edge when

  • Calipers switch relative order
  • Outer caliper hits an edge

Time complexity: O(n)

15

slide-30
SLIDE 30

Problem 3 – Solution

Initialize 2 calipers pointing up at leħtmost point of each hull. Events when any caliper hits edge. Add edge when

  • Calipers switch relative order
  • Outer caliper hits an edge

Time complexity: O(n)

15

slide-31
SLIDE 31

Problem 3 – Solution

Initialize 2 calipers pointing up at leħtmost point of each hull. Events when any caliper hits edge. Add edge when

  • Calipers switch relative order
  • Outer caliper hits an edge

Time complexity: O(n)

15

slide-32
SLIDE 32

Problem 4 – Non-parallel calipers

Which point should we add to the convex hull to make it biggest?

Figure 8: Picking a point to enlarge convex hull

16

slide-33
SLIDE 33

Problem 4 – Solution

Observation: point must be on “big hull” of all points.

  • Pick arbitrary starting point on big hull, draw two tangent lines to small hull as initial

calipers

  • Rotate calipers around such that their intersection sequentially go through all points
  • n the big hull.

Time complexity: O(n) aħter convex hull

17

slide-34
SLIDE 34

Problem 5 – Minkowski sum

How do we navigate a (convex polygon shaped) robot around convex polygon obstacles? What is the shortest path?

Figure 9: Navigating a convex robot around convex obstacle

18

slide-35
SLIDE 35

Problem 5 – Solution

The problem would be a lot easier if we enlarge our obstacles and shrink the robot to a single point. How? Just “add” the robot and the obstacle together! Actually very easy – just merge all the points and take a convex hull!

Figure 10: Computing the Minkowski Sum of two convex polygons

19

slide-36
SLIDE 36

Problem 6 – Biggest triangle

Find the three points that form biggest area triangle

Figure 11: Forming triangle by choosing 3 points

Can we solve this with a rotating-caliper based approach?

20

slide-37
SLIDE 37

Problem 6 – Solution?

Unfortunately a 3 pointer walk is incorrect. Despite being a published result, a counterexample was found, and a flaw was found in the proof. There still is an alternative O(n) method that is somewhat more complicated.

INCORRECT Minimum area k-gon (generalized version)

  • Aggarwal, Alok, et al. ”Geometric applications of a matrix searching algorithm.” Proceedings of

the second annual symposium on Computational geometry. ACM, 1986. Correct solution

  • Jin, Kai. ”Maximal Area Triangles in a Convex Polygon.” 2017.

21

slide-38
SLIDE 38

More applications of rotating calipers

  • Minimum/maximum distance between two convex polygons
  • Widest empty strip between two convex polygons
  • “Art gallery problem”
  • Intersection of two convex polygons

22