Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Programming Carrie Williams. What is Dynamic Programming? Method of breaking the problem into smaller, simpler sub-problems Method of breaking.

Similar presentations


Presentation on theme: "Dynamic Programming Carrie Williams. What is Dynamic Programming? Method of breaking the problem into smaller, simpler sub-problems Method of breaking."— Presentation transcript:

1 Dynamic Programming Carrie Williams

2 What is Dynamic Programming? Method of breaking the problem into smaller, simpler sub-problems Method of breaking the problem into smaller, simpler sub-problems Start with the smallest sub-problems and combine to produce a solution Start with the smallest sub-problems and combine to produce a solution Similar to Divide and Conquer Similar to Divide and Conquer –But uses a bottom-top approach

3 When is it used? Most often used with Optimization Problems Most often used with Optimization Problems When the Brute Force approach becomes too time consuming When the Brute Force approach becomes too time consuming –Brute Force is when you find all possible solutions and compare them

4 Example: Matrix-Chain Multiplication Given: A sequence of matrices (A 1 A 2,…,A n ) with A i having dimension m i-1 x m Given: A sequence of matrices (A 1 A 2,…,A n ) with A i having dimension m i-1 x m Cost of a Solution: The number of operations needed to compute A 1 *A 2 *…*A n Cost of a Solution: The number of operations needed to compute A 1 *A 2 *…*A n Optimal Solution: The solution with minimal cost Optimal Solution: The solution with minimal cost

5 MCM Example We have a matrix sequence of (90,20,15,50,180) We have a matrix sequence of (90,20,15,50,180) So we have matrices of the following sizes: So we have matrices of the following sizes: –A 1 = 90 x 20 –A 2 = 20 x 15 –A 3 = 15 x 50 –A 4 = 50 x 180

6 MCM Example Cont. We want to compute A 1 *A 2 *A 3 *A 4 We want to compute A 1 *A 2 *A 3 *A 4 5 possible ways 5 possible ways –(A 1 (A 2 (A 3 A 4 ))) –(A 1 ((A 2 A 3 )A 4 )) –((A 1 A 2 )(A 3 A 4 )) –((A 1 (A 2 A 3 ))A 4 ) –(((A 1 A 2 )A 3 )A 4 ) Recall that the number of operations needed to compute an m x n matrix with a n x p is mnp Recall that the number of operations needed to compute an m x n matrix with a n x p is mnp

7 MCM Example Cont. We could try all possible solutions and see what solution gives us the least cost We could try all possible solutions and see what solution gives us the least costOR We could use the method of dynamic programming We could use the method of dynamic programming –Finding the optimal solution by finding the optimal solution of sub-problems

8 Finding the Solution Start with the computation of two matrices Start with the computation of two matrices Let M[i,j] be the cost of the optimal solution Let M[i,j] be the cost of the optimal solution M[i,j]=min {M[i,k] + M[k+1,j] + m i-1 m k m j } M[i,j]=min {M[i,k] + M[k+1,j] + m i-1 m k m j } i<k<j i<k<j Now we must compute M[i,j], i>1, j 1, j<n, from the bottom up

9 Finding the Solution cont. Using the previous example, we need to compute M[i,j] for i>1, j 1, j<4 M[i,i]=0, for all i M[i,i]=0, for all i The solutions for 2 matrices are the following: The solutions for 2 matrices are the following: –M[1,2] = 27,000 –M[2,3] = 15,000 –M[3,4] = 135,000

10 Chart of Optimal Solutions 4321 127,0000 215,0000 3135,0000 40

11 Computing the Cost of Three Matrices M[1,3] = min {M[1,2] + M[3,3] + m 0 m 2 m 3 } M[1,3] = min {M[1,2] + M[3,3] + m 0 m 2 m 3 } {M[1,1] + M[2,3] + m 0 m 1 m 3 } = 27,000 + 0 + 90*15*50 = 94,500 {M[1,1] + M[2,3] + m 0 m 1 m 3 } = 27,000 + 0 + 90*15*50 = 94,500 = 0 + 15,000 + 90*20*50 = 105,000 = 0 + 15,000 + 90*20*50 = 105,000 M[2,4] = min {M[2,3] + M[4,4] + m 1 m 3 m 4 } M[2,4] = min {M[2,3] + M[4,4] + m 1 m 3 m 4 } {M[2,2] + M[3,4] + m 1 m 2 m 4 } {M[2,2] + M[3,4] + m 1 m 2 m 4 } = 15,000 + 0 + 20*50*180 = 195,000 = 15,000 + 0 + 20*50*180 = 195,000 = 0 + 135,000 + 20*50*180 = 315,000 = 0 + 135,000 + 20*50*180 = 315,000 Minimum for M[1,3] = 94,500 = ((A 1 A 2 )A 3 ) Minimum for M[1,3] = 94,500 = ((A 1 A 2 )A 3 ) Minimum for M[2,4] = 195,000 = (A 2 (A 3 A 4 )) Minimum for M[2,4] = 195,000 = (A 2 (A 3 A 4 ))

12 Chart of Optimal Solutions 4321 194,50027,0000 2195,00015,0000 3135,0000 40

13 Computing the Cost of Four Matrices M[1,4] = min {M[1,3] + M[4,4] +m 0 m 3 m 4 } M[1,4] = min {M[1,3] + M[4,4] +m 0 m 3 m 4 } {M[1,2] + M[3,4] + m 0 m 2 m 4 } {M[1,2] + M[3,4] + m 0 m 2 m 4 } {M[1,1] + M[2,4] + m 0 m 1 m 4 } {M[1,1] + M[2,4] + m 0 m 1 m 4 } = 94,500 + 0 + 90*50*180 = 904,000 = 94,500 + 0 + 90*50*180 = 904,000 = 27,000 + 135,000 +90*15*180=405,000 = 27,000 + 135,000 +90*15*180=405,000 = 0 + 195,000 + 90*20*180 = 519,000 = 0 + 195,000 + 90*20*180 = 519,000 Hence, the minimum cost is 405,000, which is ((A 1 A 2 )(A 3 A 4 )) Hence, the minimum cost is 405,000, which is ((A 1 A 2 )(A 3 A 4 ))

14 Chart of Optimal Solutions 4321 1405,00094,50027,0000 2195,00015,0000 3135,0000 40

15 Conclusion We used the method of dynamic programming to solve the matrix-chain multiplication problem We used the method of dynamic programming to solve the matrix-chain multiplication problem We broke the problem into three sub- problems We broke the problem into three sub- problems –The minimal cost of two matrices, then three matrices, and finally four matrices We found the solution by using the sub- problem’s optimal solutions We found the solution by using the sub- problem’s optimal solutions


Download ppt "Dynamic Programming Carrie Williams. What is Dynamic Programming? Method of breaking the problem into smaller, simpler sub-problems Method of breaking."

Similar presentations


Ads by Google