This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CMPT 405 Design and Analysis of Algorithms Outline Solutions to Exercises on Dynamic Programming 1. The residents of the underground city of Zion defend themselves through a combination of kung fu, heavy artillery, and efficient algorithms. Recently they have become interested in automated methods that can help fend off attacks by swarms of robots. Here is what one of these robot attacks looks like. A swarm of robots arrives over the course of n seconds; in the i-th second, x i robots arrive. Based on remote sensing data, you know this sequence x 1 , x 2 , . . . , x n in advance. You have at your disposal an electromagnetic pulse (EMP), which can destroy some of the robots as they arrive; the EMPs power depends on how long it has been allowed to charge up. To make it precise, there is a function f ( ) so that if j seconds have passed since the EMP was last used, then it is capable of destroying up to f ( j ) robots. So specifically, if it is used in the k-th second, and it has been j seconds since it was previously used, then it destroys min( x k , f ( j )) robots. (After this use, it will be completely drained.) We will also assume that EMP starts off completely drained, so if it is used for the first time in the j-th second, then it is capable of destroying up to f ( j ) robots. The problem is: Given the data on robot arrivals x 1 , x 2 , . . . , x n , and given the recharging function f ( ) , choose the points in time at which you are going to activate the EMP so as to destroy as many robots as possible. (a) Show that the following algorithm does not correctly solve this problem, by giving an instance on which it does not return the correct answer. Schedule-EMP( x 1 , . . ., x n ) Let j be the smallest number for which f ( j ) x n (If no such j exists then set j = n ) Activate the EMP in the n-th second If n- j 1 then Continue recursively on the input x 1 , . . ., x n- j (i.e. invoke Schedule-EMP( x 1 , . . ., x n- j ) In your example, say, what the correct answer is and also what the algorithm above finds. (b) Give an efficient algorithm that takes the data on robot arrivals x 1 , . . . , x n , and the recharging func- tion f ( ) , and returns the maximum number of robots that can be destroyed by a sequence of EMP activations. (a) Let n = 4 , f (1) = 2 , f (2) = 3 , f (3) = 5 , f (4) = 7 ; and x 1 = x 2 = 1 , x 3 = 5 , x 4 = 3 . Then the algorithm above will activate EMP in seconds 2 and 4, while activating in seconds 2 and 4 is better....
View Full Document