CPSC 490: Problem Solving in Computer Science of money. You may buy - - PowerPoint PPT Presentation

cpsc 490 problem solving in computer science
SMART_READER_LITE
LIVE PREVIEW

CPSC 490: Problem Solving in Computer Science of money. You may buy - - PowerPoint PPT Presentation

Lecture 18: Geometric interpretations of problems, sweeps Henry Xia, Brandon Zhang based on CPSC 490 slides from 2014-2018 2019-03-21 University of British Columbia CPSC 490: Problem Solving in Computer Science of money. You may buy fractional


slide-1
SLIDE 1

CPSC 490: Problem Solving in Computer Science

Lecture 18: Geometric interpretations of problems, sweeps

Henry Xia, Brandon Zhang

based on CPSC 490 slides from 2014-2018

2019-03-21

University of British Columbia

slide-2
SLIDE 2

Problem 1 – Strategy game

You are choosing a combination of n ≤ 105 items. You have 1 unit of money to spend. Each item has two attributes xi and yi, and costs 1 unit

  • f money. You may buy fractional items (then you will get a fractional part of its

attributes). The total score of your combination of items is (total x) · (total y). What’s the maximum score you can get?

Source: ICPC Pacific Northwest Regional 2018

1

slide-3
SLIDE 3

Problem 1 – Solution

Consider plotting the items in the xy-plane. If we have two items ⃗ a, ⃗ b, what possible (total x, total y) combinations can we get? All of them will lie on the line segment a-b. In general, we can choose combinations that are inside the convex hull of all the items. Now, it’s easy to see that the optimal answer will lie on the boundary of the convex hull. To find it, we can ternary search on each side of the convex hull. Time complexity: O n n

2

slide-4
SLIDE 4

Problem 1 – Solution

Consider plotting the items in the xy-plane. If we have two items ⃗ a, ⃗ b, what possible (total x, total y) combinations can we get? All of them will lie on the line segment ⃗ a-⃗ b. In general, we can choose combinations that are inside the convex hull of all the items. Now, it’s easy to see that the optimal answer will lie on the boundary of the convex hull. To find it, we can ternary search on each side of the convex hull. Time complexity: O(n log n)

2

slide-5
SLIDE 5

Problem 2 – Polyline

Input: n ≤ 105 points in the plane. Output: the maximum number of points you can choose out of these, such that you can make a polyline with them with all slopes positive.

3

slide-6
SLIDE 6

Problem 2 – Solution

Note that the points we choose must be non-decreasing in x-coordinate and increasing in y-coordinate. Sort the points by x-coordinate. Then, the answer is the LIS on y-coordinates! Be careful with equal x-coordinates: we should tiebreak by smaller y-coordinate first. Time complexity: O(n log n)

4

slide-7
SLIDE 7

Line sweeps

Recall the 1D interval union problem. We solved this by scanning from leħt to right, processing “events” (leħt and right endpoints

  • f intervals) in order of increasing x-coordinate, and keeping track of how many intervals

were under the sweep line.

5

slide-8
SLIDE 8

Line sweeps

The general method for solving sweep problems:

  • Identify important “events” that occur as we move from leħt to right.
  • Keep track of some auxiliary data structure that lets us
  • find the answer, and
  • update effjciently at each event.

6

slide-9
SLIDE 9

Problem 3 – Pachinko

Given N ≤ 105 non-intersecting, non-horizontal line segment obstacles, where does the pinball end up?

7

slide-10
SLIDE 10

Problem 3 – Solution

We only need to know for each segment, which “next segment” the pinball will fall onto. We’ll find these by sweeping from leħt to right.

8

slide-11
SLIDE 11

Problem 3 – Solution

We only need to know for each segment, which “next segment” the pinball will fall onto. We’ll find these by sweeping from leħt to right.

8

slide-12
SLIDE 12

Problem 3 – Solution

We only need to know for each segment, which “next segment” the pinball will fall onto. We’ll find these by sweeping from leħt to right.

8

slide-13
SLIDE 13

Problem 3 – Solution

Data structure: set of lines Events: line segment endpoints Actions:

  • Leħt endpoint: insert line
  • Right endpoint: remove line
  • Lower endpoint of segment: query for the next line below the point

9

slide-14
SLIDE 14

Problem 4 – Angular sweep

Divide the rectangle into two halves with equal area such that the two sides have an equal number of blue and red dots.

10

slide-15
SLIDE 15

Problem 4 – Solution

Observation: line must go through center. Do an “angular sweep” by rotating a line around the center of the rectangle!

11

slide-16
SLIDE 16

Problem 4 – Solution

Observation: line must go through center. Do an “angular sweep” by rotating a line around the center of the rectangle!

11

slide-17
SLIDE 17

Problem 4 – Solution

Observation: line must go through center. Do an “angular sweep” by rotating a line around the center of the rectangle!

11

slide-18
SLIDE 18

Problem 4 – Solution

Observation: line must go through center. Do an “angular sweep” by rotating a line around the center of the rectangle!

11

slide-19
SLIDE 19

Problem 4 – Solution

Observation: line must go through center. Do an “angular sweep” by rotating a line around the center of the rectangle!

11

slide-20
SLIDE 20

Problem 4 – Solution

Observation: line must go through center. Do an “angular sweep” by rotating a line around the center of the rectangle!

11

slide-21
SLIDE 21

Problem 4 – Solution

Observation: line must go through center. Do an “angular sweep” by rotating a line around the center of the rectangle!

11

slide-22
SLIDE 22

Problem 4 – Solution

Observation: line must go through center. Do an “angular sweep” by rotating a line around the center of the rectangle!

11

slide-23
SLIDE 23

Problem 4 – Solution

Observation: line must go through center. Do an “angular sweep” by rotating a line around the center of the rectangle!

11

slide-24
SLIDE 24

Problem 4 – Solution

Observation: line must go through center. Do an “angular sweep” by rotating a line around the center of the rectangle!

11

slide-25
SLIDE 25

Problem 4 – Solution

Observation: line must go through center. Do an “angular sweep” by rotating a line around the center of the rectangle!

11

slide-26
SLIDE 26

Problem 4 – Solution

Observation: line must go through center. Do an “angular sweep” by rotating a line around the center of the rectangle!

11

slide-27
SLIDE 27

Problem 4 – Solution

Observation: line must go through center. Do an “angular sweep” by rotating a line around the center of the rectangle!

11

slide-28
SLIDE 28

Problem 4 – Solution

Observation: line must go through center. Do an “angular sweep” by rotating a line around the center of the rectangle!

11

slide-29
SLIDE 29

Problem 4 – Solution

State: how many of each dot is on each side. Events: dot crosses over from one side to the other (sorted by angle around center) Actions: when a dot hits the line, +1 or −1 to the relevant state.

12

slide-30
SLIDE 30

Problem 5 – Angular sweep with circle

Find the circle of radius R that contains the most points.

13

slide-31
SLIDE 31

Problem 5 – Solution

There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle the circle until it is). For every single point, rotate a circle around that point.

14

slide-32
SLIDE 32

Problem 5 – Solution

There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle the circle until it is). For every single point, rotate a circle around that point.

14

slide-33
SLIDE 33

Problem 5 – Solution

There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle the circle until it is). For every single point, rotate a circle around that point.

14

slide-34
SLIDE 34

Problem 5 – Solution

There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle the circle until it is). For every single point, rotate a circle around that point.

14

slide-35
SLIDE 35

Problem 5 – Solution

There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle the circle until it is). For every single point, rotate a circle around that point.

14

slide-36
SLIDE 36

Problem 5 – Solution

There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle the circle until it is). For every single point, rotate a circle around that point.

14

slide-37
SLIDE 37

Problem 5 – Solution

There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle the circle until it is). For every single point, rotate a circle around that point.

14

slide-38
SLIDE 38

Problem 5 – Solution

There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle the circle until it is). For every single point, rotate a circle around that point.

14

slide-39
SLIDE 39

Problem 5 – Solution

There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle the circle until it is). For every single point, rotate a circle around that point.

14

slide-40
SLIDE 40

Problem 5 – Solution

There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle the circle until it is). For every single point, rotate a circle around that point.

14

slide-41
SLIDE 41

Problem 5 – Solution

There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle the circle until it is). For every single point, rotate a circle around that point.

14

slide-42
SLIDE 42

Problem 5 – Solution

There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle the circle until it is). For every single point, rotate a circle around that point.

14

slide-43
SLIDE 43

Problem 5 – Solution

There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle the circle until it is). For every single point, rotate a circle around that point.

14

slide-44
SLIDE 44

Problem 5 – Solution

There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle the circle until it is). For every single point, rotate a circle around that point.

14

slide-45
SLIDE 45

Problem 5 – Solution

There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle the circle until it is). For every single point, rotate a circle around that point.

14

slide-46
SLIDE 46

Problem 5 – Solution

There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle the circle until it is). For every single point, rotate a circle around that point.

14

slide-47
SLIDE 47

Duality

We can change between points and lines with the map (a, b) ↔ y = ax − b. If we apply this map, every point turns into a line and every line turns into a point. This is the “dual space” representation. Some properties:

  • Dual of dual of point is the same point
  • Point above line ⇔ dual of point below dual of line
  • 2 lines L1, L2 intersect in a point ⃗

x ⇔ the dual of ⃗ x passes through dual of L1, L2

  • 3 points collinear ⇔ dual of points intersect at the same point
  • Points CCW on upper hull ⇔ lines leħt to right on lower envelope

15

slide-48
SLIDE 48

Sweeps in the dual

Point above line ⇔ dual of point below dual of line Angular sweep in primal space ⇔ line sweep in dual space!

16

slide-49
SLIDE 49

Convex hulls in the dual

Upper convex hull of points, CCW ⇔ lower envelope of lines, CW Giħtwrapping in primal space ⇔ line sweep in dual space!

17

slide-50
SLIDE 50

Rotating calipers in the dual

Upper convex hull of points, CCW ⇔ lower envelope of lines, CW Lower convex hull of points, CCW ⇔ upper envelope of lines, CW Rotating calipers in primal space ⇔ line sweep in dual space!

18

slide-51
SLIDE 51

Other topics

  • Voronoi diagram / Delaunay triangulation
  • Line arrangement and topological line sweep
  • K-d trees, space hashing
  • Doubly-connected edge list
  • Dynamic convex hull / lower envelope

19