The problem of DP matrix connecting multiplication

Source: Internet
Author: User

A thinking problem of optimal binary search tree

as with the optimal binary search tree, the matrix multiplication problem is also a Cattleya number problem (its dynamic programming construction processVery similar)


Analytical Solutions:
A, paving the mathematical knowledge first to understand how matrix multiplication is multiplied:

1) for continuous n matrix multiplication A1 * A2 *a3 .... An, its multiplication order can be arbitrary, you can add parentheses above, change the order of multiplication, for example, a*b*c three matrix multiplication can be A * (B*C)

You can also directly press the left-to-right order. The number of consecutive two matrices must satisfy m*p,p*n to multiply, and the result after multiplication is a m*n matrix. (Knowledge of linear algebra)

2) for the matrix multiplication of 2 m*p,p*n, the multiplication times are m*n*p times. This is the preparation of knowledge, know that the matrix of successive multiplication of the number of operations related to the sequence of operations, it is easy to cite examples, a little.
B, Cattleya several, prove very troublesome, have time to see the combination of mathematics and then see
C, the focus is to solve this problem.


Set M[i, j] indicates the minimum number of multiplication from the first matrix to the J Matrix, (I numbered from 0), and our ultimate goal is to seek m[0, n-1].

Ai * ..... Ak * Ak+1.....aj

Suppose you want to get the value of this equation (that is, from the matrix I to The Matrix J), the last matrix multiplication is after the matrix K (attention to the exact description of the position) is broken (that is, left and right have been calculated), then easy to get

Its recursive type:

m[i, j] = min{m[i, K] + m[k+1, j] + p[i-1] * p[K] * p[j]} I <= k <= j-1

Where di is the first dimension of Matrix Ai, Dk+1 is the second dimension of the matrix Ak (i.e. the first dimension of ak+1, is the same), Dj+1 is the second dimension of the last matrix Aj.

Getting this formula is also a process of reverse thinking.


It can be found that the structure is very similar to the optimal binary search tree by using the dynamic programming construction process of matrix multiplication (referred to in the previous DP's what is called Professional, no longer detailed)

Realize:

Initial conditions: M[i, i]= 0

Form order: In view of its recursive and the best binary search tree similar, the order is also diagonal, the drawing will know.

The code is similar to the control logic of the optimal binary search tree:

Package Section8;


/* Eighth dynamic planning after class exercise: matrix multiplication */

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,10,10,20,20,25};
Intresult = Mateven (Dim);

System.out.println ("\ n") The least multiplication of the optimal strategy multiplication order of dynamic programming is: "+result";
}

public static int Mateven (Int[]dim) {
The size of the dimension that accepts n matrices is set to a dim of 2n
int n =DIM.LENGTH/2; There are n matrices, numbered 0...n-1, the dimensions of the matrices numbered K are dim[2k] * dim[2k+1]

Int[][]result = new Int[n][n]; Minimum cost matrix

Initialization
for (int i = 0;i < n;i++)
Result[i][i] = 0;

Fill the matrix along the diagonal
for (int d = 1;d <= n-1;d++)//Total n-1 diagonal Line required
{
for (int i = 0;i <= n-d-1;i++)//The first point of the diagonal of section D is the horizontal line D
{
int j = i-d;
Int j = i+ D; The point on the diagonal of section D, the relationship of the horizontal ordinate is j = i + D

This determines the coordinates of a position (I,J) and then fills it (I,J)
int Min = 1000000000;
for (int k = i;k <= j-1;k++)//break from the back of the K matrix
{
Dynamic Programming state transfer 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;
}

}

RETURNRESULT[0][N-1];
}
}



The above uses an array to accept the dimensions of the matrix of a multiplication,

For example, the matrix dimension of the multiplication is: 30*35 35*15 15*5 5*10 10*20 20*25

The best multiplication times obtained by dynamic programming are:


Optimal strategies for dynamic programming the minimum multiplication caused by the multiplication order is: 15125

The value of the entire m[i, J] can be obtained by directly returning the matrix.

If you multiply from left to right, it is much more than this number of times.


-------------------------------------------------------------------------

Of course, to do some more processing, you can also get the specific order, similar to the optimal binary search tree, is to record the process of dynamic planning, a little

----------------------------------------------------------------------------------------------


Summarize:


Matrix multiplication problem is a Cattleya number problem

The structure of dynamic programming is very similar to the optimal binary search tree.

What is the most sub-structure of matrix multiplication?

The problem of DP matrix connecting multiplication

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.