Optimal matrix concatenation combined with Dynamic Programming

Source: Internet
Author: User

Optimal matrix concatenation combined with Dynamic Programming
1. introduction multi-matrix concatenation for general matrix multiplication, such as matrix A (m, n) and matrix B (n, p) the number of addition times required for multiplication is m * n * p multiplication. Because matrix multiplication satisfies the combination Law, the combination of matrix multiplication affects the number of multiplication executions of the entire calculation expression. In the following example, A (1300), B (), C (): (1) (AB) C) Perform A multiplication of times (2) (A (BC) the number of multiplication performed is 450. 2. optimal matrix combination expression (1) set the matrix concatenation product AiAi + 1... Aj is abbreviated as A [I: j]. If the optimal calculation order is set to be broken between Ak and Ak + 1, the brackets are used as follows: (AiAi + 1... Ak) (Ak + 1... (Aj) In this Order, calculate A [I: k] And A [k + 1: j] And then multiply the calculation result. The calculation is A [I: k] calculation amount + A [K + 1: j] calculation amount + the calculation amount of the Two multiplied here the key is: computing A [I: the two subprocesses (computing A [I: k] And A [K + 1: j]) in the optimal order are also the optimal order (2) the specific calculation design calculates the number of multiplication times required by A [I, j] As m [I, j]. M [I, j] = 0; (I = j, indicating a matrix, of course, multiplication is not required) M [I, j] = min (M [I, k] + M [k + 1, j] + pi * pk * pj); (k is taken between [I, j) to indicate the position of the split point, find the most suitable split point to minimize the number of multiplications) the following uses dynamic planning to calculate 6 matrix concatenation. You can use bottom-up calculation to calculate the Split points of the matrix. For example, calculate the product of two matrices in 01, calculate the product of three matrices in 02, and calculate the product of four matrices in 03: 01 12 23 34 45 02 13 24 35 03 14 25 04 15 05 3. the program instance program can generate an optimal multiplication expression based on the rows and columns of multiple matrices. 1 # include <iostream> 2 # include <vector> 3 # include <algorithm> 4 # include <limits. h> 5 # include <string> 6 using namespace std; 7 // calculate M Matrix 8 int calculate_M (vector <int> & num, vector <pair <int, int >>> & data, vector <int> & points) {9 int len = data. size (); 10 for (int span = 1; span <len; span ++) {// distance between 11 for (int col = 0; col <len-span; col ++) {// operation start column 12 13 for (int I = col; I <col + span; I ++) {14 int tmp = num [col] [I] + num [I + 1] [col + span] + data [col]. first * data [I]. second * data [col + span]. second; 15 if (tmp <num [col] [col + span]) {16 points [col] [col + span] = I; /// record split point 17 num [col] [col + span] = tmp; // record minimum multiplication times 18} 19} 20} 21} 22 return 0; 23} 24 25 // generate the final matrix multiplication expression 26 string make_result (vector <int> & points, int t1, int t2) based on the recorded split point) {27 if (t1 = t2) 28 return string (1, 'A' + t1); 29 int split = points [t1] [t2]; 30 return "(" + make_result (points, t1, split) + "*" + make_result (points, split + 1, t2) + ")"; 31} 32 33 int main () 34 {35 vector <pair <int, int> data; // Save the matrix's rows and columns with 36 data. push_back (make_pair (10,100); // A37 data. push_back (make_pair (100,5); // B38 data. push_back (make_pair (5, 25); // C39 data. push_back (make_pair (25, 15); // D40 data. push_back (make_pair (15, 20); // E41 42 43 int len = data. size (); 44 vector <int> num (len, vector <int> (len); // defines two-dimensional vectors and pre-allocates space, record multiplication times 45 vector <int> points (len, vector <int> (len); // defines two-dimensional vectors and pre-allocates space, record split point 46 for (int I = 0; I <len; I ++) {47 for (int j = 0; j <len; j ++) {48 points [I] [j] =-1; 49 if (I = j) 50 num [I] [j] = 0; // multiply by yourself, so it is 051 else52 num [I] [j] = INT_MAX; // otherwise, it is recorded as the maximum integer 53} 54} 55 56 calculate_M (num, data, points ); 57 cout <make_result (points, 0, len-1) <"\ t least multiplication times:" <num [0] [len-1] <endl; 58 return 0; 59}

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.