cpsc 490 problem solving in computer science
play

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


  1. 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

  2. 1 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

  3. A convex hull of a set is the smallest convex set containing the set. Why do we care about convex hulls? They give us nice convex polygons, which we can optimize over easier. 2 Convex hull Figure 2: Some sets and their convex hulls

  4. Step 2: scan from right to leħt, “wrap” around the bottom. Step 1: scan from leħt to right, “wrap” around the top 3 Monotone chain algorithm Figure 3: Upper and lower hulls built by monotone chain algorithm

  5. 4 Monotone chain algorithm

  6. 4 Monotone chain algorithm

  7. 4 Monotone chain algorithm

  8. 4 Monotone chain algorithm

  9. 4 Monotone chain algorithm

  10. 4 Monotone chain algorithm

  11. 4 Monotone chain algorithm

  12. 4 Monotone chain algorithm

  13. 4 Monotone chain algorithm

  14. 4 Monotone chain algorithm

  15. 4 Monotone chain algorithm

  16. 4 Monotone chain algorithm

  17. 4 Monotone chain algorithm

  18. 4 Monotone chain algorithm

  19. 4 Monotone chain algorithm

  20. 4 Monotone chain algorithm

  21. 4 Monotone chain algorithm

  22. 4 Monotone chain algorithm

  23. 4 Monotone chain algorithm

  24. 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 Monotone chain algorithm

  25. • Convex hull of 1, 2, 3 points Edge cases • Collinear points 6 Monotone chain algorithm

  26. Time complexity analysis 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 Monotone chain algorithm • 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 )

  27. Time complexity analysis Can we do better? 7 Monotone chain algorithm • 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 ) 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.

  28. 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? 8 Merging convex hulls Figure 4: Two convex hulls

  29. 9 Merging convex hulls

  30. 9 Merging convex hulls

  31. 9 Merging convex hulls

  32. 9 Merging convex hulls

  33. 9 Merging convex hulls

  34. 9 Merging convex hulls

  35. 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 10 Merging convex hulls ⇒ Time complexity of merge: O ( n ) ⇒ Divide and conquer also solves convex hull in O ( n log n ) .

  36. 11 Similar idea works in 3D! Merging convex hulls in 3D

  37. 11 Project to 2D, use 2D convex hulls to find “bottom edge” Merging convex hulls in 3D

  38. 11 “Giħt-wrap” to find faces, moving to neighbor vertex of current edge Merging convex hulls in 3D

  39. 11 “Giħt-wrap” to find faces, moving to neighbor vertex of current edge Merging convex hulls in 3D

  40. 11 “Giħt-wrap” to find faces, moving to neighbor vertex of current edge Merging convex hulls in 3D

  41. 11 “Giħt-wrap” to find faces, moving to neighbor vertex of current edge Merging convex hulls in 3D

  42. 11 “Giħt-wrap” to find faces, moving to neighbor vertex of current edge Merging convex hulls in 3D

  43. 11 “Giħt-wrap” to find faces, moving to neighbor vertex of current edge Merging convex hulls in 3D

  44. 11 “Giħt-wrap” to find faces, moving to neighbor vertex of current edge Merging convex hulls in 3D

  45. 12 on one side of plane of new triangle • Update new: for the new vertex sort neighbors and find candidate plane and add that one • Giħt wrap: find which candidate neighbor (leħt or right) causes “least rotation” of the Summary • There is only one possible candidate • Candidate neighbor of leħt vertex = neighbor of leħt vertex such that leħt convex hull is entirely • For leħt & right vertices, find their “candidate neighbor” • Sort neighbors of leħt vertex CCW, neighbors of right vertex CW • “Giħt-wrap” smartly to find each face around in a circle • Compute hull of 2D projection and find “bottom edge” Merging convex hulls in 3D • 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!

  46. If you “look” from point P, what can you “see”? P Hint: to binary search on a circular list, first cut the list in half. 13 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: Figure 5: Finding the leħtmost and rightmost point when viewed from P

  47. 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 Problem 1 – Solution

  48. 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. 14 Problem 1 – Solution ⇒ binary search to find start/end vertices on hull for each half, then binary search on each half of the hull for when P → p i → p i + 1 changes from CCW to CW (or vice versa). Time complexity: O ( N log N + Q log N )

  49. by picking up to K out of N points to form a polygon? 15 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 Figure 6: Enclosing an area by picking a subset of points to form polygon.

  50. Observation 1: must pick points on the convex hull Observation 2: convex hull is convex polygon Main idea: build the polygon by adding triangles one by one 16 Problem 2: Solution ⇒ run DP on intervals of vertices on the convex hull 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 ( N 3 K )

  51. 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)? section, so minimum not in right section! 17 Ternary search Evaluate it at two points in the middle, if leħt < right, then already increasing in middle

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

  53. one probe reused next iteration 19 R Does it work in 2D/3D? Yes if f is convex. • This is easier then usual! Just use two probes at middle and middle 1. What about on an integer domain? d d , R 2, then probe at L 1 5 L , • Let d What if we put probe points closer to middle? • Golden section search: probe at golden ratios Can we use less probes per iteration? not too small, otherwise you’ll run into numerical errors. • Make sure • This is just binary search on derivative ? and 1 2 • So choose 1 2 • Converges faster! (by a constant factor) Ternary search

  54. one probe reused next iteration 19 5 Does it work in 2D/3D? Yes if f is convex. • This is easier then usual! Just use two probes at middle and middle 1. What about on an integer domain? d d , R 2, then probe at L 1 R L , What if we put probe points closer to middle? • Let d • Golden section search: probe at golden ratios Can we use less probes per iteration? • This is just binary search on derivative • Converges faster! (by a constant factor) Ternary search • So choose 1 / 2 − ϵ and 1 / 2 + ϵ ? • Make sure ϵ not too small, otherwise you’ll run into numerical errors.

  55. one probe reused next iteration 19 5 Does it work in 2D/3D? Yes if f is convex. • This is easier then usual! Just use two probes at middle and middle 1. What about on an integer domain? d d , R 2, then probe at L 1 R L , What if we put probe points closer to middle? • Let d • Golden section search: probe at golden ratios Can we use less probes per iteration? • This is just binary search on derivative • Converges faster! (by a constant factor) Ternary search • So choose 1 / 2 − ϵ and 1 / 2 + ϵ ? • Make sure ϵ not too small, otherwise you’ll run into numerical errors.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend