Vous êtes sur la page 1sur 7

Department of Computer Science And Engineering

DUIET

Design And Analysis Of Algorithm


Project

MOVING ON A CHECKERBOARD

Palash Gogoi Roll - CSC No - 11/09

Rajdeep Borgohain Roll - CSC No - 15/09

November 15, 2011

Project Report submitted on Moving on a Checkerboard problem as a requirement of Design And Analysis Of Algorithm course work.

November 15, 2011


1

MOVING ON A CHECKERBOARD
Palash Gogoi and Rajdeep Borgohain Department of Computer Science And Engineering, DUIET ABSTRACT : Dynamic Programming is a programming technique that can be used to solve complex problems by breaking complex problems into simpler subproblems. Here we demonstrate a way to nd the maximum protable path through a n n checkerboard by applying Optimal Substructure by which the optimal solution is obtained by solution to its subproblems. 1.Introduction : We consider a n n checkerboard. The checker placed in the checkerboard moves form bottom edge to the top edge. At each step checkers can be moved to one of the three squares : square immediately above square that is one up and one to the left only if checker is not already in leftmost column square that is one up and one to the right only if checker is not already in rightmost column Each move from square X to Y fetches p(x,y) dollars.p(x,y) is given for all pairs (x,y) the move is legal. Here p(x,y) is not necessarily negative. The main objective is to nd an algorithm that will move the checkers from bottom edge of Checkerboard to the top edge while gathering maximum dollars.Any square in bottom edge and top edge can be chosen as the starting and ending point. The running time of the algorithm is analyzed. 2.Description : A checkerboard with i rows and j columns is considered and each square is represented by the pair (i,j). Here 1 i and j n. The main objective is to nd the most protable way to any square in the 1st row to any square in the row n Once we get there, we can look up the most protable ways to go to square in row n and pick the best one.

3.Finding the Optimal Substructure : The most protable way to get from some square in row 1 to a particular square (i , j) can be considered a subproblem. For the optimal substructure considered a subproblem (i,j) where i 1 and consider the most protable way to (i,j). Now depending on the legality of the moves, the optimal substructure must be through (i 1, j ). Here j can be dened as : j = j - 1, for going to the upper left column. j = j, for going to the upper column. j = j + 1, for going to the upper right column. Now, the way to get to (i 1, j ) within the most protable way to (i, j) must be a protable way to (i 1, j ) This is the optimal substructure. 4.Describing the Optimal Substructure : In our most protable way to (i, j) which goes through (i 1, j) we earn a prot of d dollars and due to legality of the moves we earn p((i 1, j ), (i, j)) dollars. Now, if there is a way to (i 1, j ) that earns d dollars, where d d , then we would follow that path to get to (i 1, j ) on our way to (i, j) The total prot would be d + p((i 1, j )) > d + p((i 1, j )). This would contradict our view of optimal path to (i, j). So, we have the optimal path through (i 1, j ). 5.Overlapping Subproblem : The path we consider nds an overlapping subproblem where recursive algorithm solves the same problem over and over, rather then generating new ones. So, we need to nd a protable way to the next row that is to (i + 1, j 1) to (i + 1, j) and to (i + 1, j + 1). Let d[i, j] be prot we earn in most protable way to (i,j). For j = 0, we have d[1, j] = 0 where j = 1,2,3..........n. When we take i = 1,2..........n we have d[i 1, j 1] + p((i 1, j 1), (i, j)) if j > 1, d[i, j] = max d[i 1, j] + p((i 1, j), (i, j)) always, d[i 1, j + 1] + p((i 1, j + 1), (i, j)) if j < n 3

To keep track of our track to (i, j) we use w[i, j] to be the value of j to achieve maximum value of (d[i, j]) Here we have 2 i n and 1 j n. 6.Algorithm : Checkerboard(n,p) 1. for j 1 to n 2. do d[i, j] 0 3. for i 2 to n 4. do for j 1 to n 5. do d[i, j] 6. if j > 1 7. then d[i, j] d[i 1, j 1] + p((i 1, j 1), (i, j)) 8. w[i, j] j 1 9. if d[i 1, j] + p((i 1, j), (i, j)) > d[i, j] 10. then d[i, j] d[i 1, j] + p((i 1, j), (i, j)) 11. w[i, j] j 12. if j < n and d[i 1, j + 1] + p((i 1, j + 1), (i, j)) > d[i, j] 13. then d[i, j] d[i 1, j + 1] + p((i 1, j + 1), (i, j)) 14. w[i, j] j + 1 15. return d and w After we ll the d[i, j] table, the prot earned by the most protable way to any square along the top row is max1jn {d[n, j]} 7.Computing The Moves : To compute the set of moves we use Recursive Backtracking. To print the squares from row 1 to row n : Print(w, i, j) if i > 1 then P rint(w, i 1, w[i, j]) end if print "(" i "," j ")" If t = max1jn d[n, j], the initial call becomes Print(w, n ,t) 8. Analysis and Running Time : If we take the Checkerboard algorithm, the for loop of line 1 2 computes the value of d[i, j] 0 for j = 1, 2...n. The for loop of line 3 14 uses recurrence to compute d[i, j] and w[i, j]. In the rst iteration when i = 2, the loop computes for d[i, j] and w[i, j] for j = 1, 2...n depending on the 4

value of j. The second iteration i = 3, computes d[i, j + 1] and w[i, j + 1] for j = 1, 2...(n 1) and so forth. Thus,we can create the n n prot table (d) and moves table (w) in a bottom-up fashion. The columns are indexed by j and the rows i, we rst compute row 1(1, 1)...(1, n), then row 2(2, 1)...(2, n), until row n(n, 1)...(n, n). Since, the for loops are nested two level deep and each loop index takes on at most n values,the time taken to create the prot table is (n2 ). The running time of Checkerboard is (n2 ). After computing the d and w tables, since i = n in the rst call and i decreases by 1 in each recursive call, Print runs in (n) time. 9.Conclusion : We have used Dynamic Programming to eectively break down the problem of Moving through a n n Checkerboard into by considering its Optimal Substructure. We analyzed the running time of the algorithm and found it to be (n2 ). Thus, Dynamic Programming can be used break a complex problem into subproblem and nd an optimal solution.

Bibliography
[1] Cormen, Thomas H. Leiserson, Charles E.Rivest, Ronald L.Stein, Cliord (2001), Introduction To Algorithm MIT Press and McGraw-Hill. [2] S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani (2003), Algorithms,MIT Press and McGraw-Hill. [3] Bellman, Richard (1997),Dynamic Programming, Princeton University Press. Dover paperback edition. [4] H. Simpson(2001), Algorithm Techniques,Cambridge University Press.

Vous aimerez peut-être aussi