This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Chapter 6– Solutions 2 6.1. Subproblems: Define an array of subproblems D ( i ) for 0 ≤ i ≤ n . D ( i ) will be the largest sum of a (possibly empty) contiguous subsequence ending exactly at position i . Algorithm and Recursion: The algorithm will initialize D (0) = 0 and update the D ( i )’s in ascending order according to the rule: D ( i ) = max { ,D ( i − 1) + a i } The largest sum is then given by the maximum element D ( i ) ∗ in the array D . The contiguous subse quence of maximum sum will terminate at i ∗ . Its beginning will be at the first index j ≤ i ∗ such that D ( j − 1) = 0, as this implies that extending the sequence before j will only decrease its sum. Correctness: The contiguous subsequence of largest sum ending at i will either be empty or contain a i . In the first case, the value of the sum will be 0. In the second case, it will be the sum of a i and the best sum we can get ending at i − 1, i.e. D ( i − 1) + a i . Because we are looking for the largest sum, D ( i ) will be the maximum of these two possibilities. Running Time: The running time for this algorithm is O ( n ), as we have n subproblems and the solution of each can be computed in constant time. Moreover, the identification of the optimal subsequence only requires a single O ( n ) time pass through the array D . 6.2. Subproblems: Define subproblem D ( i ) for 0 ≤ i ≤ n to be the minimum total penalty to get to hotel i . Algorithm and Recursion: The algorithm will initialize D (0) = 0 and compute the remaining D ( i ) in ascending order using the recursion: D ( i ) = min ≤ j<i { (200 − a j ) 2 + D ( j ) } To recover the optimal itinerary, we can keep track of a maximizing j for each D ( i ) and use this information to backtrack from D ( n ). Correctness: To solve D ( i ) we consider all possible hotels j we can stay at on the night before reaching hotel i : for each of these possibilities, the minimum penalty to reach i is the sum of the cost of a oneday trip from j to i and the minimum penalty necessary to reach j . Because we are interested in the minimum penalty to reach i , we take the minimum of these values over all j ’s. Running Time: The running time is O ( n 2 ), as we have n subproblems and each takes time O ( n ) to solve, as we need to compute the minimum of O ( n ) values. Moreover, backtracking only takes time O ( n ). 6.3. Subproblems: Define subproblem D ( i ) to be the maximum profit which Yuckdonald’s can obtain from locations 1 to i . Algorithm and Recursion: The algorithm will initialize D (0) = 0, and use the following rule to solve the other subproblems: D ( i ) = max { D ( i − 1) ,p i + D ( i ∗ ) } where i ∗ is the largest index j such that m j ≤ m i − k , i.e. the first location preceding i and at least k miles apart from it....
View
Full Document
 Spring '10
 CHOW
 Dynamic Programming, running time, subproblems

Click to edit the document details