The essence and application of dynamic programming programming model

Divide and conquer, greedy, linear programming, dynamic programming, and network flow are five commonly used algorithms in computer science.

Most of the interview questions like to test dynamic programming, because it is not directly applied like linear programming and network flow algorithms.

Now try to analyze this kind of problem with a more general model, and I hope that I can directly apply this kind of problem when I encounter this kind of problem in the future.

1. The essence of dynamic programming (DP)

Dynamic programming is between the greedy and the NPC problem: it is neither the optimal solution at every step like the greedy model, nor the NPC problem where every step cannot be determined and can only be exhausted

A problem that can be solved by dynamic programming has the characteristic that a sub-problem of scale i can be solved on the basis that the sub-problem of scale i - 1 has been solved.

Every time I use dynamic programming, I am troubled by this question: Will the new elements added later cause this optimal solution to be invalid? If you follow this thinking trap, the problem size will eventually become exponentially increasing and become an NPC problem

This is the feature mentioned above. As long as we can find the optimal solution for the existing stage, and prove that the solution is valid, this DP equation is completely established. As for whether the later problem will interfere with this solution is a problem that needs to be considered later.

Then a question will arise at this time: Is the local optimal solution necessarily the global optimal solution? Of course not, the optimal solution we solve under the scale i can only be calculated as a local optimal solution, its meaning is: if the problem ends here, we have solved all possible optimal solutions

2. Why can the DP problem be iterated using the previous optimal solution?

Take an integer linear programming problem: the input is X=01011.., the output is F(X), and the maximum value of F is found. My F() is a black box. You can't judge what effect a certain bit of X has on the final result. There is no way to iterate on this problem, but it can only be traversed exponentially.

The DP problem is different. We do not need to know all the scales of the problem. We only need to know the solution in the previous scale. After introducing i, we can use the previous state for iterative calculation.

So, this is determined by the characteristics of the problem itself, and the problem can calculate a partial scale solution

3. DP unified reference model

Try to normalize the DP problem, extract some features to find a unified solution

1) First assume that our problem can be solved with DP. (In fact, the conditions discussed above are met, and divide and conquer and greed are difficult to solve, so it can be determined that it is DP)

2) Divide the problem. When dividing, we must meet the following characteristics: we need to complete i under the condition of completing i - 1, and this solution cannot destroy all the optimal solutions that have been solved before, do not consider the impact on the future, and then we have our own methods ( Generally, the scale is 1 and then added to N)

3) Decision-making process. This part requires us to write the state transition equation of DP, which is also the most flexible and brain-burning part, which can be done in several steps.

Set the current processing unit to cur

a. First check: the impact on the global problem after adding 1 unit scale, that is, whether our global optimal solution OPT is independent of this calculation

Not independent: OPT = DP[ i ]

独立:OPT = max {DP[ 0],DP[ 1 ]...DP[ i ]}

This means that whether the optimal solution we are seeking includes the processing of cur, cur itself is a part of OPT, for example, to process each character of a string of strings, to calculate OPT, each cur must be calculated In columns, such problems are not independent

It is also easy to judge if it is not independent: a certain DP may contribute to OPT, or it may not have anything to do with it. For example, for all selection problems, selecting several units, OPT is only related to some units. question is independent

b. The real difficulty starts here, we add a new unit cur, and we need to find out all the previous state sets associated with it

What do you mean, all our problems are constrained. As mentioned earlier, we have found all optimal solutions that satisfy the constraints of the problem before calculating cur, so adding cur will obviously break this balance

To make it re-satisfy the constraints, we must add additional transition costs, which are ultimately calculated in DP[i], and which state set to transition from is what we need to consider

That is to say, after adding cur, with which previous states break the constraints of the problem, we need to find out all previous states

One more thing to say here, 2) I said that you don’t need to consider the impact of the current on the future, because you will consider the impact of the previous states on yourself later.

This is relatively abstract, I will give some examples: the robbery problem, two consecutive rooms cannot be selected, then the cur state is associated with the cur - 1 state; the string palindrome is divided, and the cur is associated with all previous possible states;

Two-dimensional DP[ i ][ j] is associated with DP[ i - 1 ][ j ] and DP[ i ][ j - 1 ]; some units are picked to satisfy the condition, and cur units are associated with all previous states

c. After finding the set of associated units {pred(i)} , the addition of cur will conflict with these units, and it needs to be converted under the action of the external force F, and DP[ i ] satisfies the following conditions:

given a pred(i)

DP[ i ] = F (DP[pred(i)])

This formula means that cur uses pred(i) to calculate DP[ i ] under the action of F, and divides the specific points of F into two types:

DP[ i ] = DP[pred(i)] + trans

DP[ i ] = C

That is to say, cur can add a trans complexity processing on the basis of pred(i) to satisfy the constraints

Or simply a constant C indicating that no conversion from pred(i) is possible, cur and pred(i) do not coexist, and DP[i] is only contributed by cur

d. Perform the above operations on all pred(i) to find an optimal DP[ i ], this DP[ i ] must be the optimal solution of cur at present, and then set cur = cur + 1, and iteratively calculate

e. Go back to a, choose the OPT model according to the problem type based on all the DPs we solved

4. Summary

To summarize the general processing model above:

1) Division of the problem (i - 1 scale, i scale)

2) Decision-making, examining the characteristics of the problem and selecting the OPT equation

find the set {pred(i)} associated with the current state cur

Solve DP[ i ] = pred(i) -> cur for all pred(i) to find the optimal DP[ i ]  

 

This framework is an abstraction of all the DP problems I have encountered before. It should be able to cope with most of the problems. It will be supplemented with new content in the future.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325048220&siteId=291194637