矩阵链动态规划

矩阵相乘,组合次序不同需要的计算次数不同.

 

1,最基本得两矩阵相乘:

 

/*

 * Author: dengzhaoqun

 * Date:   2011/04/17 

 */

 

#include <stdio.h>

#include <stdlib.h>

 

int *GetMatrix(int row,int col);

int main()

{

int *A,*B,*C;

int rowa,cola;

int rowb,colb;

int i,j,k;

 

 

printf("Input the rows of matrix A: ");

scanf("%d",&rowa);

printf("Input the cols of matrix A: ");

scanf("%d",&cola);

 

A=GetMatrix(rowa,cola);

 

        printf("/n");

printf("Input the rows of matrix B: ");

scanf("%d",&rowb);

printf("Input the cols of matrix B: ");

scanf("%d",&colb);

 

B=GetMatrix(rowb,colb);

 

 

if(cola!=rowb)

{

printf("Incompatible dimensions/n");

return;

}

else

{

C=NULL;

C=(int *)malloc(rowa*colb*sizeof(int));

if(NULL==C)

{

printf("Memory allocating failed/n");

return;

}

}

 

i=0;

for(i;i<rowa;i++)

{

j=0;

for(j;j<colb;j++)

{

C[i*colb+j]=0;

k=0;

for(k;k<cola;k++)

{

                              C[i*colb+j]=C[i*colb+j]+A[i*cola+k]*B[k*colb+j];

}

}

   }

 

 

        printf("/nThe result is: %d * %d /n",rowa,colb);

i=0;

for(i;i<rowa;i++)

{

j=0;

for(j;j<colb;j++)

{

printf("%d ",C[i*colb+j]);

}

printf("/n");

}

 

free(A);

free(B);

free(C);

 

return 0;

}

//----------------end of method main-------------------

 

// ---------the begin of method GetMatrix----

int *GetMatrix(int row,int col)

{

int *M=NULL;

int i,j;

 

M=(int *)malloc(row*col*sizeof(int));

if(NULL==M)

{

printf("Memory allocating failed/n");

return;

}

 

   printf("Input the content "); 

   printf("%d * %d/n",row,col);

 

i=0;

for(i;i<row;i++)

{

j=0;

for(j;j<col;j++)

{

scanf("%d",&M[i*col+j]);

}

}

return M;

}

//-----------------end of method GetMatrix-------------------

 

 

2,矩阵链动态规划的递归实现:

 

/*

 * Author: dengzhaoqun

 * Date:   2011/04/28 

 */

 

#include <stdio.h>

#include <stdlib.h>

 

// the global variables

int *chain=NULL;

long *M=NULL;

int *S=NULL;

 

int *InitInt(int num);

long *InitLong(int num);

void GetChain(int num);

long ChainMul(int len,int i,int j);

void Output(int num);

void Trace(int i,int j,int num);

 

// --- begin of method main ---

int main()

{

int num;

 

 

 

 printf("Input the number of matrixs: ");

scanf("%d",&num);   

 

GetChain(num);

M=InitLong(num);

S=InitInt(num);

ChainMul(num,1,num);

 

Output(num);

 

free(chain);

free(M);

free(S);

return 0;

}

// --- end of method main ---

 

// --- begin method ChainMul ---

long ChainMul(int len,int i,int j)

{

int k;

long temp;

long *this=&(M[(i-1)*len+(j-1)]);

 

if(*this!=-1)

{

return *this; 

 

}

else

{

for(k=i;k<j;k++)

{

temp=ChainMul(len,i,k)+ChainMul(len,k+1,j)+chain[i-1]*chain[k]*chain[j];

if(*this>temp&&(*this!=-1))

{

       *this=temp;

S[(i-1)*len+(j-1)]=k;

}

else if(*this==-1)

{

*this=temp;

S[(i-1)*len+(j-1)]=k;

}

}

 

return *this;

}

 

 

}

// --- end of method ChainMul ---

 

// --- begin method GetChain ---

void GetChain(int num)

{

 

int i;

 

                // the number of matrix is num, so the P(i)'s number is num+1

chain=(int *)malloc((num+1)*sizeof(int));

if(NULL==chain)

{

printf("Memory allocating failed./n");

exit(1);

}

 

 

i=0;

for(i;i<num+1;i++)

{

printf("Input P(%ld): ",i);

scanf("%d",&chain[i]);

}

 

}

// --- end of method GetChain ---

 

 

 

// --- begin method InitLong ---

long *InitLong(int num)

{

 

int i;

int j;

long *MLong;

 

MLong=(long *)malloc(num*num*sizeof(long));

 

if(NULL==MLong)

{

printf("Memory allcation failed./n");

exit(1);

}

// init the MLong

i=0;

for(i;i<num*num;i++)

{

MLong[i]=-1;

}

 

 

i=0;

for(i;i<num;i++)

{

j=0;

for(j;j<num;j++)

{

if(j==i)MLong[i*num+j]=0;

}

}

 

return MLong;

}

 

// --- end of method InitLong ---

 

// --- begin method InitInt ---

int *InitInt(int num)

{

int i;

int *SInt;

 

SInt=NULL;

SInt=(int *)malloc(num*num*sizeof(int));

if(NULL==SInt)

{

printf("Memory allocation failed./n");

exit(1);

}

//init the SInt

i=0;

for(i;i<num*num;i++)

{

SInt[i]=-1;

}

 

return SInt;

}

// --- end of method InitInt ---

 

// --- begin of method Output ---

void Output(int num)

{

printf("The minimum value is %ld/n",M[num-1]);

Trace(1,num,num);

}

 

// --- end of method Output ---

//

// --- begin method Trace ---

void Trace(int i,int j,int num)

{

if(i==j)

{

printf("A[%i]",i);

}

else

{

printf("(");

Trace(i,S[(i-1)*num+(j-1)],num);

Trace(S[(i-1)*num+(j-1)]+1,j,num);

printf(")");

}

}

// --- end of method Trace ---

 

3,矩阵链动态规划迭代实现:

 

/*

 * Author: dengzhaoqun

 * Date:   2011/04/28 

 */

 

#include <stdio.h>

#include <stdlib.h>

 

// the global variables

int *chain=NULL;

long *M=NULL;

int *S=NULL;

 

int *InitInt(int num);

long *InitLong(int num);

void GetChain(int num);

void ChainMul(int num);

void Output(int num);

void Trace(int i,int j,int num);

 

// --- begin of method main ---

int main()

{

int num;

 

 

 

         printf("Input the number of matrixs: ");

scanf("%d",&num);   

 

GetChain(num);

M=InitLong(num);

S=InitInt(num);

                ChainMul(num);

 

Output(num);

 

free(chain);

free(M);

free(S);

return 0;

}

// --- end of method main ---

 

// --- begin method ChainMul ---

void ChainMul(int num)

{

int len,m,n;

long temp;

long *this=NULL;

 

for(len=2;len<num+1;len++)

{

for(m=1;m<num+2-len;m++)

for(n=m;n<m+len-1;n++)

{

temp=M[(m-1)*num+(n-1)]+M[(n+1-1)*num+(m+len-1-1)]+chain[m-1]*chain[n]*chain[m+len-1];

 

this=&(M[(m-1)*num+(m+len-1-1)]);

if(temp<(*this)&&(*this!=-1))

{

*this=temp;

S[(m-1)*num+(m+len-1-1)]=n;

}

else if(*this==-1)

{

*this=temp;

S[(m-1)*num+(m+len-1-1)]=n;

}

}

}

}

// --- end of method ChainMul ---

 

// --- begin method GetChain ---

void GetChain(int num)

{

 

int i;

 

                // the number of matrix is num, so the P(i)'s number is num+1

chain=(int *)malloc((num+1)*sizeof(int));

if(NULL==chain)

{

printf("Memory allocating failed./n");

exit(1);

}

 

 

i=0;

for(i;i<num+1;i++)

{

printf("Input P(%ld): ",i);

scanf("%d",&chain[i]);

}

 

}

// --- end of method GetChain ---

 

 

 

// --- begin method InitLong ---

long *InitLong(int num)

{

 

int i;

int j;

long *MLong;

 

MLong=(long *)malloc(num*num*sizeof(long));

 

if(NULL==MLong)

{

printf("Memory allcation failed./n");

exit(1);

}

// init the MLong

i=0;

for(i;i<num*num;i++)

{

MLong[i]=-1;

}

 

 

i=0;

for(i;i<num;i++)

{

j=0;

for(j;j<num;j++)

{

if(j==i)MLong[i*num+j]=0;

}

}

 

return MLong;

}

 

// --- end of method InitLong ---

 

// --- begin method InitInt ---

int *InitInt(int num)

{

int i;

int *SInt;

 

SInt=NULL;

SInt=(int *)malloc(num*num*sizeof(int));

if(NULL==SInt)

{

printf("Memory allocation failed./n");

exit(1);

}

//init the SInt

i=0;

for(i;i<num*num;i++)

{

SInt[i]=-1;

}

 

return SInt;

}

// --- end of method InitInt ---

 

// --- begin of method Output ---

void Output(int num)

{

printf("The minimum value is %ld/n",M[num-1]);

Trace(1,num,num);

}

 

// --- end of method Output ---

//

// --- begin method Trace ---

void Trace(int i,int j,int num)

{

if(i==j)

{

printf("A[%i]",i);

}

else

{

printf("(");

Trace(i,S[(i-1)*num+(j-1)],num);

Trace(S[(i-1)*num+(j-1)]+1,j,num);

printf(")");

}

}

// --- end of method Trace ---

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值