Benbanfa understand dynamic programming algorithm

Dynamic programming has been widely used in programming, time for some of the problems by dynamic programming we can significantly reduce the complexity of the program. In essence not a dynamic programming algorithm, but to solve a class of problems idea. This blog by some very simple and classic issues (such as the number of towers, 0-1 knapsack full backpack, take the stairs issue, the longest common subsequence, etc.) to help you understand the general routine of dynamic programming.

Welcome to explore and, if wrong please correct me

For reprint, please indicate the source http://www.cnblogs.com/nullzx/

The basic idea of ​​dynamic programming

If we can solve a problem when a big problem into a problem or a smaller number of the same nature, when we solve for the answers to these minor problems, the answer is very easy to solve a big problem for such a situation obviously we can recursively (or partition) way to solve the problem. If you found some minor problems in the process of solving these small problems, we need to repeat the calculation many times, then we will simply have to answer to solving the problem is too small to record on a table so that the next encounter this small problem, we only need to look-up table can directly get the result, this is the dynamic programming vernacular to explain. Dynamic Programming difficulty lies in how to define issues and sub-issues.

2. awkward routines

1) If you can be a larger problem into one or several smaller sub-problems, that is, to find recurrence relations, at this time we might first program written in the form of recursion.

2) there is the phenomenon of sub-problems to solve if repeated use recursion to solve smaller problems, then we build a table (this table may have only one line) recorded sub-problems need to repeat solved. The filling process and will be a big problem into sub-problems the way the contrary, we will begin to fill in a form from the most simple sub-problems. Now we use the following routine to solve these classic problems.

3. Use routine problem-solving

3.1 Fibonacci cut the number of columns

Problem Description: Fibonacci define f (n) = f (n-1) number of column + f (n-2), and f (1) = 1, f (2) = 1, find f (n) of value. Fibonacci number sequence is defined to convert itself into a big problem with the nature of the two sub-problems, so we can be written in the form of direct recursive definition.


	public static int recursion(int n) {
		
		if (n < 0) {
			return 0;
		}
		
		if (n == 1 || n == 2) {
			return 1;
		}
		
		return recursion(n-1) + recursion(n-2);
	}

We f (6), for example the recursive process now drawn

clip_image002

We found that when solving F (6), need to solve F (2) four times to solve F (1) three times, solving F (3) three times, F (4) twice, so that the efficiency of our algorithm is very low of. Improve the efficiency of the way is F (1), F (2), F (3) .... The results in the table, next time you want to calculate these questions when we get directly from the table just fine, this is a the simplest example of dynamic programming. Now we follow the routines, from the smallest child to ask began filling enough.


	public static int dynamic(int n) {
		
		int[] table = new int[n+1];
		
		table[1] = 1;
		table[2] = 1;
		
		/*从小到大填表*/
		for (int i = 3; i < table.length; i++) {
			table[i] = table[i-1] + table[i-2];
		}
		
		return table[n];
	}

Incidentally, this example is only an example of an entry, in fact it is not the presence of optimal substructure, and does not require an array of length n + table 1, it is necessary only to two variables (which can be understood as a dynamic optimized version of the plan), and we explain the reason for this is just to let everyone in terms of dynamic programming to understand the problem.

Guess you like

Origin www.cnblogs.com/nullzx/p/10991305.html