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/705 Design and Analysis of Algorithms Outline Solutions to Exercises on Dynamic Programming and Network Flow 1. Suppose that you are given an n n checkerboard and a checker. You must move the checker from the bottom edge of the board to the top edge of the board according to the following rule. At each step you may move the checker to one of three squares: the square immediately above, the square that is one up and one to the left (but only if the checker is not already in the leftmost column), the square that is one up and one to the right (but only if the checker is not already in the rightmost column). Each time you move from square x to square y , you receive p ( x,y ) dollars. You are given p ( x,y ) for all pairs ( x,y ) for which a move from x to y is legal. Do not assume that p ( x,y ) is positive. Give an algorithm that figures out the set of moves that will move the checker from somewhere along the bottom edge to somewhere along the top edge while gathering as many dollars as possible. Your algorithm is free to pick any square along the bottom edge as a starting point and any square along the top edge as a destination in order to maximize the number of dollars gathered along the way. What is the running time of your algorithm? Denote each square by the pair ( i,j ) where i is the row number, j is the column number, and 1 i,j n . Our goal is to find a most profitable way from any square in row 1 to any square in row n . Once we do so, we can look up all the most profitable ways to get to any square in row n and pick the best one. A subproblem is the most profitable way to get from some square in row 1 to a particular square ( i,j ) . We have optimal substructure as follows. Consider a subproblem for ( i,j ) , where i > j , and consider the most profitable way to ( i,j ) . Because of how we define legal moves, it must be through square ( i- 1 ,j prime ) , where j prime = j- 1 , j , or j + 1 . then the way we got to ( i- 1 ,j prime ) within the most profitable way to ( i,j ) must itself be a most profitable way to ( i- 1 ,j prime ) . The usual cut-and-paste argument applies. Suppose that in our most profitable way to ( i,j ) , which goes through ( i- 1 ,j prime ) , we earn a profit of...
View Full Document