7-1 matrix chain multiplication problem (20 points) (thinking + detailed explanation + topic analysis) dynamic programming practice

Keywords: Algorithm Dynamic Programming

1: Title:

The multiplication of matrix is defined as follows: let A be m × The matrix of P, B is p × N, then the product of A and B is m × The matrix of n is written as C=AB, where the element C in row i and column j of matrix C
ij

It can be expressed as: c
ij


k=1
p

a
ik

×b
kj

=a
i1

b
1j

+a
i2

b
2j

+⋯+a
ip

b
pj

.

When multiple matrices are multiplied, the multiplication times required by different calculation sequences are different. For example, A is 50 × 10 matrix, B is 10 × 20 matrix, C is 20 × 5 matrix, there are two ways to calculate ABC: (AB)C and A(BC). The former requires 15000 times of multiplication, while the latter requires only 3500 times.

Set A
1

,A
2

,...,A
n

Is A matrix sequence, A
i

Yes, the order is P
i−1

∗P
i

Matrix of (1 ≤ i ≤ n). Try to determine the multiplication order of the matrix so that A is calculated
1

A
2

...A
n

The total number of times the elements are multiplied in the process is the least.

Input format:
Each input file is A test case, and the first line of each test case gives A positive integer n(1 ≤ n ≤ 100), indicating A total of N matrices A
1

,A
2

,...,A
n

, the second line gives n+1 integers P
0

,P
1

...P
n

, separated by spaces, where 1 ≤ P
i

≤ 100 (0 ≤ i ≤ n), the ith matrix A
i

Yes, the order is P
i−1

∗P
i

Matrix of.

Output format:
The minimum number of multiplications required to obtain the product of the above matrix.

Input example:
A set of inputs is given here. For example:

5
30 35 15 5 10 20

Output example:
The corresponding output is given here. For example:

11875

2: Basic analysis

1. Basic dynamic planning knowledge:

1) : the solving process is a multi-stage decision-making process. Each step deals with a sub problem, which can be used to solve the problem of combinatorial optimization
Meaning: by decomposing the complex original problem into relatively simple subproblems
(2) : optimization principle: any subsequence of an optimal decision sequence itself must be the optimal decision sequence relative to the initial and end states of the subsequence
(3) : what are the characteristics of dynamic programming (whether it is the key to dynamic programming)
Overlapping subproblem, optimal substructure, no aftereffect
◆ overlapping sub problems: sub problems occur repeatedly
◆ optimal substructure: the optimal solution of a problem includes the optimal solution of its subproblem
◆ no aftereffect: once the state of a certain stage is determined, the evolution of the subsequent process will no longer be affected by previous states and decisions.

(4) : dynamic planning and design elements
1> What is the objective function of problem modeling and optimization? What are the constraints?
2> How to divide subproblems (boundaries)?
3> What is the dependency between the optimization function value of the problem and the optimization function value of the subproblem? (recursive equation)
4> . whether the optimization principle is met?
5. > how to define the smallest problem? What is the optimization function value, that is, the initial value?

2: On the matrix multiplication and recursive equation of this problem

3: Example demonstration

3: Train of thought

Idea: when considering here, because multiple matrices are multiplied to find the minimum multiplication times,
For example, A1A2A3A4, the multiplication order will be different according to different division, and then the multiplication times will be different
Division: ((A1A2) * A3) *A4, etc., then when solving the minimum multiplication times, it involves a
Dynamic process
When using the idea of dynamic partition, first create a two-dimensional array to store the data multiplied by different number matrices
The minimum number of multiplication, and then the minimum number of multiplication is obtained according to the division

4: Upper code

/*
	Idea: when considering here, because multiple matrices are multiplied to find the minimum multiplication times,
		  For example, A1*A2*A3*A4, the multiplication order will be different according to different division, and then the multiplication times will be different
		 Division: ((A1*A2) * A3) *A4, etc., then when solving the minimum multiplication times, it involves a
		 Dynamic process 
		 When using the idea of dynamic partition, first create a two-dimensional array to store the data multiplied by different number matrices
		 The minimum number of multiplication, and then the minimum number of multiplication is obtained according to the division 
  */

#include<bits/stdc++.h>
using namespace std;

int N;
int p[1000];
int m[1000][1000];  //m[i][j] means: A(i)A(i+1)(i+2)...A(j); 

int minimum_num(){
	//Initialize the matrix, and the multiplication times of a matrix is 0 
	for(int i = 1; i <= N; i++)
	m[i][i] = 0;
	
	//Add data to the two-dimensional array and update (minimize),
	
	for(int i = N; i >= 1; i--){//i here starts with N, not 1, because once i start with 1  
							    //Then m[1][N] is found in the first cycle, and then m[1][N] is divided 
							    //When updating, some of the values have not been calculated. If you start with N, then the final result is
								//m[1][N], so you can use some of the previously calculated values
		for(int j = i + 1; j <= N; j++){
			//Any m[i][j] can be expressed first, that is, it can be divided from behind I 
			m[i][j] = m[i][i] + m[i+1][j] + p[i-1]*p[i]*p[j]; 
			
			//Partition update
			for(int k = i + 1;k < j; k++){//Here k = i+1, which means that the division starts after i+1,
										 //Because when the above is first expressed, it is already divided from the back of i  
				int temp = m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j];
				
				if(temp < m[i][j]){
					m[i][j] = temp;
				}
					
			} 		
		}							 
		
	} 
	
	return m[1][N];

}


int main(){
	
	cin >> N;
	
	for(int i = 0; i < N + 1; i++){
		cin >> p[i];
	}
	
	int result = minimum_num();
	
	cout << result;
	
} 

Posted by eaglelegend on Sat, 18 Sep 2021 15:23:59 -0700