-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain matrix multiplication.c
71 lines (57 loc) · 1.54 KB
/
chain matrix multiplication.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <limits.h>
void printOptimalParenthesis(int i, int j, int n, int bracket[][n], char *name) {
if (i == j) {
printf("%c", *name);
(*name)++;
return;
}
printf("(");
printOptimalParenthesis(i, bracket[i][j], n, bracket, name);
printOptimalParenthesis(bracket[i][j] + 1, j, n, bracket, name);
printf(")");
}
int matrixChainMultiplication(int dims[], int n)
{
int i,L,k;
int m[n][n];
int bracket[n][n];
for ( i = 1; i < n; i++)
m[i][i] = 0;
for ( L = 2; L < n; L++)
{
for ( i = 1; i < n - L + 1; i++)
{
int j = i + L - 1;
m[i][j] = INT_MAX;
for ( k = i; k <= j - 1; k++)
{
int cost = m[i][k] + m[k + 1][j] + dims[i - 1] * dims[k] * dims[j];
if (cost < m[i][j])
{
m[i][j] = cost;
bracket[i][j] = k;
}
}
}
}
char name = 'A';
int bracketIndex = 0;
printf("Optimal Parenthesization: ");
printOptimalParenthesis(1, n - 1, n, (int *)bracket, &name);
printf("\nMinimum number of scalar multiplications is %d\n", m[1][n - 1]);
return m[1][n - 1];
}
int main()
{
int n,dims[100],i;
printf("enter number of elements: ");
scanf("%d",&n);
printf("enter array: \n");
for(i=0;i<n;i++)
{
scanf("%d",&dims[i]);
}
matrixChainMultiplication(dims, n);
return 0;
}