Lecture 4 Dynamic Programming
Lecture 4 Dynamic Programming
Dynamic Programming
Greedy Approach: ?
Dynamic Programming
Recursive Equation
Optimal Substructure
Recursive Equations:
Function Call itself
Optimal Sub-Structure
Optimal solution to a problem contains optimal solution to sub-
Problem.
Overlapping Sub-problem: Repeating sub-problem
A function call another function that also part of another function of
problem.
Advantage
+ Dynamic Programming reduce Time Complexity
Dynamic Programming
Recursion Methodologies
There are two way solve recursive solution
• Top-Down: use recursion
• Bottom-Up: use Memoization or Tabulation
• use recursive Equation and For Loop
Fibonacci Series
Fib(n)= 0 if n=0
1 if n=1
fib(n-2)+fib(n-1)
Int fib( int n)
{
if (n<=1)
return n;
Else
return(fib(n-2)+fib(n-1)
}
Return M[n];
}
already calculated …
Fibonacci Series
Store the result into Global array
Total call=6
T(n)= n+1
T(n)= O(n)
This is called Memoization.
Memoization brings big Difference.
This is called Bottom-Up approach.
We use iterative method -> called Tabulation Method
Dynamic Problem
Dynamic programming is a method of solving optimization
problems by combining the solutions of subproblems
Developing these algorithms follows four steps:
• Characterize the optimality - formally state what properties an
optimal solution exhibits
• Recursively define an optimal solution - analyze the problem in
a top-down fashion to determine how subproblems relate to the
original
• Solve the subproblems - start with a base case and solve the sub-
problems in a bottom-up manner to find the optimal value
• Reconstruct the optimal solution - (optionally) determine the
solution that produces the optimal value
Rod Cutting Problem
Assume a company buys long steel rods and cuts them into shorter
rods for sale to its customers. If each cut is free and rods of different
lengths can be sold for different amounts, we wish to determine how
to best cut the original rods to maximize the revenue.
• Let the length of the rod be n inches. There are 2n-1 different ways to cut
the rod.
To find the optimal value we simply add up the prices for all the
pieces of each permutation and select the highest value.
lengths i1, i2, ... , ik. such that n = i1 + i2 + ... + ik, then the revenue
for a rod of length n is
Rod Cutting Problem
Optimal Substructure:
Rod Cutting Problem
where T(j) is the number of times the recursion occurs for each iteration
of the for loop with j = n-i. The solution to this recursion can be shown
to be T(n) = 2n which is still exponential behavior.
The problem with the top-down naive solution is that we recomputed
all possible cuts thus producing the same run time as brute-force (only
in a recursive fashion).
Rod Cutting Problem: Bottom-Up
the run time can be drastically improved (at the cost of additional
memory usage).
Total Length
Piece 0 1 2 3 4 5
Profit ↓Length
0
0 0 0 0 0 0 0
→
1
Length of Profit 2 0 2 4 6 8 10
Pieces per Piece 2 ↓
1 2 5 0 2 5 7 10 12
2 5 3 ↓ ↓
3 9 9 0 2 5 9 11 14
4 6 4 ↓ ↓ ↓ ↓ ↓
6 0 2 5 9 11 14
Note that to compute any rj we only need the values r0 to rj-1 which we
store in an array.
Hence we will compute the new element using only previously
computed values. The implementation of this approach is
Rod Cutting Problem: Bottom-Up
Rod Cutting Problem: Extended Bottom-Up
Matrix Multiplication: Dynamic Programming
An example: A1 A2 A3
A1 : 10 100
A2 : 100 5
A3 : 5 50
28
Matrix-Chain multiplication (cont.)
The problem:
A1, A2 , ..., An
Given a chain of n
matrices, where matrix Ai has dimension pi-1x
30
Elements of dynamic programming (cont.)
Overlapping subproblems: (cont.)
1.
.4
1. 2. 1. 3. 1. 4.
.1 .4 .2 .4 .3 .4
2. 3. 2. 4. 1. 2. 3. 4. 1. 2. 1. 3.
.2 .4 .3 .4 .1 .2 .3 .4 .1 .3 .2 .3
3. 4. 2. 3. 2. 3. 1. 2.
.3 .4 .2 .3 .2 .3 .1 .2
RECURSIVE-MATRIX-CHAIN (p, i, j)
1 if i = j
2 then return 0
3 m[i,j] ← -1
4 for k←i to j-1
5 do q←RECURSIVE-MATRIX-CHAIN (p, i, k)
+ RECURSIVE-MATRIX-CHAIN (p, k+1, j)+ pi-1 pk pj
6 if q < m[i,j]
7 then m[i,j] ←q
8 return m[i,j]
32
Elements of dynamic programming (cont.)
bn (2 n )
34
Matrix-Chain multiplication (cont.)
35
Matrix-Chain multiplication (cont.)
36
Matrix-Chain multiplication (cont.)
0 if i j
m[i, j ] min m[i, k ] m[k 1, j ] p p p } if i j.
i k j i 1 k j
38
Matrix-Chain multiplication (cont.)
39
Matrix-Chain multiplication (cont.)
40
Matrix-Chain multiplication (cont.)
41
Matrix-Chain multiplication (Contd.)
MATRIX-CHAIN-ORDER(p)
n←length[p]-1
for i←1 to n
do m[i,i]←0
for l←2 to n
do for i←1 to n-l+1
do j←i+l-1
m[i,j]← ∞
for k←i to j-1
do q←m[i,k] + m[k+1,j]+pi-1 pk pj
if q < m[i,j]
then m[i,j] ←q
s[i,j] ←k
return m and s
42
Matrix-Chain multiplication (cont.)
An example: matrix dimension
A1 30 x 35
A2 35 x 15
A3 15 x 5
A4 5 x 10
A5 10 x 20
A6 20 x 25
6 1 5 3 2
i
2
j
5 151 4 3 3 3
25 i
j 118 105 3 3 3 4
4 3 3
75 00
4 5 5
3 2 1 3 3
937 712 575
5 5 4 5
2 5 1 2 3
437 250
250 350
787 6
5 00 0
1 157 5 500
262 750 100
1000
50 0
00 00
0 05 0
A A A A A A
1 2 3 4 5 6
44
Matrix-Chain multiplication (cont.)
45
Matrix-Chain multiplication (Contd.)
PRINT-OPTIMAL-PARENS (s, i, j)
1 if i=j
2 then print “Ai”
3 else print “ ( “
4 PRINT-OPTIMAL-PARENS (s, i, s[i,j])
5 PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
6 Print “ ) ”
46
Matrix-Chain multiplication (Contd.)
RUNNING TIME:
47