Basic Introduction to Dynamic planning

Source: Internet
Author: User

Dynamic planning I believe that we all know, dynamic programming algorithm is also novice in the first contact algorithm design is very distressed, sometimes it is difficult to understand, but after the real understanding, it will feel that dynamic planning is actually not as difficult as imagined. Online also has a lot about the dynamic planning of the article, most of the narrative concept, explain the principle, people find obscure difficult to understand, even if the time to read, found that when they do the problem when they feel at a loss. I think the most important thing is to understand the algorithm is to practice, only through their own practice, you can improve faster. Words do not say, next, I will use an example to step-by-step to explain how dynamic planning is used, only know how to use, in order to better understand, rather than blindly on the concept and principle of repeated pondering.

First of all, let's take a look at this problem (this topic originates from Beida POJ):

Digital triangles (POJ1163)

In the number triangle above, look for a path from the top to the bottom, so that the number of the path is the largest. Each step on the path can only go down the left or right. Just ask for the maximum and then do not have to give a specific path. The number of lines in the triangle is greater than 1 or equal to 100, and the number is 0-99

Input format:

5//Represents the number of lines in the triangle next enter the triangle

7

3 8

8 1 0

2 7 4 4

4 5 2 6 5

Requires output of the most Yamato

Next, we will analyze the idea of solving problems:

First of all, it must be a two-dimensional array to hold the digital triangle.

We then use D (R, J) to denote the number J of line R (r,j from 1).

We use Maxsum (R, J) to represent the sum of the numbers of the best paths in the paths from D (r,j) to the bottom.

Therefore, the final problem of this question becomes the maxsum (1,1)

When we see this topic, the first thing we think of is that we can solve the problem with a simple recursive return:

D (R, J) departs, the next step can only go D (r+1,j) or D (r+1, j+1). So for N-row triangles, we can write the following recursion: [CPP] view Plain Copy if (r = = N) maxsum (r,j) = D (r,j) el Se maxsum (r, j) = max{Maxsum (r+1,j), Maxsum (r+1,j+1)} + D (r,j)

Based on the simple recursion above, we can easily write the complete recursive code:[CPP]  View plain  copy #include  <iostream>     #include  <algorithm>     #define  MAX 101     using namespace std;     int d[max][max];     int n;     int MaxSum (int &NBSP;I,&NBSP;INT&NBSP;J) {           if (i==n)               return D[i][j];            int x = maxsum (i+1,j);            int y = maxsum (i+1,j+1);            return max (x,y) +d[i][j];    }   int main () {            int i,j;            cin >> n;           for (i=1;i<=n;i++)               for (j=1;j<=i;j++)                          cin >> D[i][j];           cout  << maxsum (1,1)  << endl;    }         

For a recursive code like this, when I submit to POJ, the following results are displayed:

Yes, the code runs out of time and why does it timeout?

The answer is simple, because we have repeatedly calculated that when we do recursion, the computer helps us calculate the following diagram:

Take the third row of number 1, when we calculate the maxsum starting with the number 3 from line 2nd, we calculate the maxsum from 1, and when we calculate the maxsum starting from the number 8 in the second row, we calculate the maxsum from 1, which means that there is a repeat calculation. This wastes a lot of time. That is, if a recursive method is used to traverse each path in depth, there is a large number of repetitive computations. The time complexity is 2 of n times, for n = 100 rows, definitely timed out.

Next, we have to consider how to improve, we naturally think that if each of the Maxsum (R,J) is saved, the next time the value of the use of the direct access, you can avoid duplication of calculation. Then you can use the time complexity of N square to complete the calculation. Because the total number of triangles is n (n+1)/2

    based on this idea, we can improve the code above to make it a recursive dynamic programming program:  [CPP]   View plain   Copy #include  <iostream>     #include  <algorithm>    using namespace std;       #define  MAX 101         int d[max][max];       int n;    

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.