CPSC 490: Problem Solving in Computer Science 1 Convex sets A set S - - PowerPoint PPT Presentation

cpsc 490 problem solving in computer science
SMART_READER_LITE
LIVE PREVIEW

CPSC 490: Problem Solving in Computer Science 1 Convex sets A set S - - PowerPoint PPT Presentation

Lecture 16: Convex hull, binary/ternary search Henry Xia, Brandon Zhang based on CPSC 490 slides from 2014-2018 2019-03-14 University of British Columbia CPSC 490: Problem Solving in Computer Science 1 Convex sets A set S is convex x


slide-1
SLIDE 1

CPSC 490: Problem Solving in Computer Science

Lecture 16: Convex hull, binary/ternary search

Henry Xia, Brandon Zhang

based on CPSC 490 slides from 2014-2018

2019-03-14

University of British Columbia

slide-2
SLIDE 2

Convex sets

A set S is convex ⇔ ∀x, y ∈ S, 0 ≤ λ ≤ 1, λx + (1 − λ)y ∈ S ⇔ the line segment x → y is inside the set

Figure 1: In a convex set, the line segment between any 2 points lies in a set

1

slide-3
SLIDE 3

Convex hull

A convex hull of a set is the smallest convex set containing the set.

Figure 2: Some sets and their convex hulls

Why do we care about convex hulls? They give us nice convex polygons, which we can

  • ptimize over easier.

2

slide-4
SLIDE 4

Monotone chain algorithm

Step 1: scan from leħt to right, “wrap” around the top Step 2: scan from right to leħt, “wrap” around the bottom.

Figure 3: Upper and lower hulls built by monotone chain algorithm

3

slide-5
SLIDE 5

Monotone chain algorithm

4

slide-6
SLIDE 6

Monotone chain algorithm

4

slide-7
SLIDE 7

Monotone chain algorithm

4

slide-8
SLIDE 8

Monotone chain algorithm

4

slide-9
SLIDE 9

Monotone chain algorithm

4

slide-10
SLIDE 10

Monotone chain algorithm

4

slide-11
SLIDE 11

Monotone chain algorithm

4

slide-12
SLIDE 12

Monotone chain algorithm

4

slide-13
SLIDE 13

Monotone chain algorithm

4

slide-14
SLIDE 14

Monotone chain algorithm

4

slide-15
SLIDE 15

Monotone chain algorithm

4

slide-16
SLIDE 16

Monotone chain algorithm

4

slide-17
SLIDE 17

Monotone chain algorithm

4

slide-18
SLIDE 18

Monotone chain algorithm

4

slide-19
SLIDE 19

Monotone chain algorithm

4

slide-20
SLIDE 20

Monotone chain algorithm

4

slide-21
SLIDE 21

Monotone chain algorithm

4

slide-22
SLIDE 22

Monotone chain algorithm

4

slide-23
SLIDE 23

Monotone chain algorithm

4

slide-24
SLIDE 24

Monotone chain algorithm

Algorithm summary

  • Sort all points by x-coordinate
  • Start from leħt most point
  • Iteratively add points into the hull
  • If adding a point causes CCW turn, pop points from hull until this is no longer the case

before adding!

  • Repeat the above steps from right to leħt.

5

slide-25
SLIDE 25

Monotone chain algorithm

Edge cases

  • Convex hull of 1, 2, 3 points
  • Collinear points

6

slide-26
SLIDE 26

Monotone chain algorithm

Time complexity analysis

  • Sort all points by x-coordinate: O(n log n)
  • Each point is added and removed at most once: O(n)

⇒ Time complexity: O(n log n) Can we do better? Arrange n points on a circle equivalent to to sorting, n n . However, not all points on the hull, so actually O n h possible.

7

slide-27
SLIDE 27

Monotone chain algorithm

Time complexity analysis

  • Sort all points by x-coordinate: O(n log n)
  • Each point is added and removed at most once: O(n)

⇒ Time complexity: O(n log n) Can we do better? Arrange n points on a circle ⇒ equivalent to to sorting, Ω(n log n). However, not all points on the hull, so actually O(n log h) possible.

7

slide-28
SLIDE 28

Merging convex hulls

Consider the following alternate divide & conquer approach:

  • 1. Split points into two halves
  • 2. Compute convex hull of each half
  • 3. Merge two convex hulls into one

How do we merge?

Figure 4: Two convex hulls

8

slide-29
SLIDE 29

Merging convex hulls

9

slide-30
SLIDE 30

Merging convex hulls

9

slide-31
SLIDE 31

Merging convex hulls

9

slide-32
SLIDE 32

Merging convex hulls

9

slide-33
SLIDE 33

Merging convex hulls

9

slide-34
SLIDE 34

Merging convex hulls

9

slide-35
SLIDE 35

Merging convex hulls

Basic idea

  • Start with the nearest two points
  • Alternate moving leħt side CW and right side CCW along hull
  • Stop when cannot move anymore

⇒ Time complexity of merge: O(n) ⇒ Divide and conquer also solves convex hull in O(n log n).

10

slide-36
SLIDE 36

Merging convex hulls in 3D

Similar idea works in 3D!

11

slide-37
SLIDE 37

Merging convex hulls in 3D

Project to 2D, use 2D convex hulls to find “bottom edge”

11

slide-38
SLIDE 38

Merging convex hulls in 3D

“Giħt-wrap” to find faces, moving to neighbor vertex of current edge

11

slide-39
SLIDE 39

Merging convex hulls in 3D

“Giħt-wrap” to find faces, moving to neighbor vertex of current edge

11

slide-40
SLIDE 40

Merging convex hulls in 3D

“Giħt-wrap” to find faces, moving to neighbor vertex of current edge

11

slide-41
SLIDE 41

Merging convex hulls in 3D

“Giħt-wrap” to find faces, moving to neighbor vertex of current edge

11

slide-42
SLIDE 42

Merging convex hulls in 3D

“Giħt-wrap” to find faces, moving to neighbor vertex of current edge

11

slide-43
SLIDE 43

Merging convex hulls in 3D

“Giħt-wrap” to find faces, moving to neighbor vertex of current edge

11

slide-44
SLIDE 44

Merging convex hulls in 3D

“Giħt-wrap” to find faces, moving to neighbor vertex of current edge

11

slide-45
SLIDE 45

Merging convex hulls in 3D

Summary

  • Compute hull of 2D projection and find “bottom edge”
  • “Giħt-wrap” smartly to find each face around in a circle
  • Sort neighbors of leħt vertex CCW, neighbors of right vertex CW
  • For leħt & right vertices, find their “candidate neighbor”
  • Candidate neighbor of leħt vertex = neighbor of leħt vertex such that leħt convex hull is entirely
  • n one side of plane of new triangle
  • There is only one possible candidate
  • Giħt wrap: find which candidate neighbor (leħt or right) causes “least rotation” of the

plane and add that one

  • Update new: for the new vertex sort neighbors and find candidate
  • Update old: for vertex that did not move, new candidate neighbor must be further along

in the sorted list! ⇒ amortized O(degree)

⇒ Time complexity: O(n) merge ⇒ O(n log n) divide and conquer for 3D convex hull!

12

slide-46
SLIDE 46

Problem 1 – Binary search on a convex hull

Given N ≤ 100, 000 points on the plane, answer Q ≤ 100, 000 queries of the following type: If you “look” from point P, what can you “see”? P

Figure 5: Finding the leħtmost and rightmost point when viewed from P

Hint: to binary search on a circular list, first cut the list in half.

13

slide-47
SLIDE 47

Problem 1 – Solution

Observation 1: solution lies on convex hull Observation 2: pick an arbitrary point on hull, draw a line through, then one extremum on each side of the line. P

14

slide-48
SLIDE 48

Problem 1 – Solution

Observation 1: solution lies on convex hull Observation 2: pick an arbitrary point on hull, draw a line through, then one extremum on each side of the line. ⇒ binary search to find start/end vertices on hull for each half, then binary search on each half of the hull for when P → pi → pi+1 changes from CCW to CW (or vice versa). Time complexity: O(N log N + Q log N)

14

slide-49
SLIDE 49

Problem 2 – Dynamic programming on a convex hull

Given N ≤ 100 points on the plane, and K ≤ N, what’s the maximum area you can enclose by picking up to K out of N points to form a polygon?

Figure 6: Enclosing an area by picking a subset of points to form polygon.

15

slide-50
SLIDE 50

Problem 2: Solution

Observation 1: must pick points on the convex hull Observation 2: convex hull is convex polygon ⇒ run DP on intervals of vertices on the convex hull Main idea: build the polygon by adding triangles one by one dp(l, r, k) = maximum enclosed area using k vertices out of l, l + 1, . . . , r given that l → r is an edge on the boundary = max

l<x<r Area(l, x, r) + dp(x, r, k − 1)

Time complexity: O(N3K)

16

slide-51
SLIDE 51

Ternary search

We can use binary search to find points of interest on a monotonically increasing function. What about minimizing a unimodal function (one that decreases, then increases)? Evaluate it at two points in the middle, if leħt < right, then already increasing in middle section, so minimum not in right section!

17

slide-52
SLIDE 52

Ternary search

What if two probe points are equal? Must be strictly unimodal (i.e. strictly decrease to minimum, then possibly stay flat at minimum, then strictly increase), to avoid failure!

18

slide-53
SLIDE 53

Ternary search

What if we put probe points closer to middle?

  • Converges faster! (by a constant factor)
  • So choose 1 2

and 1 2 ?

  • This is just binary search on derivative
  • Make sure

not too small, otherwise you’ll run into numerical errors.

Can we use less probes per iteration?

  • Golden section search: probe at golden ratios
  • ne probe reused next iteration
  • Let d

R L, 5 1 2, then probe at L d, R d What about on an integer domain?

  • This is easier then usual! Just use two probes at middle and middle 1.

Does it work in 2D/3D? Yes if f is convex.

19

slide-54
SLIDE 54

Ternary search

What if we put probe points closer to middle?

  • Converges faster! (by a constant factor)
  • So choose 1/2 − ϵ and 1/2 + ϵ?
  • This is just binary search on derivative
  • Make sure ϵ not too small, otherwise you’ll run into numerical errors.

Can we use less probes per iteration?

  • Golden section search: probe at golden ratios
  • ne probe reused next iteration
  • Let d

R L, 5 1 2, then probe at L d, R d What about on an integer domain?

  • This is easier then usual! Just use two probes at middle and middle 1.

Does it work in 2D/3D? Yes if f is convex.

19

slide-55
SLIDE 55

Ternary search

What if we put probe points closer to middle?

  • Converges faster! (by a constant factor)
  • So choose 1/2 − ϵ and 1/2 + ϵ?
  • This is just binary search on derivative
  • Make sure ϵ not too small, otherwise you’ll run into numerical errors.

Can we use less probes per iteration?

  • Golden section search: probe at golden ratios
  • ne probe reused next iteration
  • Let d

R L, 5 1 2, then probe at L d, R d What about on an integer domain?

  • This is easier then usual! Just use two probes at middle and middle 1.

Does it work in 2D/3D? Yes if f is convex.

19

slide-56
SLIDE 56

Ternary search

What if we put probe points closer to middle?

  • Converges faster! (by a constant factor)
  • So choose 1/2 − ϵ and 1/2 + ϵ?
  • This is just binary search on derivative
  • Make sure ϵ not too small, otherwise you’ll run into numerical errors.

Can we use less probes per iteration?

  • Golden section search: probe at golden ratios

⇒ one probe reused next iteration

  • Let d = R − L, ϕ = (

√ 5 − 1)/2, then probe at L + ϕd, R − ϕd What about on an integer domain?

  • This is easier then usual! Just use two probes at middle and middle 1.

Does it work in 2D/3D? Yes if f is convex.

19

slide-57
SLIDE 57

Ternary search

What if we put probe points closer to middle?

  • Converges faster! (by a constant factor)
  • So choose 1/2 − ϵ and 1/2 + ϵ?
  • This is just binary search on derivative
  • Make sure ϵ not too small, otherwise you’ll run into numerical errors.

Can we use less probes per iteration?

  • Golden section search: probe at golden ratios

⇒ one probe reused next iteration

  • Let d = R − L, ϕ = (

√ 5 − 1)/2, then probe at L + ϕd, R − ϕd What about on an integer domain?

  • This is easier then usual! Just use two probes at middle and middle+1.

Does it work in 2D/3D? Yes if f is convex.

19

slide-58
SLIDE 58

Ternary search

What if we put probe points closer to middle?

  • Converges faster! (by a constant factor)
  • So choose 1/2 − ϵ and 1/2 + ϵ?
  • This is just binary search on derivative
  • Make sure ϵ not too small, otherwise you’ll run into numerical errors.

Can we use less probes per iteration?

  • Golden section search: probe at golden ratios

⇒ one probe reused next iteration

  • Let d = R − L, ϕ = (

√ 5 − 1)/2, then probe at L + ϕd, R − ϕd What about on an integer domain?

  • This is easier then usual! Just use two probes at middle and middle+1.

Does it work in 2D/3D? Yes if f is convex.

19

slide-59
SLIDE 59

Ternary search (continuous)

Find minimum of strictly unimodal 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): rb = mid2

6

else: lb = mid1

7

return (l+r)/2

Time complexity: O(log r−l

ε ) 20

slide-60
SLIDE 60

Ternary search (discrete)

Find minimum of strictly unimodal function f : Z → R

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): rb = mid2

6

else: lb = mid1

7

return lb

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

21

slide-61
SLIDE 61

Problem 3 – Convex optimization

Find the point that minimizes the total distance to all the other points. The points could be in d dimensions (d ≤ 5).

22

slide-62
SLIDE 62

Problem 3 – Solution

Observation 1: distance function is convex. Observation 2: sum of convex functions is convex ⇒ do d-dimensional ternary search

  • Ternary search on dimension 1
  • Given a probe point in dimension 1, do (d − 1)-dimensional ternary search to find

minimum with dimension 1 fixed, take this as the “value” of the function Time complexity: O(n logd ε−1)

23

slide-63
SLIDE 63

Problem 4 – Rectangle through L-Shaped Corridor

Consider if we had to move a long piece of furniture that had dimensions ℓ × w with ℓ ≤ w through an L-shaped corrider. If we know the dimensions of the corridor a and b, as well as the length ℓ of the furniture, how wide can the furniture be?

Source: Codeforces 98C

24

slide-64
SLIDE 64

Problem 4 – Solution

Consider the following cases when a ≤ b.

  • ℓ ≤ a ≤ b then even if ℓ = w this can fit through the corridor
  • a < ℓ ≤ b then a is the maximum. Clearly this fits through the corridor, and no larger

w can, since both dimensions can’t be larger than ℓ

  • a ≤ b < ℓ then we need to rotate the furniture through the corridor. Consider if we

rotate with two corners of the furniture touching the outer sides of the corridor, then

  • ur answer is the minimum distance of this line to the opposite corner.

This distance is unimodal, so we can ternary search for the answer as we rotate one side!

25

slide-65
SLIDE 65

References

  • http://www.cs.uu.nl/docs/vakken/ga/slides1.pdf
  • http://www.loria.fr/~pougetma/enseignement/webimpa/

2-3D-enveloppe-convexe-od.pdf

  • http://www3.jouy.inra.fr/miaj/public/vigneron/talks/diam.pdf
  • http://www.cs.umd.edu/~mount/754/Lects/754lects.pdf

26