Introduction to algorithms ---------------- matrix chain in Dynamic Planning
[Problem description]
Given the dimension of n concatenation matrices, We need to calculate the number of multiplications used in the optimal calculation order, that is, the minimum number of multiplications required. For example, given the dimensions of the three concatenation matrices {A1, A2, A3} are 10*100,100*5 and 5*50, respectively, using (A1A2) A3, the number of multiplication times is 10*100*5 + 10*5*50 = 7500, while A1 (A2A3 ), the number of multiplication times is 100*5*50 + 10*100*50 = 75000. Obviously, the best order is (A1A2) A3, and the number of multiplication times is 7500.
Analysis:
Matrix chain multiplication Problem description:
Given the sequence [A1, A2,..., An] consisting of n matrices, and the product A1A2... An, we can find the method to minimize the number of multiplication.
1) Find the optimal sub-structure
The most difficult part of this problem is to find the optimal sub-structure. Product A1A2... any Bracket method of An divides the sequence into two parts, that is, the place where the last multiplication is calculated. We will mark this position as k, that is, calculate A1... ak and Ak + 1... and then multiply the results of the two parts.
The optimal sub-structure is as follows: Suppose A1A2... the product of An is separated by the product Ak and Ak + 1, then the prefix sub-chain A1... the angle brackets of Ak must be A1... an accesskey is enclosed in parentheses, And the suffix is the same as that of the sub-chain.
At the beginning, we didn't know the exact position of k. We had to traverse all the locations to ensure that we could find the appropriate k to separate the product.
2) construct recursive Solutions
If m [I, j] is the cost of the optimal solution of matrix chain Ai... Aj, then
Limit 0 if I = j
M [I, j] =
Min (I ≤ k 3) build auxiliary tables to solve overlapping sub-Problems
From the recursion in step 2, we can find that there are many overlapping subproblems in the solution process, an nXn dimension auxiliary table m [n] [n] s [n] [n] [n] can be used to represent the optimal product cost and its split position k, respectively.
The auxiliary table s [n] [n] can be constructed using two methods. One is to build a base-to-top table. This method requires that the subproblem solution be gradually entered in an incremental manner, that is, the solution of all matrix chains with a length of 2 is calculated first, and then the matrix chain with a length of 3 is calculated until the length is n. The other is the top-down form-filling memorandum, this method initializes each element of the table to a special value (in this problem, the optimal product price can be set to a maximum value) to indicate that the value is to be calculated, fill in the solutions to the subproblems encountered one by one in the recursive process.
Here, I use the c ++ container to solve the problem. Generally, the problem caused by the array subscript is extremely easy to travel, or c ++ is more convenient. To solve the problem, the array must use constants to represent the array subscript. If you don't need to talk nonsense, you can read the algorithm introduction tree to find out how to solve the problem and write the code easily.
# Include
# Include
# Include
Using namespace std; pair
>, Vector
> Matrix_chain_order (vector
P) {int n = p. size (); vector
> M (n, vector
(N, 0); // the vector of a vector is equivalent to an array, that is, a two-dimensional array m [] [] vector.
> S (n, vector
(N, 0); // same as s [] [] for (int len = 2; len <n; len ++) {for (int I = 1; I <n-len + 1; ++ I) {int j = I + len-1; m [I] [j] = 0x7fffffff; for (int k = I; k <= J-1; ++ k) {int q = m [I] [k] + m [k + 1] [j] + p [I-1] * p [k] * p [j]; if (q <m [I] [j]) {m [I] [j] = q; s [I] [j] = k ;}}}} return make_pair (m, s);} // print the optimal void print_Optimal_Parens (vector
> S, int I, int j) {if (I = j) cout <"A" <I; else {cout <"("; print_Optimal_Parens (s, i, s [I] [j]); print_Optimal_Parens (s, s [I] [j] + 1, j); cout <")";}} // General Recursive Method int recursive_matrix_chain (vector
P, int I, int j) {int n = p. size (); vector
> M (n, vector
(N, 0); if (I = j) return 0; m [I] [j] = 0x7fffffff; for (int k = I; k <j; k ++) {int q = recursive_matrix_chain (p, I, k) + recursive_matrix_chain (p, k + 1, j) + p [I-1] * p [k] * p [j]; if (q <m [I] [j]) m [I] [j] = q;} return m [I] [j];} // implement int look_up_chain (vector
> M, vector
P, int I, int j) {if (m [I] [j] <0x7fffff) // If m [I] [j] is not infinite, it means m [I] [j] has been calculated by LookUpChain of the next layer and return m [I] [j]; if (I = j) m [I] [j] = 0; for (int k = I; k <j; k ++) {int q = look_up_chain (m, p, I, k) + look_up_chain (m, p, k + 1, j) + p [I-1] * p [k] * p [j]; if (q <m [I] [j]) m [I] [j] = q;} return m [I] [j];} int memoized_matrix_chain (vector
P, int s, int e) {int n = p. size (); vector
> M (n, vector
(N, 0); for (int I = 1; I <n; I ++) {for (int j = I; j <n; ++ j) {m [I] [j] = 0x7fffff ;}} return look_up_chain (m, p, s, e) ;}int main () {vector
P = {30, 35, 15, 5, 10, 20, 25}; vector
> M = matrix_chain_order (p). first; vector
> S = matrix_chain_order (p ). second; cout <"vector m:" <endl; for (size_t I = 0; I <m. size (); I ++) {copy (m [I]. begin (), m [I]. end (), ostream_iterator
(Cout, ""); cout <endl ;}cout <"vector s:" <endl; for (size_t I = 0; I
(Cout, ""); cout <endl ;}cout <"Output Optimal Solution: ---->"; print_Optimal_Parens (s, 1, 6); cout <endl; cout <"--------------------------------------------------" <endl; vector
> M1 = matrix_chain_order (p). first; vector
P1 = {30, 35, 15, 5, 10, 20, 25}; cout <"common Recursive Method:"; cout <recursive_matrix_chain (p1, 1, 6) <endl; cout <"recursive solution with Memo:"; cout <look_up_chain (m1, p1, 1, 6) <endl ;}