Dynamic Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Dynamic Programming

Description:

Dynamic Programming Carrie Williams – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 16
Provided by: D583
Category:

less

Transcript and Presenter's Notes

Title: Dynamic Programming


1
Dynamic Programming
  • Carrie Williams

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

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

4
ExampleMatrix-Chain Multiplication
  • Given A sequence of matrices (A1A2,,An) with Ai
    having dimension mi-1 x m
  • Cost of a Solution The number of operations
    needed to compute A1A2An
  • Optimal Solution The solution with minimal cost

5
MCM Example
  • We have a matrix sequence of (90,20,15,50,180)
  • So we have matrices of the following sizes
  • A1 90 x 20
  • A2 20 x 15
  • A3 15 x 50
  • A4 50 x 180

6
MCM Example Cont.
  • We want to compute A1A2A3A4
  • 5 possible ways
  • (A1(A2(A3A4)))
  • (A1((A2A3)A4))
  • ((A1A2)(A3A4))
  • ((A1(A2A3))A4)
  • (((A1A2)A3)A4)
  • 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
  • OR
  • 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
  • Let Mi,j be the cost of the optimal solution
  • Mi,jmin Mi,k Mk1,j mi-1mkmj
  • iltkltj
  • Now we must compute Mi,j, igt1, jltn, from the
    bottom up

9
Finding the Solution cont.
  • Using the previous example, we need to compute
    Mi,j for igt1, jlt4
  • Mi,i0, for all i
  • The solutions for 2 matrices are the following
  • M1,2 27,000
  • M2,3 15,000
  • M3,4 135,000

10
Chart of Optimal Solutions
4 3 2 1
1 27,000 0
2 15,000 0
3 135,000 0
4 0
11
Computing the Cost of Three Matrices
  • M1,3 min M1,2 M3,3 m0m2m3
  • M1,1 M2,3
    m0m1m3 27,000 0 901550 94,500
  • 0 15,000 902050 105,000
  • M2,4 min M2,3 M4,4 m1m3m4
  • M2,2
    M3,4 m1m2m4
  • 15,000 0 2050180 195,000
  • 0 135,000 2050180 315,000
  • Minimum for M1,3 94,500 ((A1A2)A3)
  • Minimum for M2,4 195,000 (A2(A3A4))

12
Chart of Optimal Solutions
4 3 2 1
1 94,500 27,000 0
2 195,000 15,000 0
3 135,000 0
4 0
13
Computing the Cost of Four Matrices
  • M1,4 min M1,3 M4,4 m0m3m4
  • M1,2 M3,4 m0m2m4
  • M1,1 M2,4 m0m1m4
  • 94,500 0 9050180 904,000
  • 27,000 135,000
    9015180405,000
  • 0 195,000 9020180 519,000
  • Hence, the minimum cost is 405,000, which is
    ((A1A2)(A3A4))

14
Chart of Optimal Solutions
4 3 2 1
1 405,000 94,500 27,000 0
2 195,000 15,000 0
3 135,000 0
4 0
15
Conclusion
  • We used the method of dynamic programming to
    solve the matrix-chain multiplication problem
  • 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-problems
    optimal solutions
Write a Comment
User Comments (0)
About PowerShow.com