How to understand the dynamic programming

Author: bubbling
link: https: //www.zhihu.com/question/39948290/answer/83942329
Source: know almost
copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

If the landlord is not a contest to brush the question, you can put aside what state transition equation on the books first, and you can teach people a branch point of thinking O (∩_∩) O:
we are faced with the optimal solution or a statistical problems and the like, based on the question "we want to simulate complete a big task," this large task can be divided into several steps, each step there are several decisions, after each step is completed, the state reached a stage
such as, you from a to Z, there is no direct, so the first step required to an intermediate location, such as H or I, and then the second step forward, such as the P or Q, and finally to Z, every step of a number of decisions, such as the first step you can decide to a, generally is one such model in the H or I, can draw their own maps look
like, you probably see the problem, if the first step to H and I can be the first two-step to the P and Q can be, that every step I only choose the best, do not get greedy with the results yet, yes, if the state of each stage you need to go through with the decision has nothing to do, it would be greedy to get a good result understanding greedy :)


However, the reality may be that your choice will affect the first step behind the branch, such as the first step you can choose to H or I, but to H after, you can only choose to go through the P or Q to Z, and if to I, you can only select R or S to Z, this way, even if you are the first step to H or I to choose a better road, not the end result is the best, because you chose such as H, that Wan IRZ a way to start a short path to Z of more than H, the optimal path may be AIRZ, so you have to try to make all these road again, before we know what the best, understand exhaustive yet? :)
OK, we slightly changed under the title set, if not from I to R and S, but the Q or R, how
Indeed, we can use each exhaustive way to solve this problem, the number of paths and exhaustive as the above figure, however, we can have a faster way, you will not be calculated separately AHQZ and AIQZ two roads, because they have state of the intersection, with the first figure of thought can be keenly felt, we only need to calculate each state have a common position seeking the best of each stage, the final stage of each selected optimal combination of greed combined on the line, because various stages of completion point is that we have a state of thing, so, we first calculate AHQ and AIQ, choose the best one, and then Q to Z with the best put together, is the best, no need to put all paths do it again (although direct view of the inside Q to Z, but you can use their imagination to think of it as a complex of various branches of the road), so that put a x ^ (a + b + c + ... ) is calculated to reduce the number of x ^ a + x ^ b + x ^ c ..., where x represents the number of times each decision (assuming simple point decision same times each), abc representative of the number of steps of each phase
thus we can be BFS from A to Z start, and BFS Each point save the optimal state, if there are different paths BFS to the same point, leaving the best one on the line, such as the above that, your algorithm may start with AHQ search to Q in this position, after the AIQ went to here, best to stay a final round three points from PQR to Z, over, relatively less time drawing the second chapter operation
if you understand, I congratulate you been able to effectively solve many problems need of a DP, but also learn to understand graph theory of a single source shortest path problem then

 

Finally, to be a classic example of a 0-1 knapsack problem, consolidate it, the task is that we can withstand the maximum weight W of the bag inside from the N number of selected items stuffed to the maximum value, and therefore can be divided into tasks N steps, each step face the i-th article, there are two decision-making: the election, or give up, state here, that is, after the step of factors influence decisions, where is the "free space backpack"
for example, items weight of 1,2,3,4,5,6, W = 12, scratch decision, 0 means cancel 1 represents a group selected, there are eight states after three BFS:
000 left 12 is
001 remaining 9
...... (omitted)
110 remaining 9
...... (slightly)
after the first three steps, 001 and 110 to reach the same state, can hold things all the remaining 9 weight, so that the rest of the decision, and maybe take the branch road is the same, with before you choose a 001 or 110 is no relationship (no after-effect), so just look at the big value 001, or 110 can be a big value and reduce the eight states to seven, continue BFS down, every round merge the same state, after completion of all the states from the last round Find the greatest value on ok
because the state has a W up to + 1, a N rounds, so complexity is O (NW), way the book said state transition equation of fact, with this process is very similar, but for some problems, compared to BFS + state consolidation, analysis equation of state can do better optimization, such as the introduction monotonous queue or anything, but BFS + state consolidation allows you to get a no pursuit of the limit, but also relatively fast solution, and sometimes in conjunction with specific issues more suitable, for example, according to the actual needs of the problem, the search can be pruned to reduce the workload and bound, I used at work, replacing the DP algorithm colleagues from wiki copied, can improve the efficiency of some

============ I supplement dividing line ====================

As the afternoon to go to work, written in a rush, some students have pointed out that my idea is actually to convert the problem and seek to chart a path, is indeed the case, although this practice often than textbooks state transition equation slower approach, but a move can cover a lot of problems, such as the following three:
problem 81 - Project Euler
problem 82 - Project Euler
problem 83 - Project Euler
these three questions are both matrices, the required value through the node and the smallest is only 81 right and down, that is, a DP, 82 can be vertically right columns DP can, 83 can be vertically and horizontally, irrelevant to the DP, but with a single source shortest path may pass to kill
because I learn algorithm from start it is lazy, always seek the least of tricks to solve as many problems in the complexity reached are not likely to try to pursue faster, so badly abused ACM, brush caution question please, please OJ great God who pat: )

Possible on "state is determined by how" The problem described above is relatively simple, we would add, in fact, probably under the influence of a step decision-making factors, are the current state, such as above 01 backpack, each decision is selected or not vote, but if the remaining W is no longer enough, left with "no election" a decision available, so do the state with the remaining W
another example of this problem:
problem 191 - Project Euler
after the abstract actually saying: consisting of L , O, a is a string consisting of 30 can not occur 3 or more consecutive a, a most frequent L, the number of such strings have?
Obviously a 30-step decision, every decision from the three letters which time the election legitimate, O any case legally, L lawful in case of no-show off, A is not legal in an existing string AA last time, so state is the presence of a combination of the distribution over the last two letters L and a, there are two values L occurs, distribution a has **, a *, * a, AA four kinds of (L * represents O or , do not launched), so that 2 * 4 = 8 states it

Finally, leave a small problem, is done before the examiner when a face questions often, the impression that there are six or seven students fundamental algorithms can speak now "with DP", however, asked the state transition dizzy ^ _ ^:
in agreement under the rules, entered as an array of digital single-handedly Landlords cards, design algorithm calculates the hand play out at least a few can
be noted here just to be with Landlords example, does not mean that the number of cards is limited to 20, can be seen as N is a problem according to the rules of digital packets, said Landlords are doing before because the game industry, but also to say when the interview is relatively easy to reduce the students of tension, but also a hint: we all know by greedy landlords It is not the optimal order of the cards, huh. . .

Guess you like

Origin www.cnblogs.com/wanghj-dz/p/11854657.html