Matakuliah Tahun : K0414 / Riset Operasi Bisnis dan Industri : 2008 / 2009 Penerapan Dinamik Programming (DP) Pertemuan 16 (GSLC) Learning Outcomes • Mahasiswa dapat mendemonstrasikan penyelesaian masalah dinamik programming, dengan menggunakan bantuan program komputer. Bina Nusantara University 3 Outline Materi: • Penerapan Dinamik Programming (DP) • Penyelesaian masalah DP dengan program komputer. Bina Nusantara University 4 Dynamic Programming • An extraordinarily powerful approach for some classes of problems. • The trick is: You must be able to express the answer for your current problem in terms of smaller problems for which you already know the answer. • Sometimes the possibility of using dynamic programming presents itself when we arrange our data in matrix form. • Let’s look at the “rock game” proposed by the authors. Bina Nusantara University 5 The “Rock Game” • Rules: – Start with two piles of rocks, n in one pile and m in the other. – Players take turns; I start. On each turn, a player can either take a single rock (taken from either pile), or two rocks (one from each pile). – Player who takes the last rock wins! • Goal: We want to find an optimal strategy, so that given an initial collection of n and m rocks, we are sure to win (provided that’s possible, of course!) Bina Nusantara University 6 Encoding the Rocks game • We can use a matrix to represent the current state of the game; being in row i and column j represents having i rocks in the first pile and j in the second. • I put a W in each matrix cell if there exists a winning strategy for me, given the corresponding configuration of rocks, and an L if a competent opponent is sure to beat me. Bina Nusantara University 7 Moves in the Rock game • Each possible action that I can take “reduces” the size of the game, and corresponds to a move from my current cell to – the cell immediately above; – the cell immediately to the left; – the cell on the diagonal, one space to the left and one up. • Suppose that I move, and enter a cell that already has a W; that symbol means that there exists a winning strategy for me, provided that I move first. Since it is now my opponent’s move, I have just lost the game! Bina Nusantara University 8 Trying out the Rock Game example • Let’s fill in the matrix, following the lead of our authors. • Note how we easily build on the known solutions for smaller problems to progressively fill in the matrix. • Let’s go a step further, and plot the possible moves that correspond to a winning strategy. Note that a particular game will be represented by a particular path through our matrix. • Can we write a rule for expressing the form of a winning game, expressed as a path? Bina Nusantara University 9 Revisiting USChange as a DP Problem • Recall: Given a set of coin denominations {c1,c2,…,cd}, and a monetary value M to return as change, return the smallest number of coins possible. • Here’s how to recast the problem by reference to “smaller” problems. We define the “best number of coins” for M in terms of the “best number of coins” for M - ci, for each of the denominations. bestNumCoinsM c 1 1 bestNumCoinsM min bestNumCoinsM c2 1 bestNumCoins 1 M c d 10 Bina Nusantara University Recursive Implementation RecursiveChange(M,c,d) if M = 0 return M ; bestNumCoins <- Infinity for i <- 1 to d { if M >= ci { numCoins <RecursiveChange(M-ci,c,d) if numCoins + 1 < numBestCoins numBestCoins <- numCoins + 1 } Bina Nusantara University } return bestNumCoins 11 Drawbacks of Recursion • This recursive algorithm suffers the same problem of the recursive Fibonacci program; it computes the same values multiple times. – For example, if M=10, we will generate a call for M - 5 = 5; we will also generate a call for M - 1 = 9, which will generate (M - 1) - 1, etc until eventually there is another call with argument 5. • We are better off to start from M=0, and build up the best coin counts for increasing M, always referring back to the optimal values already recorded. This is the essence of dynamic programming. Bina Nusantara University 12 Example… • Suppose we have already determined the optimal number of coins to return for the following values of M: M=1 1 (1 cent) M=2 2 (2 X 1 cent) M=3 3 ( 3 X 1 cent) M=4 4 (4 X 1 cent) M=5 1 (1 nickel) For M=6, we check Optimum(6 - 1 cent) + 1 = Optimum(5) + 1 = 2 (1 nickel + 1 cent) Optimum(6 - 1 nickel) + 1 = Optimum(1) + 1 = 2 (1 cent + 1 nickel) So, the optimum number of coins in change for 6 cents is 2 (1 nickel + 1 cent) Bina Nusantara University 13 DP Implementation DPChange(M,c,d) bestNumCoins0 <- 0 for m <- 1 to M { bestNumCoinsm <- Infinity for i <- 1 to d { if m >= ci { if (bestNumCoinsm-ci + 1 < bestNumCoinsm) bestNumCoinsm <bestNumCoinsm-ci + 1 } } } Bina Nusantara University return bestNumCoinsM 14 The Manhattan Tourist Problem Source 3 1 3 4 2 0 2 6 0 4 7 3 4 0 4 3 3 1 2 8 1 2 5 2 3 4 0 3 2 2 5 6 1 2 5 4 5 4 3 2 Edge weight Sink Node/Vertex Bina Nusantara University A graph Edge 15 The Greedy Solution 3 0 1 3 4 2 3 0 6 0 4 4 9 4 13 3 3 1 4 15 19 2 0 1 2 ? 8 3 2 3 2 5 6 1 2 0 5 4 5 5 7 3 Bina Nusantara University 2 4 20 5 2 ? 3 2 23 We see immediately that the greedy solution is not optimal! 16 Approach to Dynamic Programming • Start with the more general problem - find the best path from vertex i to vertex j. Call the length of the best path (the sum of the edge weights along it) sij. • With analogy to the DPChange algorithm, we discover that we can efficiently find the solution by finding the answer for all the sub-problems (all possible sink vertices), and then picking out the solution we want. Bina Nusantara University 17 Getting started - paths along the edges are completely constrained! Column 0 Row 0 3 0 1 1 3 4 0 4 14 4 4 3 3 2 3 4 0 1 2 5 2 9 1 2 8 3 0 9 2 5 6 1 2 5 4 5 4 5 7 3 9 2 6 0 5 Bina Nusantara University 2 3 3 2 18 Now we can find s11… Can arrive at (1,1) either from above (N) or the left (W). We know the best scores for each of those possible sources, and the weights of the edges that connect (1,1) to them… 3 0 1 1 3 4 0 4 4 14 4 4 3 3 2 3 4 0 1 2 5 2 9 1 2 8 3 0 9 2 5 6 1 2 5 4 5 4 5 7 3 9 2 6 0 5 Bina Nusantara University 2 3 3 2 19 …and the rest of column 1 3 0 1 1 3 2 3 0 4 4 6 0 5 4 3 9 10 Bina Nusantara University 20 4 5 4 4 3 3 2 3 4 0 1 2 5 2 9 1 2 8 3 0 9 2 5 6 1 2 7 14 5 14 2 4 5 3 2 20 …and the next column 3 0 1 1 3 2 3 0 4 4 6 0 5 4 3 9 10 Bina Nusantara University 4 20 7 4 17 3 22 30 4 3 2 3 4 0 1 2 5 2 9 1 2 8 3 0 9 2 5 6 1 2 5 7 14 5 14 2 4 5 3 2 21 …and the next 3 0 1 1 3 2 3 0 4 4 6 0 5 4 3 9 10 Bina Nusantara University 4 20 7 4 4 13 17 3 22 3 22 30 1 2 5 2 32 3 1 2 0 9 4 20 8 3 2 2 5 6 1 2 0 9 5 7 14 5 14 2 4 5 3 2 22 …and the last 3 0 1 1 3 2 3 0 4 4 6 0 5 4 3 9 10 Bina Nusantara University 4 20 7 4 4 13 17 3 22 3 30 3 15 1 4 20 2 0 22 8 3 2 9 2 5 6 1 2 0 9 5 7 14 5 14 2 4 5 2 24 25 5 2 32 1 3 2 34 23 Recurrence relation si1, j weight((i 1, j) (i, j) si, j max si, j1 weight((i, j 1) (i, j) Bina Nusantara University 24 Manhattan DP Algorithm ManhattanTouristDP(vert, horiz, n, m) s(0,0) <- 0 for i <- 1 to n s(i,0) <- s(i-1,0) + vert(i,0) for j <- 1 to m s(0,j) <- s(0,j-1) + horiz(0,j) for i <- 1 to n { for j <- 1 to m { s(i,j) <- max{ s(i-1,j) + vert(i,j), s(i,j-1) + horiz(i,j) } } } return s(n,m) Bina Nusantara University 25 What if the grid is not regular? • Model the street system as a directed graph. The intersections are vertices, and all the streets are oneway. – We assume that there are no cycles in the graph; it is a directed acyclic graph (DAG) • Represent the graph as a set of vertices and a set of weighted edges: G = (V,E) • Number vertices from 1 to |V| with a single integer; we drop the matrix formalism. Bina Nusantara University 26 Longest Path DAG Problem: • Input: A weighted DAG G with source and sink vertices. • Output: the longest (optimal) path between source and sink. • Notation: – Write a directed edge as a pair of vertices, (u,v). – The indegree of a vertex is the number of incoming edges; the outdegree the number that exit. – Vertex u is a predecessor to v if (u,v) is in E; if v has indegree k that means it has k predecessors. Bina Nusantara University 27 Modified recurrence relation: sv max uPredecessors(v) su weight(u v) A problem: In what order to we visit the vertices?? By the time vertex v is visited, all of its predecessors must have already been visited! An ordering of vertices v1,v2,…,vn is topological if for every edge (vi,vj) in the DAG, i < j. If the ordering is topological, all is well because whenever we visit a vertex, we are guaranteed to have already visited its predecessors. Bina Nusantara University 28 Bina Nusantara University 29