Java implementation of dynamic programming solving matrix multiplication problem

Source: Internet
Author: User

First, let's take a look at the four steps of dynamic planning:

1. Identify the nature of the optimal solution and characterize its structure;

2. Recursive definition of the optimal solution;

3. Describe the optimal value in a bottom-up manner;

4. The optimal solution is constructed according to the information obtained when the optimal value is calculated.

The improved dynamic Programming algorithm: Memo method, is to describe the optimal value in the top-down way, for the dynamic planning method and the memo method, the use of the two are as follows:

In general, using a dynamic programming algorithm is better than using a memo method when all sub-problems of a problem are at least once solved. At this point, the dynamic planning algorithm does not have any redundant computations. At the same time, for many problems, it is often possible to reduce the computational time and space requirement of the dynamic programming algorithm by using the table access method of its rules. When some sub-problems in sub-problem space can not be solved, it is advantageous to use the memo method, because it can be seen from the control structure that the method solves only those problems that really need solving.

For the dynamic programming algorithm, we must clear two basic elements, these two elements for the design of the algorithm to solve specific problems, whether the choice of dynamic programming algorithm is instructive:

1 The validity of the algorithm depends on the optimal substructure property of the problem itself : The first step of the design algorithm is usually to characterize the structure of the optimal solution. When the optimal solution of the problem contains the optimal solution of the sub-problem, it is said that the problem has the optimal substructure property. The optimal substructure property of the problem provides an important clue that the problem can be solved by using the dynamic programming algorithm .

In the problem of optimal order of matrix products, it is noted that if a1a2 ... An optimal full-parenthesis method between AK and Ak+1 is broken, then the sub-chain a1a2a3 can be determined ... The complete parenthesis method of AK and ak+1ak+2...an is also optimal, that is, the problem has the optimal substructure property. In the analysis of the optimal substructure property of the problem, the method used is universal. First of all, it is assumed that the sub-problem derived from the original problem is not the optimal solution, and then we try to show that it can construct a better solution than the original problem in this hypothesis, which leads to the contradiction.

In the dynamic programming algorithm, the optimal solution of the whole problem is constructed by using the optimal sub-structure property of the problem and the optimal solution of the recursive sub-problem from the bottom-up method. The space scale of the sub-problem is smaller in algorithm investigation. For example, in the optimal order problem of the product of the proof, the sub-problem space consists of all the unused sub-chains of the Matrix chain. The number of all unused strands is O (n*n), thus the space size of the sub-problem is O (n*n)

2 A dynamic programming algorithm can be used to solve problems should have another basic element is the overlap of sub-problems. When a recursive algorithm is used to solve this problem from top to bottom, each sub-problem is not always a new problem, and some sub-problems are counted repeatedly. The dynamic programming algorithm takes advantage of the overlapping nature of this seed problem, solves each sub-problem only once, then saves it to a table, and when it needs to solve the problem again, it simply uses the constant time to look at the result. Often, the number of sub-problems increases with the size of the problem. Therefore, the use of dynamic programming algorithm usually only requires polynomial time, so as to obtain a higher efficiency of solving problems.

The following is a Java implementation that uses the dynamic programming algorithm to solve the problem of matrix multiplication:

Package Dynamic_planning;public class Strassen {/* * Array[i][j] denotes ai ... AJ multiplies the least number of calculations * s[i][j]=k, which means AI ... The optimal substructure in AJ (j-i+1) matrix is ai ... AK and A (k+1) ... Aj * P[i] represents the number of AI rows, p[i+1] represents the number of AI columns */private int array[][];p rivate int p[];p rivate int s[][];p ublic Strassen () {p=new I Nt[]{2,4,5,5,3};array=new int[4][4];s=new int[4][4];} Public Strassen (int n,int []p) {this.p=new int[n+1];this.array=new int[n][n];this.s=new int[4][4];for (int i=0;i< p.length;i++) this.p[i]=p[i];} /********************* method One, dynamic planning **********************************/public void Martixchain () {int n=array.length;for (int i=0;i<n;i++) array[i][i]=0;for (int r=2;r<=n;r++) {for (int i=0;i<=n-r;i++) {int j=i+r-1;array[i][j]= array[i+1][j]+p[i]*p[i+1]*p[j+1];s[i][j]=i;for (int k=i+1;k<j;k++) {int t=array[i][k]+array[k+1][j]+p[i]*p[k+1] *p[j];if (T<array[i][j]) {array[i][j]=t;s[i][j]=k;}}}} /* If the matrix to be asked is: Ap ... Aq,then a=0,b=q-p */public void traceBack (int a,int b) {if (a<b) {TraceBack (A, s[a][b]); TraceBack (s[a][b]+1, b); System.out. println ("Put a" +a+ "to a" +s[a][b]+ ", in the" a "+ (s[a][b]+1) +" to a "+b+", and then put a "+a+" to a "+b+");}} /********************* Method Two: Memo method *****************************/public int Memorizedmatrixchain () {int n=array.length ; for (int i=0;i<n;i++) {for (int j=i;j<n;j++) array[i][j]=0;} Return Lookupchain (0,n-1);} public int Lookupchain (int a,int b) {if (array[a][b]!=0) return array[a][b];if (a==b) return 0;array[a][b]=lookupchain (A, A) +lookupchain (a+1, b) +p[a]*p[a+1]*p[b+1];s[a][b]=a;for (int k=a+1;k<b;k++) {int T=lookupchain (A, k) +lookUpChain ( K+1, B) +p[a]*p[k+1]*p[b+1];if (T<array[a][b]) {array[a][b]=t;s[a][b]=k;}} return array[a][b];} public static void Main (string[] args) {Strassen strassen=new Strassen ();//strassen.martixchain (); Strassen.memorizedmatrixchain (); Strassen.traceback (0, 3);}}



Java implementation of dynamic programming solving matrix multiplication problem

Related Article

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.