A Thinking Exercise of the optimal binary search tree
Like the optimal binary search tree, the matrix concatenation problem is also a catlan Number Problem (the construction process of its dynamic planning is)
Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------
Analysis:
A. Math knowledge
First, we need to figure out how to multiply the matrix:
1) Multiply n consecutive matrices by A1 * A2 * a3 ......... an. The order of multiplication can be arbitrary. You can add parentheses to change the order of multiplication. For example, a * B * C can multiply the three matrices by a * (B * C)
You can also directly follow the order from left to right. The bits of two consecutive matrices must satisfy M * p, p * n to be multiplied, and the result after multiplication is a matrix of M * n. (Linear algebra knowledge)
2) For the matrices of 2 m * P and p * n, the number of common multiplications is M * n * P.
This is preliminary knowledge. After knowing that the number of consecutive multiplication operations of a matrix is related to the Operation Sequence, it is easy to give an example.
B. catlan: It turns out to be troublesome. You have time to read the combined math.
Bytes ------------------------------------------------------------------------------------------------------------------------
C. The key point is to solve this problem.
Set M [I, j] to the least multiplication times from the I matrix to the J matrix, (I from 0 ), our final goal is to find M [0, n-1].
AI *... AK * ak + 1... AJ
Assume that we want to obtain the subvalue of this formula (from matrix I concatenation to matrix J). The last matrix multiplication is after matrix K (pay attention to the accurate description position) disconnected (that is, the left and right sides are multiplied), so it is easy to get
Its recurrence formula:
M [I, j] = min {M [I, K] + M [k + 1, j] + DI * Dk + 1 * dj + 1} I <= k <= J-1
Where di is the first dimension of matrix AI, and Dk + 1 is the second dimension of matrix AK (I .e., the first dimension of ak + 1, which is the same ), dj + 1 is the second dimension of the last matrix AJ.
This formula is also a process of reverse thinking.
-------------------------------------------------------------------------------
We can use the dynamic planning and construction process of matrix concatenation to compare it with the optimal binary search tree, and find that the structure is very similar (as mentioned in the previous article about DP, which is called professional, I will not elaborate on it)
Implementation:
Initial Condition: M [I, I] = 0
Table filling order: given that the recursive type is similar to the optimal binary search tree, the order of table filling is also diagonal. you can draw your own pictures.
The code is similar to the control logic of the optimal binary search tree:
Package section8;
/* Chapter 8 exercise after dynamic planning: matrix concatenation */
Public class mateven {
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Int [] Dim = {30, 35, 35, 15, 15, 5, 5, 5, 10, 20, 25 };
Int result = mateven (DIM );
System. Out. println ("\ n the optimal multiplication order obtained by dynamic planning results in the following least multiplication numbers:" + result );
}
Public static int mateven (INT [] Dim ){
// The dim size of the dimension array that accepts n matrices is 2n.
Int n = dim. length/2; // There are n matrices numbered 0... n-1. the dimension of the matrix numbered K is dim [2 K] * dim [2 k + 1].
Int [] [] result = new int [N] [N]; // minimum cost Matrix
// Initialization
For (INT I = 0; I <n; I ++)
Result [I] [I] = 0;
// Fill in the matrix along the diagonal line
For (INT d = 1; D <= n-1; D ++) // fill in a total of N-1 diagonal lines
{
For (INT I = 0; I <= n-d-1; I ++) // the first point of the diagonal line of the D is d
{
// Int J = I-d;
Int J = I + D; // point on the diagonal line of entry D. The relationship between x and y coordinates is J = I + D.
// Determine the coordinates of a position (I, j), and then fill in (I, j)
Int min = 1000000000;
For (int K = I; k <= J-1; k ++) // disconnect from the back of the K Matrix
{
// Dynamic planning state transition equation
Int temp = Result [I] [k] + result [k + 1] [J] + (dim [2 * I] * dim [2 * k + 1] * dim [2 * j + 1]);
If (temp <min)
Min = temp;
}
Result [I] [J] = min;
}
}
Return result [0] [n-1];
}
}
The preceding uses an array to accept the dimension of a concatenation matrix,
For example, the matrix dimension of concatenation is: 30*35 35*15 15*5 5*10 10*20*25
The optimal multiplication times obtained by using dynamic planning are:
The maximum number of multiplications caused by the order of multiplication of the optimal policy for dynamic planning is 15125.
Directly return the Matrix to obtain the value of the entire M [I, j ].
If we perform multiplication from left to right, it is far more than this number.
-------------------------------------------------------------------------
Of course, after some processing, we can get the specific order. Similar to the optimal binary search tree, it is to record the process of dynamic planning.
Bytes ----------------------------------------------------------------------------------------------
Summary:
Matrix concatenation is a catlan number problem.
The construction process of dynamic planning is very similar to the optimal binary search tree.
What is the most sub-structure of matrix concatenation?