Experiment 5 Dynamic Programming (2) be familiar with matrix continually multiply algorithm, design a dynamic programming algorithm to solve

1, experimental purposes and tasks

  1. Step master basic dynamic programming algorithm: find the optimal solution of the nature and characterization of the structure wherein; recursively defined optimal value; bottom-up fashion to calculate the optimal value; information obtained by calculating the optimum value in accordance with optimal solution configuration.
  2. Familiar matrix continually multiply algorithm, design a dynamic programming algorithm to solve.

Second, the experimental environment

c++

Third, the experimental content

Given n matrix < A . 1 , A 2 , ..., A n >, where Ai and Ai + 1 is multiplicative, I =. 1 , 2 ... , n . Matrix  A I  dimension to P I-. 1 '  P I, how to determine matrix calculation calculated even order the product, so that even in this order compute the matrix multiplication product of the minimum number required.

Design a dynamic programming algorithm to determine the order of evaluation even calculate the product matrix such that even in this order of the matrix multiplication to calculate the required minimum number of multiplication.

Randomly generated 10 or more characters in the input file input.txt, such as: P = {30,35,15,5,10,20,25}

Fourth, the algorithm description

Fake code:

 

 

Fifth, the execution of the algorithm

Since the matrix multiplication of matrix multiplication requires two dimensions satisfy: the number of columns of a matrix to the same number of rows in the second matrix. So long as we can represent all of the dimensions of the matrix with the numbers N + 1, here we use d to represent these numbers N + 1, where di and di + 1 represent the i-th number of matrix rows and columns.

Sixth, the algorithm time complexity analysis

Using the idea of ​​dynamic programming to solve the complexity of the time matrix sequence itself continually multiply problems with algorithms to calculate the relevant matrix BB, BB matrix needs to be calculated on the entire triangular section, we derive the step by step:

 

Column 11: Calculated B (0,1) B (0,1): 11-0 = 1-0 = 1 required computations.

 

Of 22: Calculated B (1,2) B (1,2): calculated = 12-1 = 2-1 require 1.

Of 22: Calculated B (0,2) B (0,2): 22-0 = 2-0 = 2 requires calculations.

 

Of 33: Calculated B (2,3) B (2,3): 13-2 = 3-2 = 1 required computations.

Of 33: Calculated B (1,3) B (1,3): 23-1 = 3-1 = 2 requires calculations.

Of 33: Calculated B (0,3) B (0,3): 33-0 = 3-0 = 3 required calculations.

⋮⋮

The first N-1N-1 rows: computing B (N-2, N-1) B (N-2, N-1): requires (N-1) - (N-2) = 1 (N-1) - (N-2) = 1 computations.

The first N-1N-1 rows: computing B (N-3, N-1) B (N-3, N-1): requires (N-1) - (N-3) = 2 (N-1) - (N-3) = 2 calculations.

The first N-1N-1 rows: computing B (N-4, N-1) B (N-4, N-1): requires (N-1) - (N-4) = 3 (N-1) - (N-4) = 3 computations.

⋮⋮

The first N-1N-1 rows: computing B (0, N-1) B (0, N-1): requires (N-1) - (0) = N-1 (N-1) - (0) = N-1 computations.

Therefore, computation time complexity:

 

Although the algorithm time complexity is O (N3), we only need to store a matrix on it, so the space complexity is O (N2)

Eight, results analysis

Source:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<time.h>
#include<stdlib.h>
#include<fstream>
#include<string.h>

using namespace std;
const int MAX = 100;
const int n=10;
int p[MAX+1],m[MAX][MAX],s[MAX][MAX];
int a[n];
void creatInput(){
	srand(time(NULL));
	FILE *p;
	p = fopen("input.txt", "w");
	if (!p){
		cout << "文件打开异常!" << endl;
		return ;
	}
	for (int i = 0; i <= n;++i){
		fprintf(p, "%d\n", rand()%30);
	}
	fclose(p);


}

void readInput(){
	int dl = 0;
	ifstream file("input.txt");
	while(!file.eof()){
		file>>a[dl];
		++dl;
	}
    file.close();

}


void matrixChain()
{
    for(int i=1; i<=n; i++)
        m[i][i]=0;
    for(int r=2; r<=n; r++)
    {
        for(int i=1; i<=n-r+1; i++) 
        {
            int j=i+r-1;
            m[i][j]=m[i+1][j]+p[i-1]*p[i]*p[j];
            s[i][j]=i;
            for(int k=i+1; k<j; k++)
            {
                int t=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
                if(t<m[i][j])
                {
                    s[i][j]=k;
                    m[i][j]=t;
                }
            }
        }
    }
}
void traceback(int i,int j)
{
    if(i==j)
        return;
    traceback(i,s[i][j]);
    traceback(s[i][j]+1,j);
    cout<<"Multiply A"<<i<<","<<s[i][j]<<"and A"<<s[i][j]+1<<","<<j<<endl;
}
int main()
{
	creatInput();
	readInput();
	for(int i = 0;i <= n;++i){
		p[i] = a[i];
	}
	for(int j : a)
		cout<<j<<" ";
	cout<<endl;
    matrixChain();
    traceback(1,n);
    cout<<m[1][n]<<endl;
    return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_38971487/article/details/91890989