The minimum path of matrix and space compression of two-dimensional dynamic programming

Source: Internet
Author: User

Title: Given a matrix, starting from the upper left corner can only move to the right or down, and finally to the lower right corner of the position, all the numbers on the path add up as the path of the road and. Requires the return of all paths and the smallest path in and.

Example:

The path 1,3,1,0,6,1,0 is the path and smallest in all paths, so it returns its and 12.

Analytical:

This topic is similar to the one before the dynamic planning of the digital triangle problem. There is no doubt that this problem is also solved with dynamic programming. This topic is easy to solve as long as the state and transfer equations are determined. The code is given directly below:

Use path to record paths, for each path[i][j],0 represents dp[i][j] value from above, plus 1 for dp[i][j] value from the left side of the int minPathSum1 (int matrix[][col], int dp[][ COL], int path[][col]) {if (Matrix = = NULL) {return 0;} DP[0][0] = matrix[0][0];//Calculates the value of the first column for (int i = 1; i < row; I + +) {dp[i][0] = Dp[i-1][0] + matrix[i][0];p ath[i][0] = 0;} Calculates the value of the first row for (int j = 1; j < Col; J + +) {Dp[0][j] = Dp[0][j-1] + matrix[0][j];p ath[0][j] = 1;} Calculate the other values for (int i = 1; i < row; i++) {for (int j = 1; j < Col; J + +) {int direction = Dp[i][j-1] < Dp[i-1][j]? 1:0 ;DP [I][j] = (direction?  DP[I][J-1]: dp[i-1][j]) + matrix[i][j];p ath[i][j] = direction;}} Forreturn dp[row-1][col-1];}

This is where the shortest path of each point is stored on the DP and which point the shortest path is stored on path. This allows you to find the shortest path and the path one at a time.

Spatial compression of two-dimensional dynamic programming

The above topic is very simple, not the focus of this article, the following look at the two-dimensional dynamic programming space compression problem. The time complexity of the above dynamic programming is O (M*n), and the spatial complexity is the size O (m*n) of the two-dimensional array. The method of space compression is not to record the solution of all sub-problems. So it is possible to record the first row, the second line, or only one row array ... One calculation. Until the last line, get dp[n-1] is the top left corner to the bottom right corner of the smallest path and.

Code implementation:

int minPathSum2 (int matrix[][col], int dp[]) {dp[0] = matrix[0][0];//calculates the shortest path for the first row for (int j = 1; j < Col; J + +) {Dp[j] = Dp[j -1] + matrix[0][j];} Calculates the other minimum path except the first row and for (int i = 1; i < row, i++) {for (int j = 0; J < Col; J + +) {if (= = = 0) {Dp[j] + = Matrix[i][j];} ELSE{DP[J] = Dp[j-1] < DP[J] [dp[j-1]: Dp[j];DP [j] + = Matrix[i][j];}} For}//forreturn dp[col-1];}

The spatial compression of this two-dimensional dynamic programming can be applied to almost all two-dimensional dynamic programming problems, which saves a lot of space by rolling the update by an array (column array or aerial array). However, in the process of scrolling, the dynamic planning table is constantly updated by the row array or the column array, and the last one is only the minimum path of the last row or the last column of the Dynamic planning table. So the true minimum path cannot be traced back through a dynamic planning table.

The minimum path of matrix and space compression of two-dimensional dynamic programming

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.