Techknow Study

CMM{Chain Matrix Multiplication}

8:22:00 PM vikas 0 Comments Category :

CMM{Chain Matrix Multiplication}

CMM is a techniqueto minimize the multiplication of matrix, when we have more than one matrix.
and the other function of CMM is to parentheses.

Here we have a c program for parentheses the matrix multiplication





#include<stdio.h>
#include<conio.h>
void print(int i,int j);
void matrix(int p[20],int n);
int m[20][20],s[20][20];
void main()
{
int i,j,p[20],n;
clrscr();
printf("enter the no. of elements in p");
 scanf("%d",&n);
 printf("enter the elements of p");
 for(i=0;i<n;i++)
 scanf("%d",&p[i]);
 matrix(p,n);
 printf("solution is \n");
 for(i=1;i<n;i++)
 {
 for(j=1;j<n;j++)
 {
 printf("\t%d",m[i][j]);
 }
 printf("\n");
 }
 printf("split value matrix\n");
 for(i=1;i<n;i++)
 {
 for(j=1;j<n;j++)
 {
 printf("\t%d",s[i][j]);
 }
 printf("\n");
 }
 printf("paranthization is as follows\n");
 print(1,n-1);
 getch();
 }
void matrix(int p[20],int n)
{
 int i,j,k,l,q;
 for(i=1;i<=n;i++)
 {
  m[i][i]=0;
 }
 for(l=2;l<=n;l++)
 {
    for(i=1;i<=n-l+1;i++)
     {
         j=i+l-1;
         m[i][j]=9999;
         for(k=i;k<=j-1;k++)
           {
             q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
             if(q<m[i][j])
              {
                 m[i][j]=q;
                 s[i][j]=k;

               }
               }
     }
 }
}
void print(int i,int j)
{
 if(i==j)
 {
      printf("a%d",i);
       }
 else
 {
  printf("(");
  print(i,s[i][j]);
  print(s[i][j]+1,j);
  printf(")");

 }
}

A pictured output example:-



RELATED POSTS

0 comments