**Title:**Determine n matrix connection multiplication A1A2A3 … an**calculation order**, so that the number of “number multiplier” required for the calculation matrix is calculated according to this sequence.

- This problem meets the conditions of dynamic planning
- matrix multiplication to satisfy
**-combined law** - Two matrix multiplication, to meet the number of columns of the left matrix = the number of rows of the right matrix
- two matrix multiplication, the calculation amount is: the number of rows on the left matrix
**multiplied**The number of columns on the left matrix (ie: the number of rows on the right matrix)The number of columns on the right matrix - Question is not actually a multiplication, but just
**The order of the matrix multiplication involved in the decision**

**Thinking:**

Bring the matrix multiplication product to {p (i-1), Pi, P (i+1), …, pk, …, pj}:

- p (i-1) is a matrix AI
**line number**， - PI is a matrix AI
**column number**, also the matrix a (i+1)**line number** - p (i+1) is the matrix matrix a (i+1)
**column number** - is: {p (i-1), Pi, P (i+1)} indicates two matrix AIA (i+1) multiplication

Assuming the last part of the optimal calculation order of the matrix multiplication A [i: j] is interrupted between matrix AK and matrix A (k+1),

K Position is only J-1 possibility

calculation quantity: the optimal calculation amount of: A [i: K] + A [K + 1: J] optimal calculation amount + the calculation amount of the two matrix left by the last multiplication operation (as shown below)

recursive algorithm (memorandum of use: solve the problem of overlapping subtrays, improve algorithm efficiency):

```
#include <iostream>
using namespace std;
#Define MaxSize 20
void matrixchain (int *p, int n, int m [] [] [maxsize], int s [] [] [maxsize]) {//
int i, j, r, k;
int T; // t is the middle amount
for (i = 1; i <= n; i ++) {
m [i] [i] = 0; // Set up a single matrix to 0, because the calculation in the figure above will appear, so it is set to 0
}
For (r = 2; r <= n; r ++) {// r means the number of matrix in the connection matrix
For (i = 1; I <= n-r+1; i ++) {// i indicates the first one of the connection matrix
j = i + r -1; // j means the last one of the connection matrix
m [i] [j] = m [i + 1] [j] + p [i-1] * p [i] * p [j]; // Here I give K to K (refer to the calculated in the figure above)
s [i] [j] = i;
for (k = i+1; k <j; k ++) {
t = m [i] [k] + m [k + 1] [j] + p [i-1] * p [k] * p [j]; // (calculated in the figure above)
if (t <m [i] [j]) {
m [i] [j] = t;
s [i] [j] = k;
}
}
}
}
COUT << Endl << "The best multiplication:" << m [1] [n] << Endl;
}
void traceback (int i, int j, int s [] [] [maxsize]) {// optimal multiplication path path
if (i <j-1) {{
Traceback (i, s [i] [j], s);
Traceback (s [i] [j]+1, j, s);
COUT << "Matrix a" << i << "," << s [i] [j] << "<< s [i] +1 <<", "<< j <", "j <<<< j <" <endl;
}
}
int main () {)
int m [maxsize] [maxsize], // m [i] [j] recorded from I to J required at least the number
s [maxsize] [maxsize]; // s [i] [j] corresponding to the disconnection position of m [i] [j]
int p [maxsize]; // Store the number of lines of matrix, the number of columns
int n;
do {
COUT << "Please enter the number of matrices connected to the matrix (2 to 20) n =";
cin >> n.
} While (n <2 || n> 20);
Cout << "Please enter the number of rows of n matrices in turn and the number of columns of the last matrix (input n+1 number):" << endl;
for (int i = 0; i <n+1; i ++) {
cin >> p [i];
}
Matrixchain (p, n, m, s);
Traceback (1, n, s);
}
```

Reference materials:

China University MOOK algorithm design and analysis 5.3-> 5.5