cpsc 490 dp guest lecture
play

CPSC 490 DP - Guest Lecture Part 4: DP Optimizations David Zheng - PowerPoint PPT Presentation

CPSC 490 DP - Guest Lecture Part 4: DP Optimizations David Zheng 2020/01/30 University of British Columbia Announcements An upsolver for A1 has been released! You can get 25% marks on the questions you didnt solve. 1 Guest Lecturer:


  1. CPSC 490 DP - Guest Lecture Part 4: DP Optimizations David Zheng 2020/01/30 University of British Columbia

  2. Announcements • An upsolver for A1 has been released! You can get 25% marks on the questions you didn’t solve. 1

  3. Guest Lecturer: David Zheng • Current coach of the ICPC competitive programming team • ICPC World Finalist in 2018 and 2019 • Master’s student in Computer Science • Taught CS490 in 2017W2 2

  4. What is DP Optimization? What do you do when you work really hard to come up with a dynamic program, but the runtime is too slow? For example if 1 ≤ k ≤ n ≤ 5000, but your algorithm runs in O ( n 2 k ). If we exploit the underlying structure of the problem at hand, it’s possible to optimize to a solution that runs in 1. O ( n 2 ) 2. O ( nk log n ) or O ( nk ) 3. O ( n log n ) This is all VERY dependant on the structure of the original problem you are trying to solve. 3

  5. How do you do DP Optimization? This is a general outline of how to solve harder DP problems. 1. Understand the problem. (This is sometimes hard!) 2. Make observations about the problem. These range from simple to complex observations. 3. Formulate a dynamic program by exploiting the recursive substructure you observed. 4. Make further observations about the underlying structure of the dynamic program. 5. Optimize the runtime with one of these common techniques: • Range min/max query structures, • Knuth Optimization (*), • Divide and Conquer Optimization (*), • Convex Lower/Upper Envelope (?), • Sophisticated Binary Search (AKA the “Alien’s trick”) (?). We will be discussing the optimizations with (*) and maybe the ones with (?) if we have time. 4

  6. Overview of the Lecture One problem. Six subproblems. We will do the following: 1. Understanding the problem 2. Making observations 3. Cooking up a DP 4. Making more observations (about the DP) 5. DP Optimization 1: Knuth Optimization 6. DP Optimization 2: Divide and Conquer Optimization 7. DP Optimization 3: Convex Hull Optimization 8. DP Optimization 4: Binary Search on the Marginal (Alien’s trick) Note: Typically for a problem you can’t use all these DP optimization tricks. 5

  7. Motivating Problem (IOI 2016 Aliens) - Part 1 Our satellite has just discovered an alien civilization on a remote planet. We have already obtained a low-resolution photo of a square area of the planet. The photo shows many signs of intelligent life. Our experts have identified n points of interest in the photo. We now want to take high-resolution photos that contain all of those points. Internally, the satellite has divided the area of the low-resolution photo into an m by m grid of unit square cells. Our satellite is on a stable orbit that passes directly over the main diagonal of the grid. The main diagonal is the line segment that connects the top left and the bottom right corner of the grid. 6

  8. Motivating Problem (IOI 2016 Aliens) - Part 2 The satellite can take a high-resolution photo of any area that satisfies the following constraints: 1. the shape of the area is a square, 2. two opposite corners of the square both lie on the main diagonal of the grid, 3. each cell of the grid is either completely inside or outside the photographed area. The satellite is able to take at most k high-resolution photos. Once the satellite is done taking photos, it will transmit the high-resolution photo of each photographed cell to our home base (regardless of whether that cell contains some points of interest). The data for each photographed cell will only be transmitted once, even if the cell was photographed several times. Thus, we have to choose at most square areas that will be photographed, assuring that: 1. each cell containing at least one point of interest is photographed at least once, and 2. the number of cells that are photographed at least once is minimized. 7 Your task is to find the minimum possible total number of photographed cells.

  9. Understanding the problem - Part 1 The middle diagram shows a solution that photographs 41 cells, while the right one is optimal and photographs only 25 cells. Reminder: n points, the grid is m by m , at most k regions, must be squares on the diagonal. 8

  10. Understanding the problem - Part 2 The right set of photos is the optimal. Since the points are symmetric, if a square has one of the points, it has both of them. We can assume that if point i is at ( x i , y i ), then x i ≤ y i . Reminder: n points, the grid is m by m , at most k regions, must be squares on the diagonal. 9

  11. Goal for this Lecture Goal: We want to solve this problem with these constraints: 1 ≤ k ≤ n ≤ 100 000 and 1 ≤ m ≤ 1 000 000 This is hard, so let’s start small to get an understanding of this problem. 10

  12. Warmup Question - Understanding the Problem (Subtask 1) Can you come up with a solution that will run fast enough if we have the following constraints? 1 ≤ k = n ≤ 50 and 1 ≤ m ≤ 100 . Discuss! Hint: k = n makes this much easier Reminder: n points, the grid is m by m , at most k regions, must be squares on the diagonal. 11

  13. Warmup Question - Find a solution (Subtask 1) Can you come up with a solution that will run fast enough if we have the following constraints? 1 ≤ n = k ≤ 50 and 1 ≤ m ≤ 100 Solution Since k = n , we can afford to have a square region for every point. For point at ( r i , c i ) we need a square with corners at ( r i , r i ) and ( c i , c i ). We can mark parts of the grid as covered, then go through the grid and count covered grid cells in O ( nm 2 ). Reminder: n points, the grid is m by m , at most k regions, must be squares on the diagonal. 12

  14. Intro Problem - Understand the Problem (Subtask 2) Can you come up with a solution that will run fast enough if we have the following constraints? 1 ≤ k ≤ n ≤ 500 and 1 ≤ m ≤ 1000 and All points are on the diagonal, so points are at ( x i , x i ). Discuss! (Make observations and work towards a DP) Note that we may have fewer photos then we have points this time! Example: n = 5 , k = 3 , m = 7. Reminder: n points, the grid is m by m , at most k regions, must be squares on the diagonal. 13

  15. Intro Problem - Make Observations (Subtask 2) 1 ≤ k ≤ n ≤ 500 and 1 ≤ m ≤ 1000 and All points are on the diagonal, so points are at ( x i , x i ). Observations • We can preprocess the points to remove duplicates and sort based on x i . • If a photo covers both ( a , a ) and ( b , b ), it must cover all points in between. • Each photo must be of the form ( x i , x j ). Photos that don’t do this are worse. Example: n = 5 , k = 3 , m = 7. • The photos we take will never overlap. The problem now is to finding k segments that cover n points, such that sum of squares of lengths in minimum. 14

  16. Intro Problem - Formulate DP (Subtask 2) Solution Let f ( i , j ) be min cost for first i points with j photos. Base case: f (0 , j ) = 0 for all 0 ≤ j ≤ k . 0 ≤ t < i f ( t , j − 1) + ( x i − 1 − x t + 1) 2 f ( i , j ) = min Our solution can be found at f ( n , k ). We have a total of O ( nk ) states, and transitions take O ( n ), so overall runtime is O ( n 2 k ). Example: n = 5 , k = 3 , m = 7. 15

  17. DP Problem - Understand the Problem (Subtask 2) 1 ≤ k ≤ n ≤ 500 and 1 ≤ m ≤ 1000 and Points can be at general coordinates ( x i , y i ). Discuss! (Make observations and work towards a DP) Example: n = 5 , k = 3 , m = 7. Reminder: n points, the grid is m by m , at most k regions, must be squares on the diagonal. 16

  18. DP Problem - Make Observations (Subtask 3) 1 ≤ k ≤ n ≤ 500 and 1 ≤ m ≤ 1000 and Points can be at general coordinates ( x i , y i ). Observations • Most of the observations we made before still hold! • The photos we take will overlap. • To cover ( x i , y i ), we need at least a square from ( x i , x i ) to ( y i , y i ). Any square that begins before and ends after will also cover it. • Our solution looks like a set of intervals that cover Example: n = 5 , k = 3 , m = 7. some given intervals. Induces intervals (0 , 2), (1 , 3), • We will never choose two intervals where one is (3 , 4), (4 , 4), and (4 , 6). contained in the other. 17

  19. DP Problem - Formulate a DP (Subtask 3) Solution The same DP from last time almost works. Sort required intervals ( x i , y i ) by left end point after preprocessing. Base case: f (0 , j ) = 0 for all 0 ≤ j ≤ k . 0 ≤ t < i f ( t , j − 1) + ( y i − 1 − x t + 1) 2 f ( i , j ) = min − (max(0 , y t − 1 − x t + 1)) 2 Example: n = 5 , k = 3 , m = 7. This new term is the overlapping square! (It will be exactly this square that gets overcounted) Induces intervals (0 , 2), (1 , 3), (3 , 4), (4 , 4), and (4 , 6). Our solution can be found at f ( n , k ). 18

  20. Optimization (Part 1) - Make more Observations (Subtask 4) 1 ≤ k ≤ n ≤ 4 000 and 1 ≤ m ≤ 1 000 000 0 ≤ t < i f ( t , j − 1) + ( y i − 1 − x t + 1) 2 − (max(0 , y t − 1 − x t + 1)) 2 f ( i , j ) = min Now, O ( n 2 k ) is going to be way too slow! It might help to define this variable: A ( i , j ) is the best t that we can choose for f ( i , j ). Claim: A ( i , j − 1) ≤ A ( i , j ) ≤ A ( i + 1 , j ) 19

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