Professional Documents
Culture Documents
Welcome To Our Presentation: Topics:Dynamic Programming
Welcome To Our Presentation: Topics:Dynamic Programming
Welcome To Our Presentation: Topics:Dynamic Programming
Topics:Dynamic Programming
Submitted To: Mr Sushanta Acharjee Lecturer,Dept. of CSE Submitted By: Nurul Huda Kadija Akter
Dynamic Programming
Dynamic programming is an algorithm design method
that can be used when the solution to a problem can be viewed as the result of a sequence of decisions.
Dynamic program solves problems by combining the solution to subproblems. table to store result of a subproblem the first time it is computed and thus never have to recompute it again.
than necessary,repatedly solving the common sub-subproblems. Where DP will solve each of them once and their answers are stored in a table for future use.
Example
For example we discuses: -Matrix-chain-multiplication. -All pairs shortest-path problem using Floyedwarshall algorithm.
Matrix-chain Multiplication
Matrix-chain Multiplication
Given a chain A1, A2, , An of n matrices, where for i=1, 2, , n, matrix Ai has dimension pi-1pi
For example consider three matrices: A1=10 100 P0=10 P1=100 P2=5 P3=50 A2=100 5 A3=5 50
Matrix-chain Multiplication
Parenthesize the product A1A2An such that the total number of scalar multiplications is minimized
Most important thing is in matrix-chain multiplication we are not actually multiplying matrices.Our goal is to determine an order for multiplying matrice that has the lower cost
0 m[i, j ] =
i k< j
if i=j if i<j
11-9
Matrix-chain Multiplication
(A1(A2A3)) ((A1A2)A3)
Matrix-chain Multiplication
Matrix-chain Multiplication
Example: Consider three matrices A23, B32, and C23 There are 2 ways to parenthesize
Total: 24
Total: 36
Matrix-chain Multiplication
A=
1 1 1 1 1 1
B=
2 2 2
2 2 2
A23 C=
B32
3 3 3 3 3 3
C23
Matrix-chain Multiplication
((AB)C) A.B=
1 1 1 1 1 1
2 2 2
2 2 2
2+2 +2 2+2 +2
2+2+2 2+2+2
12 times
6 6
6 6
= D22
Matrix-chain Multiplication
((AB)C) =DC
6 6 6 6
3 3 3 3 3 3
18+18 18+18 18+18 18+18
18+18 18+18
12 times
36 36
36 36
36 36
Total=12+12=24 times .
Matrix-chain Multiplication
Now The Second Way: (A(BC))
2 2 2 2 2 2
6+6 6+6 6+6
=
3 3 3 3 3 3
18 times
Matrix-chain Multiplication
(A(BC))
=AE
1 1 1 1 1 1
12 12 12 . 12 12 12
12 12 12
= 12+12+12 12+12+12 12+12+12 12+12+12 12+12+12 12+12+12 18 times
36 36 36 = 36 36 36 Total=18+18=36 times
Matrix-chain Multiplication
Times to compute (AB)C)=24 Times to compute (A(BC)=36 So The first Parenthesization is faster.
MATRIX-CHAIN-ORDER(p[ ], n) for i 1 to n m[i, i] 0 for l 2 to n for i 1 to n-l+1 j i+l-1 m[i, j] for k i to j-1 q m[i, k] + m[k+1, j] + p[i-1] p[k] p[j] if q < m[i, j] m[i, j] q s[i, j] k return m and s
Our algorithm computes the minimum-cost table m and the split table s The optimal solution can be constructed from the split table s
Each entry s[i, j ]=k shows where to split the product Ai Ai+1 Aj for the minimum cost
Floyds Algorithm
V1 V2 V3 V4 V5 3
V1 0 9
V2 1 0
V3 V4 V5 1 5 3 2 0 4 2 0 3 0
3
5 v5
v1
1
9 2
v2
3
v4
2
4
v3
Floyd-Warshall(W)
2 D(0) W 3 for k 1 to n 4 5 6 do for i 1 to n do for j 1 to n
Algorithm of Floyd-Warshall
(k ) ij
min(
( k 1) ij
( k 1) ik
( k 1) kj
7 return D ( n ) Running time : a zippy (V 3 ). (Small constant of proportion ality, because operations are simple.)