Dynamic Programming Algorithm __ algorithm

Source: Internet
Author: User
First, the preface

Dynamic programing algorithm is a kind of mathematical idea which solves the decision problem in a phased way, and the main idea is to be small and trivial . At the same time, dynamic programming has three basic criteria:(1) optimal substructure, (2) boundary Condition and (3) state transition. using dynamic programming method to solve the problem is to find the above three criteria, and to model the dynamic programming of the problem.


Ii. Examples of dynamic programming 1, climbing stairs problem

problem Description: There is a height of 10 steps stairs, from the bottom up, each step can only go up 1 or 2 steps. The procedure is required to find out how many kinds of walking methods there are.

The simplest way to do this is to take a poor approach, which is to enumerate each case in turn, and then get the total number of ways to go, but this is the dumbest way.

Just explained the three basic principles of dynamic programming, the first criterion is the optimal substructure , which embodies the basic idea of dynamic planning, that is, by looking for the optimal substructure of the problem to achieve the problem of small, trivial, what is called the big deal. In general, dynamic programming is the problem of large scale, such as our first example, 10 steps a total number of ways to go. This is a larger problem, and we need to find a problem that is smaller than it is and the problem is essentially the same. Think about how you're going to get to the last step of the 10 steps. There are only two ways to go in the last step: from level 8th to 10th, or from 9th to 10th. Now, if we already know that there are f (8) and F (9) species walking from 1th steps to level 8th and 9th, then the solution f (a) =f (8) +f (9) of the original problem, where F (8) +f (9) represents the optimal substructure of the problem, the entire expression F (a) =f (8) +f (9) Represents the state transition equation of the problem.

Now, the dynamic programming modeling analysis of this problem has found two: the optimal substructure and the state transition equation, and what is left of a boundary condition. That is F (1) =1,f (2) =2 (first: two times one step; the second: one two), to this end, the whole problem of dynamic programming modeling analysis has been completed, summarized as follows:

Optimal substructure: F (n) = f (n-1) +f (n-2)

state transition equation: F (n) = f (n-1) +f (n-2)

Boundary conditions: F (1) =1,f (2) =2

Note that the above only uses the idea of dynamic programming to model and analyze the problem, and dynamic programming is usually also as a problem modeling method, so, specifically dynamic programming is the same problem analysis method, once a problem modeling completed, it can use a lot of problems to solve, the most common have recursive structure, Memo structure and dynamic planning structure , the following is explained separately for this example:

(1) Recursive structure

int getclimbingways (int n)
{
	if (n < 1) return
		0;
	if (n = = 1) return
		1;
	if (n = = 2)
	{return
		2;
	}
	Return Getclimbingways (n-1) + getclimbingways (n-2);
}
Recursive structure is based on the newly established dynamic programming model directly translated into code, although the recursive algorithm can solve this problem, but its time complexity is O (2^n), because in the recursive process there are many repeated computations, this can be used to solve the memorandum structure

(2) Memorandum structure

To use the memo structure, you need to create a memo area to store the results of the repeated computations in the recursive structure, so that we only need to know dp[1], dp[2]......dp[10], so we can use one-dimensional array dp[10] to represent the memo area

int dp[10] = {0};    Memo Area
int getclimbingways (int n)
{
	if (n < 1) return
		0;
	if (n = = 1) return
		1;
	if (n = = 2)
	{return
		2;
	}
	if (Dp[n]!= 0)    //Enquiry Memo, O (1) Time return
		Dp[n];
	else
	{
		Dp[n] = getclimbingways (n-1) + getclimbingways (n-2);
		return dp[n];
	}
It can be seen that the time complexity of the memo algorithm is O (n), but the space complexity is O (n), which has an effect on the whole program structure when N is very large, the following can solve the problem of high space complexity with dynamic programming structure.

(3) Dynamic planning structure

Whether it is a recursive structure or a dynamic programming structure, it is a Top-down solution (This is also the (forward) derivation of the state transition equation in the dynamic programming model), that is, if the dp[n] is required, then dp[n-1 must be sought first. This idea leads to a lot of repetitive computations (time complexity) in a recursive structure, in the memo structure resulting in greater space complexity, then we can change the thinking, bottom-up solution to this problem, this is possible, where the bottom-up only need (reverse) to deduce the state transition equation, That is, directly through the last one to find the next, that is, to seek dp[n-1 first], in the dp[n]

int getclimbingways (int n)
{
	if (n < 1) return
		0;
	if (n = = 1) return
		1;
	if (n = = 2)
	{return
		2;
	}
	int a = 1;
	int b = 2;
	int temp = 0;
	for (int i = 3; I <= n; i++)
	{
		temp = a + b;   (reverse) Deduce the state transition equation
		a = b;
		b = temp;
	}
	return temp;
}


2, longest matching bracket to substring problem

problem Description: given a string input, this string is composed only of parentheses, that contains only "(" and ")", you need to solve the longest valid matching bracket in this string for the length of the substring, such as input "()) () ()", then output 4, enter "())", The output is 6.

This problem seems to have nothing to do with dynamic planning. OK, so without the dynamic programming solution, what method can you use to solve it? First of all, for this problem, if you use brute force solution (that is, exhaustive all the cases, then search the longest substring length), in addition to exhaustive method, what other optimization methods? If it's not there, so for the problem that can be solved by exhaustive method, I know that can also use two methods to optimize: backtracking (later will update the relevant posts) and dynamic programming method , but for backtracking, in fact, it is also a kind of exhaustive method, but the "Reduction of the branch optimization", It doesn't seem to be a very easy problem. In this case, the first to find the law of the problem, see if you can use dynamic programming solution, if you want to use dynamic programming solution, it is first to carry out dynamic programming modeling, that is to find the problem: (1) The optimal substructure, (2) boundary, (3) state transition equation

(1) Dynamic Programming modeling

Optimal substructure: The original problem is to find the length of the longest matching bracket pair in the input string, where the problem can be quantified (converted), assuming that the length of the input string is size, the original problem can be quantified as The longest matching bracket for a character from subscript 0 to subscript size-1 (that is, the last character) is the length, which is recorded here as Dp[0], and asks Dp[0], can we ask for dp[1],dp[1 first? Represents the length of the longest matching bracket pair contained in a character from subscript 1 to the subscript size-1 character, where we have extracted the state of the problem (Dp[0], dp[1] 、...... dp[size-1)), The optimal substructure of the problem is obtained when the dp[n is already known outside the dp[n-1], and note that the state transition process here is dp[n]-->dp[n-1], where dp[n is the optimal substructure of the dp[n-1]

(2) border

Above we have extracted the state of the problem and the optimal substructure, can also obviously get the boundary of the problem: dp[size-1]=0, because a "(" or ")" character can not constitute a bracket matching

(3) state transition equation

In extracting the optimal substructure, we get the state transition process from dp[n]-->dp[n-1], but what is the specific transfer process? We begin by traversing the input string from the problem boundary, that is, from dp[size-1] to [0], to analyze the state transition process based on the case of each character (either the opening parenthesis ("or the closing Parenthesis")):

Note that the reverse traversal, and the problem boundary is known, so the loop starts at Size-2
for (int i=size-2; i>=0;-I.)
For the first character of the current traversal (here we already know the value of dp[i+1]):
If the first character is "("
Then we skip the dp[i+1] element to see what the next element is.
See what the j=i+dp[i+1]+1 element is, if J does not cross the line,
See if it is ")".
If it is ")", if J does not cross the line and s[j]= ")", so dp[i]=dp[i+1]+2
If not, Dp[i]=0 (original value)
The last step has been obtained S[I......J] between the longest bracket pair
Length, there is also a need to determine whether j+1 crosses the line
If there is no crossing, then dp[i]=dp[i]+dp[j+1]

If the first character is ")", the Next loop directly

By modeling the problem above, you can write the following code:

int longestvalidparentheses (string s) 
{
    int i,j,n;  
    N=s.size ();  
    int *DP = new Int[n];  
    int max=0;  
    for (i=0;i<n;i++)  
        dp[i]=0;  
    for (i=n-2;i>=0;i--)  
    {  
        if (s[i]== ')  
        {  
            j=i+dp[i+1]+1;    The J Element
            if (j<n && s[j]== ') ')  
            {  
                dp[i]=dp[i+1]+2;  
                if (j+1<n)  
                    dp[i]+=dp[j+1];  
            }  
        }  
        if (Max<=dp[i])  
            max=dp[i];  
    }  
    return max;  
}

Third, summary

In short, to understand and apply dynamic programming algorithm, first of all, according to the dynamic programming of the three criteria for modeling problems:(1) The optimal substructure, (2) boundary conditions, (3) State transfer. then consider using the most algorithm structure area according to the established dynamic programming model for code design and writing.



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.