0815 ------ algorithm notes ---------- matrix concatenation Problem

Source: Internet
Author: User

1. Definition of matrix concatenation Problem

1.1 given the product a1a2 of N matrices... an, because Matrix Multiplication satisfies the combination Law, the product of the matrix can have different calculation orders (the number of combinations in this order satisfies the catlan number ), the number of Multiply times calculated in different calculation orders is also different. For example, a1a2a3, the dimensions of these three matrices are 10*100,100*5, and 5*50. If a1a2 is calculated first, the total number of computations is 10*100*5 + 10*5*50 = 7500. However, a2a3 is first calculated, the total number of computations is 100*5*50 + 10*100*50 = 75000. The difference in the number of computations is 10 times. Here we use brackets to represent the calculation order of the matrix. Each fully enclosed brackets correspond to the computing order of a matrix;

1.2 What is the matrix concatenation product with full parentheses? The Matrix Products with full brackets can be recursively defined as (see Computer Algorithm Analysis and Design):

A) A single matrix is fully enclosed in parentheses;

B) matrix concatenation product A is fully enclosed by parentheses. Then, a can represent the product of two fully enclosed matrix concatenation products B and C, that is, a = (BC ).

2. Solve the matrix concatenation Problem

2.1 set the matrix concatenation product a1a2... the dimension of AI is P [I-1] * P [I], and m [I] [J] Is AIAI + 1... the smallest multiplication Number of AJ, which satisfies the following recursive relationship:

A) m [I] [J] = 0, I = J;

B) M [I] [J] = min {M [I] [k] + M [k + 1] [J] + P [I-1] * P [k] * P [j]}, I <= k <J;

2.2 based on the above recursive formula, we can write the recursive method of this problem. Here, to find the final calculation order, we need to use a two-dimensional array s to save the location of each disconnection.

# Include <iostream> # include <string> # include <vector> # include <string. h> # define max 100 using namespace STD; int matrixchain (int I, Int J, int * P, INT (* m) [Max], INT (* s) [Max]); void traceback (int I, Int J, INT (* s) [Max]); int main (INT argc, const char * argv []) {int P [Max], M [Max] [Max], s [Max] [Max]; int N; CIN> N; // Number of matrices int I; for (I = 0; I <= N; I ++) {// multiply the number of rows and columns of the matrix chain CIN> P [I];} memset (M,-1, sizeo F m); matrixchain (1, N, P, M, S); traceback (1, N, S); cout <Endl; return 0 ;} int matrixchain (int I, Int J, int * P, INT (* m) [Max], INT (* s) [Max]) {If (M [I] [J]! =-1) return M [I] [J]; if (I = J) return 0; else {int K, min = 1000000; For (k = I; k <j; k ++) {M [I] [k] = matrixchain (I, K, P, M, S ); M [k + 1] [J] = matrixchain (k + 1, J, P, M, S ); int TMP = m [I] [k] + M [k + 1] [J] + P [I-1] * P [k] * P [J]; if (TMP <min) {min = TMP; s [I] [J] = K ;}} return min ;}} void traceback (int I, Int J, INT (* s) [Max]) {if (I = J) {cout <"A" <I; return;} cout <"("; traceback (I, S [I] [J], S); cout <"*"; traceback (s [I] [J] + 1, J, S ); cout <")";}

2.3 The non-recursive method of matrix concatenation is actually a process of filling a table. Taking the product of six matrices as an example, the dimension of the matrix is P [] = {30, 35, 15, 5, 10, 20, 25}, as shown in, set M [I] [I] to 0 first, then calculate M [1] [2], M [2] [3] in the direction of the positive diagonal line. M [5] [6] and so on, for example, M [2] [3] = m [2] [2] + M [3] [3] + P [1] * P [2] * P [3] = 0 + 0 + 30*35*15 = 15750, here, we can only split it by 2, and then calculate M [2] [5], calculate M [2] [2] + M [3] [5] + P [1] * P [2] * P [5], M [2] [3] + M [4] [5] + P [1] * P [3] * P [5], and M [2] [4] + M [5] [5] + P [1] * P [4] * P [5], then the minimum value is m [ 2] [5]. The filled table and source program are as follows:

  1 2 3 4 5 6
1 0 15750 7875 9375 11875 15125
2   0 2625 4375 7125 10500
3     0 750 2500 5375
4       0 1000 3500
5         0 5000
6           0

 

 

    

 

 

 

 

 

# Include <iostream> # include <string> # include <vector> # define max 100 using namespace STD; void matrixchain (int * P, int N, INT (* m) [Max], INT (* s) [Max]); void traceback (int I, Int J, INT (* s) [Max]); int main (INT argc, const char * argv []) {int P [Max], M [Max] [Max], s [Max] [Max]; int N; CIN> N; // The number of matrices int I; for (I = 0; I <= N; I ++) {// multiply the number of rows and columns of the matrix chain CIN> P [I];} matrixchain (p, n, m, S); traceback (1, N, S ); cout <Endl; return 0;} void matrixchain (int * P, int N, INT (* m) [Max], INT (* s) [Max]) {int I; for (I = 1; I <= N; I ++) m [I] [I] = 0; int R; // Number of Outer Loops for (r = 2; r <= N; r ++) {for (I = 1; I <= N-R + 1; I ++) {// solve M [I] [J] Int J = I + R-1; int min = m [I] [I] + M [I + 1] [J] + P [I-1] * P [I] * P [J]; s [I] [J] = I; int K; For (k = I + 1; k <j; k ++) {int TMP = m [I] [k] + M [k + 1] [J] + P [I-1] * P [k] * P [J]; if (TMP <min) {min = TMP; s [I] [J] = K ;}} M [I] [J] = min ;}}} void traceback (int I, Int J, INT (* s) [Max]) {if (I = J) {cout <"A" <I; return ;} cout <"("; traceback (I, S [I] [J], S); cout <"*"; traceback (s [I] [J] + 1, J, S); cout <")";}

  

3. Summary

3.1 is the same as the longest common subsequence. When we use dynamic programming to solve a problem, this problem generally has two obvious characteristics: first, the optimal substructure, that is, the optimal solution of the problem includes the optimal solution of the sub-problem, and the second is that the sub-problem overlaps. That is, some sub-problems are repeatedly computed during recursive solution.

3.2 We usedMemorandum method,That is, the results of each subproblem are saved in the array in the recursive process. If you need to retrieve the results from the array next time, this avoids repeated computation.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.