Dynamic Programming Summary:

 

Dynamic programming concepts:

One problem is to continue to shrink, down to sub-problems, then come to the solution of a problem child, then there are solutions to subproblems obtained parent solution of the problem, step by step, finally get an answer.

 

 

The core dynamic programming: solving recursive expression

 

Dynamic Programming difference between greedy method:

It can be said greedy method is a special case of dynamic programming, greedy get the best value every time, and then calculate the value to the next step, and a more dynamic programming backtracking, that is my current solution of the problem is the need to front the solution has a solution to solve the problem of.

For instance, in the longest common subsequence in rising, dp [i] = a [i] + max (dp [i]) [0 <j <i]

Another example knapsack problem dp [I] [j] = max (dp [I-1] [j - weight [i]] + value [i], dp [i-1] [j]), are required back to in front of a state, and not necessarily a direct sub-state.

 

Dynamic Programming type topics:

1) 0-1 knapsack problem:

There are n items, they have their own volume and value, given the existing capacity of the backpack, how to get the items into the backpack has a maximum value of the sum?

 

Core: Preferably each article may take, is not taken, the direct succession of a backpack state dp [i-1] [j]; taken, the backpacks must accommodate this article, the weight of the backpack to be j - weight [i] , to put conditions. Finally, take the highest value

递推方程:dp [ I ][ j ] = max(dp[I-1] [j – weight[i]] + value[i], dp[i-1][j])

 

2) the longest common subsequence:

Given two strings, solving the longest common subsequence of two strings (Longest Common Sequence). Such as string 1: BDCABA; string 2: ABCBDAB

 

The longest common subsequence lengths of the two strings is four, is the longest common subsequence: BCBA

 

Core: every time we have recorded as the end of the string, the longest of the results of this character a [I]. Then we look for from a [0 ~ i-1] than in a [i] small character, this string of characters at the end of also must be less than a [I], and to the end of a string and [j] results are saved to dp [j] in, so just find the biggest dp [j] on it.

Recurrence formula: dp [i] = a [i] + max (dp [i]) [0 <j <i]

 

 

3 ) dynamic programming directed acyclic graph:

There are n cellar (n≤200) on a map, each buried in the cellar of a certain number of mines. At the same time, it gives the connection path between the cellar and the predetermined path is unidirectional, and to ensure that a small number are directed cellar cellar large number, there is no cellar can be from a plurality of passes and then back to the original cellar cellar path. Someone can start from any one dig mines, then dig down the noted connection (only choose one path), dig mines was completed when there is no connection. A program designed to dig mines, so that he can be dug up mines.

 

Core: This title is the best way to store a map of all nodes · case, and meaning of the questions, this question is directed acyclic graph there. Question, we learned that when I [i] n order node is ending, the longest path to his position with [i] n nodes with links relating to the relationship, but also said that the topic node must be small Next large, so that a state before the current node has been considered inevitable, so that: dp [i] = max (dp [j]) + n [i];

Remember that the final results are not in dp [max] made, but made a dp value, so even count dp maximum.

 

 

 

In fact, many of the problems of dynamic programming, and changing, it is difficult to grasp, addition of dynamic programming is not an algorithm, but more of an idea, question or want to play more, slowly comprehend.

Guess you like

Origin www.cnblogs.com/aresjohnson/p/11789968.html