Simple comprehension algorithm--dynamic programming

Source: Internet
Author: User

The dynamic programming method is often used to solve the optimization problem, there are many kinds of solutions, but we want to find the optimal solution.

Meet two conditions to use dynamic planning

1. With the best sub-structure

2. Sub-problems overlap

What do you mean by these two points? Let's take a look.

Now there is a bar length and price corresponding to the table, Q: Give you a length of n the steel bar How to sell the most cost-effective?

Length 1 2 3 4 5 6 7 8 9 10
Price 1 5 8 9 10 17 17 20 24 30

Now is to take all the cut all over again, find the most cost-effective cutting method, when you cut the steel bar, is not become two paragraph? That is to consider the two paragraphs how to cut the most cost-effective, this will be more complex, may wish us this:

Cut the steel bar, in two halves, but the left half is not considered, sell directly at the price, only consider the right half, so it is relatively simple? Of course, to traverse all the cuts, the left part is incremented from 1 to N. However, now our problem is to cut a steel bar, left motionless, the right side as a separate bar cut, left motionless, the right side .... Is there a taste of recursion? Like this has been repeated to solve the same problem (the right side of the bar Independent as a separate bar, continue to cut) is called sub-problem overlap.

As for what is the optimal sub-structure? As the name implies, the optimal solution of a sub-problem is the optimal solution of the problem. What do you mean? If the reinforcement is divided into a and B, then the optimal solution of A and B is the optimal solution of the original problem.

Does anyone say it's nonsense? Take a look at an example

Now there is a square, the vertex is a,b,c,d, requires a->c shortest path, is not can be divided into A->b and b->c two sub-problems? The shortest path of a->b is of course a->b, then the path is a->b->c, which conforms to the optimal substructure.

But what is the longest path that requires a->b? If the sub-problem method is solved we will be divided into A->c and c->b two sub-problem, a->c the longest path is a->d->c, the same c->b the longest path is c->d->a->b, So the longest path is not a->b? Obviously not, so this does not satisfy the optimal substructure.

Here's the code for the implementation

static void Main (string[] args)
{
Int[] p = new int[]{0,1,5,8,9,10,17,17,20,24,30};
int N=convert.toint32 (Console.ReadLine ());
DateTime Stardt = DateTime.Now;
int Maxp=cutrod (P,N);
DateTime ENDDT = DateTime.Now;
TimeSpan st = Enddt-stardt;
Console.WriteLine (MAXP);//Optimal value
Console.WriteLine (ST);//Consumption time
}
public static int Cutrod (int[] p, int n)
{
int q =-1;
if (n = = 0)
{
return 0;

}
Else
{
for (int i = 1; I <= n; i++)
{
Q = max (q, P[i]+cutrod (P, n-i));
}
return q;
}
}

public static int Max (int a1, int a2)
{
Return A1 > A2? A1:A2;
}

Above the top-down method will repeat the calculation of some sub-problems, resulting in a large time complexity, want to improve, the increase in memos, that is, when the sub-problem is found, use a data structure to write it down, the next time you need to read directly on the line. There is also a way to self-low upward, in fact, is the same, is to use space to change the time

static void Main (string[] args)
{
Int[] p = new int[] {0,1,5,8,9,10,17,17,20,24,30};
int N=convert.toint32 (Console.ReadLine ());
Int[] r = new Int[n+1];
R[0] = 0;
for (int i = 1; I <= n; i++)
{
R[i] =-1;
}
DateTime Stardt = DateTime.Now;
Bottomupcutrod (P,N,R);
DateTime ENDDT = DateTime.Now;
TimeSpan St=enddt-stardt;
Console.WriteLine (R[n]); Output optimal value
Console.WriteLine (ST);//Consumption time

}

public static void Bottomupcutrod (int[] P,int n,int []r]
{
int q =-1;
for (int i = 1; I <= n; i++)
{
for (int j = 1; J <= I; j + +)
{
Q = max (q, p[j] + r[i-j]);
}
R[i] = q;
}
}

public static int Max (int a, int b)
{
Return a > B? A:B;
}

That's it.

Simple comprehension algorithm--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.