## Example of Matrix Chain Multiplication
Let us proceed with working away from the diagonal. We compute the optimal solution for the product of 2 matrices. Here P On the basis of sequence, we make a formula In Dynamic Programming, initialization of every method done by '0'.So we initialize it by '0'.It will sort out diagonally. We have to sort out all the combination but the minimum output combination is taken into consideration.
- We initialize the diagonal element with equal i,j value with '0'.
- 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] = M - There are two cases by which we can solve this multiplication: ( M
_{1}x M_{2}) + M_{3}, M_{1}+ (M_{2}x M_{3}) - After solving both cases we choose the case in which minimum output is there.
As Comparing both output M [2, 4] = M - There are two cases by which we can solve this multiplication: (M
_{2}x M_{3})+M_{4}, M_{2}+(M_{3}x M_{4}) - After solving both cases we choose the case in which minimum output is there.
As Comparing both output M [3, 5] = M - There are two cases by which we can solve this multiplication: ( M
_{3}x M_{4}) + M_{5}, M_{3}+ ( M_{4}xM_{5}) - After solving both cases we choose the case in which minimum output is there.
M [3, 5] = 1140 As Comparing both output Now Product of 4 matrices: M [1, 4] = M There are three cases by which we can solve this multiplication: - ( M
_{1}x M_{2}x M_{3}) M_{4} - M
_{1}x(M_{2}x M_{3}x M_{4}) - (M
_{1}xM_{2}) x ( M_{3}x M_{4})
After solving these cases we choose the case in which minimum output is there
As comparing the output of different cases then ' M [2, 5] = M There are three cases by which we can solve this multiplication: - (M
_{2}x M_{3}x M_{4})x M_{5} - M
_{2}x( M_{3}x M_{4}x M_{5}) - (M
_{2}x M_{3})x ( M_{4}x M_{5})
After solving these cases we choose the case in which minimum output is there M [2, 5] = 1350 As comparing the output of different cases then '
M [1, 5] = M There are five cases by which we can solve this multiplication: - (M
_{1}x M_{2}xM_{3}x M_{4})x M_{5} - M
_{1}x( M_{2}xM_{3}x M_{4}xM_{5}) - (M
_{1}x M_{2}xM_{3})x M_{4}xM_{5} - M
_{1}x M_{2}x(M_{3}x M_{4}xM_{5})
After solving these cases we choose the case in which minimum output is there M [1, 5] = 1344 As comparing the output of different cases then '
The algorithm first computes m [i, j] ← 0 for i=1, 2, 3.....n, the minimum costs for the chain of length 1. Next TopicMatrix Chain Multiplication Algorithm |