Home Week-8 Week-10

 

PDSA - Week 9

Dynamic programming

Memoization

 

Example of nth number in Fibonacci series:-

Simple recursive

 

Memoization

 

 

Dynamic programming

 

 

Comparison

 

 

Dynamic programming problems

Grid paths

Memoization vs dynamic programming

 

 

Implementation for Grid Path problem:

Simple recursive

 

Memoization

 

Dynamic programming

 

 

Longest Common Sub Word (LCW)

LCW[i,j]={1+LCW[i+1,j+1],     if  ai=bj0,      if  aibj

 

Implementation

Output

Complexity

𝑂(𝑚𝑛)

 

Longest Common Sub Sequence (LCS)

LCS[i,j]={1+LCS[i+1,j+1],     if  ai=bjmax(LCS[i+1,j],lcs[i,j+1]),      if  aibj

 

Implementation

Output

Complexity

𝑂(𝑚𝑛)

 

 

Edit distance

ED[i,j]={ED[i+1,j+1],     if  ai=bj1+min(ED[i+1,j+1],ED[i+1,j],ED[i,j+1]),      if  aibj

Implementation

Output

Complexity

𝑂(𝑚𝑛)

 

Matrix multiplication

Implementation

Output

Complexity

𝑂(𝑛3)

 

Other implementation

Inductive structure

C[i,j]={ 0,     if  i=jmin[(C[i][k]+C[k+1][j]+dim[i][0]dim[k][1]dim[j][1])for ik<j],      if  i<j

Output

Complexity

𝑂(𝑛3)

Example

For example, we have matrices {M0, M1, M2, M3, M4} and the dimensions list of the given matrices is [[4,10],[10,3],[3,12],[12,20],[20,7]].

Matrix C : -

Here 1344 value is representing minimum number of multiplication steps.

We can identify the order of multiplication of matrix by storing the k value(value of k for which we get minimum steps) in matrix with steps.

Matrix C : -

So initially we have matrices {M0, M1, M2, M3, M4} and at a time 2 matrices we can multiply.

We will check the k value for C[0][4] which is 1, so we can parenthesize the order like{(M0 M1)(M2 M3 M4)} now we have to check the order in the second bracket matrix M2, M3, M4, so we will check the value C[2][4] which is 3 then we can parenthesize the order like ((M2 M3) M4) So, the final order will be (M0 M1)((M2 M3) M4).