Welcome To Our Presentation: Topics:Dynamic Programming

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25

Welcome To Our Presentation

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.

If same subproblem is solved several times, we can use

Dynamic programming is applicable when the

subproblems are dependent, that is, when subproblems share subsubproblems.

Difference between DP and Divideand-Conquer


A divide and conquer algorithm does more work

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.

Sub-problems are dependent in DP,where Sub-

problems are not dependent in divide and conquer .

Example
For example we discuses: -Matrix-chain-multiplication. -All pairs shortest-path problem using Floyedwarshall algorithm.

Matrix-chain Multiplication

Suppose we have a sequence or chain A1, A2, , An of n matrices to be multiplied


That is, we want to compute the product A1A2An

There are many possible ways (parenthesizations) to compute the product

Matrix-chain Multiplication

Matrix-chain multiplication problem

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

Dynamic Programming Approach


M[i,j] be the minimum of multiplication needed to compute the matrix Ai..Aj

0 m[i, j ] =
i k< j

if i=j if i<j

min {m[i, k] + m[k+1, j ] + pi-1pk pj }

11-9

Matrix-chain Multiplication

Example: consider the chain A1, A2, A3 of 3 matrices

Let us compute the product A1A2A3

There are 2 possible ways:


1. 2.

(A1(A2A3)) ((A1A2)A3)

Matrix-chain Multiplication

To compute the number of scalar multiplications necessary, we must know:


Algorithm to multiply two matrices Matrix dimensions

Matrix-chain Multiplication

Example: Consider three matrices A23, B32, and C23 There are 2 ways to parenthesize

((AB)C) = D22 C23

AB 232=12 DC 223 =12 BC 323=18 AE 233 =18

(A(BC)) = A23 E33


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

6+6 6+6 6+6 6+6 6+6 6+6 12 12 12 12 12 12 12 12 12


E=33

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.

Algorithm Of Matrix-chain Multiplication


Input: Array p[0n] containing matrix dimensions and n Result: Minimum-cost table m and split table s

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

Constructing Optimal Solution


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

Constructing Optimal Solution


Print- Optimal-Parens(s,i,j)

if(i==j) print AI else print ( Print- Optimal-Parens(s,i,s[I,j]) Print- Optimal-Parens(s,s[I,j]+1,j) print )

Floyds Algorithm

All pairs shortest path

All pairs shortest path


Graph analysis algorithm for finding

shortest paths in a weighted, directed graph


The graph: may contain negative edges A representation: a weight matrix where

W(i,j)=0 if i=j. W(i,j)= if there is no edge between i and j. W(i,j)=weight of edge

The weight matrix and the graph


1

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

1 n rows[W ] // number of vertices

(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.)

You might also like