2. In mathematics or computer science, Dynamic
Programming is a method for solving complex
problems by breaking them down into simpler subproblems. So, Matrix chain multiplication is an ideal
example that demonstrates utility of dynamic
programming.
Engineering applications often have to multiply a large
chain of matrices with large dimensions, for example:
100 matrices of dimension 100×100. We can multiply
this chain of matrices in many different ways, but we
will go for that way which takes lower computations.
3. For example, we are going to multiply 4 matrices:
M1 = 2 x 3
M2 = 3 x 4
M3 = 4 x 5
M4 = 5 x 7
And we have conditions for multiplying matrices:
• We can multiply only two matrices at a time.
• When we go to multiply 2 matrices, the number of
columns of 1st matrix should be same as the number
of rows of 2nd matrix.
4. We can multiply the chain of matrices by following
those conditions in these ways:
M1
M2
M3
M4
=2x3
=3x4
=4x5
=5x7
( M1 M2 )( M3 M4 )
(( M1 M2 ) M3 ) M4
M1 ( M 2 ( M 3 M4 )
( M1 ( M 2 M 3 ) M 4
M1 (( M2 M3 ) M4 )
= 220
= 134
= 266
= 160
= 207
Numbers of the rightmost side is number of total
scalar multiplication. So we have realized that we
can reduce the number of total multiplication and
this reduced time is a fact for a large chain of
matrices.
5. Renaming matrices as Mi and dimensions as Pi - 1 x Pi ,
we have got:
M1 = P0 x P 1
M2 = P1 x P 2
M3 = P 2 x P 3
M4 = P3 x P 4
|
|
|
Mi = Pi – 1 x Pi
6. We will use a formula:
Where C i, j means Mi to Mj .
i.e.: C 1, 4 means M1 to M4 .
And we will use a variable 'k' as follows:
M1 |k=1 M2 M3 M4
M1 M2 |k=2 M3 M4
M1 M2 M3 |k=3 M4
7. The thing we’re going to do is to apply above formula for
every 'k' in the range 'i' to 'j' and pick the lowest value every
step.
C 1 , 4 = min ( C1 , 1 + C2 , 4 + P0 * P1 * P4 , C1 , 2 + C3 , 4 + P0 *
P2 * P4 , C1 , 3 + C4 , 4 + P0 * P3 * P4 ) = min ( 207, 220, 134 )
= 134
C 2, 4 = min ( C2 , 2 + C3 , 4 + P1 * P2 * P4 , C2 , 3 + C4 , 4 + P1 *
P3 * P4 ) = min ( 224, 165 ) = 165
C 1, 3 = min ( C1 , 1 + C2 , 3 + P0 * P1 * P3 , C1 , 2 + C3 , 3 + P0 *
P2 * P3 ) = min ( 90, 64 ) = 64
C 1, 2 = P0 * P1 * P2 = 24
C 2, 3 = P1 * P2 * P3 = 60
C 3, 4 = P2 * P3 * P4 = 140
8. 1.
int Chain( int i, int j )
2.
{
3.
int min = 10000, value, k;
4.
if( i == j ){
5.
return 0;
6.
}
7.
else{
8.
for( k = i; k < j; k++ ){
9.
value = (Chain(i, k) + Chain(k + 1, j) + (dimensions[i-1] *
dimensions[k] * dimensions[j]));
10.
if( min > value ){
11.
min = value;
12.
mat[i][j] = k;
13.
}
14.
}
15.
}
16.
return min;
17. }
9. 1.
int main(void)
2.
{
3.
int result, i;
4.
printf("Enter number of matrices: ");
5.
scanf("%d", &n);
6.
printf("Enter dimensions : ");
7.
for( i = 0; i <= n; i++ ){
8.
scanf("%d", &dimensions[i]);
9.
}
10.
result = Chain(1, n);
11.
printf("nTotal number of multiplications: %d andn", result);
12.
printf("Multiplication order is: ");
13.
PrintOrder( 1, n );
14.
printf("n");
15.
}