Last active
October 30, 2015 04:13
-
-
Save jitsceait/ddfd2e110c114850f0c6 to your computer and use it in GitHub Desktop.
Program to find optimum way to multiply matrices.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdlib.h> | |
| #include<stdio.h> | |
| int matrix multiplication(int p[], int N){ | |
| int L,i, j, temp; | |
| int M[N+1][N+1]; | |
| for(i=0;i<=N; i++){ | |
| M[i][i] = 0; | |
| } | |
| for(L=2; L<=N; L++){ | |
| /* For every position i, we check every chain of len L */ | |
| for(i=1; i<N-L+1; i++){ | |
| j = i+L-1; | |
| M[i,j] = MAX_INT; | |
| /* For matrix i to j, check every split K */ | |
| for(k=i; k<j; k++){ | |
| temp = M[i][k] + M[k+1][j] + p[i-1] * p[k] * p[j]; | |
| /* Check if the current count is less than minimum */ | |
| if(temp < M[i][j]){ | |
| M[i][j] = temp; | |
| } | |
| } | |
| } | |
| } | |
| return M[1][N]; | |
| } | |
| /* Driver program to run above code */ | |
| int main(){ | |
| int p [] ={10, 20, 30, 40, 30}; | |
| int n = sizeof(p)/sizeof(p[0]); | |
| printf("%d\n", matrix_multiplication(p,n)); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment