matrix_chain_multiplication_by_dynamiic_programming
matrix_chain_multiplication_by_dynamiic_programming
The
matrices have size 4 x 10, 10 x 3, 3 x 12, 12 x 20, 20 x 7. We need to
compute M [i,j], 0 ≤ i, j≤ 5. We know M [i, i] = 0 for all i.
Let us proceed with working away from the diagonal. We compute the
optimal solution for the product of 2 matrices.
We have to sort out all the combination but the minimum output
combination is taken into consideration.
2. m (2, 3) = m2 x m3
= 10 x 3 x 3 x 12
= 10 x 3 x 12 = 360
3. m (3, 4) = m3 x m4
= 3 x 12 x 12 x 20
= 3 x 12 x 20 = 720
4. m (4,5) = m4 x m5
= 12 x 20 x 20 x 7
= 12 x 20 x 7 = 1680
o We initialize the diagonal element with equal i,j value with '0'.
o After that second diagonal is sorted out and we get all the values
corresponded to it
Now the third diagonal will be solved out in the same way.
M [1, 3] = M1 M2 M3
M [2, 4] = M2 M3 M4
M [2, 4] = 1320
M [3, 5] = M3 M4 M5
M [3, 5] = 1140
M [1, 4] = M1 M2 M3 M4
1. ( M1 x M2 x M3) M4
2. M1 x(M2 x M3 x M4)
3. (M1 xM2) x ( M3 x M4)
After solving these cases we choose the case in which minimum output is
there
M [1, 4] =1080
M [2, 5] = M2 M3 M4 M5
1. (M2 x M3 x M4)x M5
2. M2 x( M3 x M4 x M5)
3. (M2 x M3)x ( M4 x M5)
After solving these cases we choose the case in which minimum output is
there
M [2, 5] = 1350
M [1, 5] = M1 M2 M3 M4 M5
1. (M1 x M2 xM3 x M4 )x M5
2. M1 x( M2 xM3 x M4 xM5)
3. (M1 x M2 xM3)x M4 xM5
4. M1 x M2x(M3 x M4 xM5)
After solving these cases we choose the case in which minimum output is
there
M [1, 5] = 1344
The algorithm first computes m [i, j] ← 0 for i=1, 2, 3.....n, the minimum
costs for the chain of