Lec Dynamic Prog 1
Lec Dynamic Prog 1
Applied Algorithms
Fall 2003
Dynamic Programming 1
Dynamic Prog & Div and Conq
Dynamic programming like the divide and conquer
method, solves problem by combining the solutions of
sub problems
Dynamic Programming 4
Optimization problems
Dynamic problem is typically applied to
Optimization Problems
Inoptimization problems there can be many
possible solutions.
Each solution has a value and the task is to find the
solution with the optimal ( Maximum or Minimum)
value. There can be several such solutions.
Dynamic Programming 5
4 steps of Dynamic Programming Algorithm
fib(1) = 1
fib(2) = 1 1, n <= 2
fib(3) = 2 fib(n) = fib(n-1) + fib(n-2), n > 2
fib(4) = 3
fib(5) = 5
... fib(3) = 1 + 1 = 2
fib(4) = 2 + 1 = 3
fib(5) = 2 + 3 = 5
Dynamic Programming 7
Recursive Algorithm: Fibonacci Numbers
Dynamic Programming 8
Benefits of Dynamic Programming
Dynamic Programming 9
Matrix Chain Multiplication
the problem of
Dynamic Programming 10
Matrix Chain Multiplication
We are given a sequence (chain) <A1, A2, A3, ...,An> ,
of n matrices to be multiplied together. And we wish to
compute the product A1A2A3…An
Dynamic Programming 14
Dynamic Programming 15
Matrix Chain Multiplication revisited
Dynamic Programming 16
Counting the number of parenthesization.
Dynamic Programming 17
Dynamic Programming 18
Dynamic Programming 19
Dynamic Programming 20
Dynamic Programming 21
Dynamic Programming 22
Computing the optimal costs
Dynamic Programming 23
Dynamic Programming 24
Example
Matrix Dimension
Let n=6 A1 30 x 35
A2 35 x 15
A3 15 x 5
A4 5 x 10
A5 10 x 20
A6 20 x 25
Dynamic Programming 25
Order Matrix Size Products
A2 x A3 = 35 x 5 2,625
A1 (A2 x A3) = 30 X 5 5,250 + 2,625
Dynamic Programming 26