Algorithm Learning Notes (eight) General solution method for dynamic programming

Source: Internet
Author: User

11 Questions: Statistics on change patterns

SICP the first chapter 1.2.2 tree recursion, there is a problem: give half dollar, four cents in one dollar. 10 cents, 5 cents and 1 cents in coins. How many different ways do you have to change $1 into small money? The more general question is, given a random amount of cash, can we write a program that calculates the total number of changes in the form of change?


2 Dynamic Programming Dynamic Programming (PROGRAMMING,DP) is a method to study a class of optimization problems and solve complex problems by decomposing the original problem into relatively simple sub-problems. Dynamic programming is the multi-stage decision optimization problem, which can divide the process into several interrelated phases and make decisions at each stage so that the whole process achieves the best results.

Therefore, the selection of decision-making in each stage cannot be determined arbitrarily, it depends on the state currently facing. It also affects the future development. When the decision of each stage is determined, a decision sequence is formed, which also determines an active route of the whole process. The problem is that a multi-stage process with a chain-like structure is called a multistage decision-making process.

The famous application examples of dynamic programming are: Solving the shortest path problem, knapsack problem, project management, network flow optimization and so on. The basic model for dynamic planning is as follows:

    1. Determine the decision object for the problem
    2. Stage of decision-making process Division
    3. Determining state variables for each stage
    4. Determining cost function and objective function based on state variables
    5. Establish the transfer process of state variables at each stage. Determining the state transition equation
3 General prerequisites for using dynamic Programming 3.1 The optimization principle of dynamic programming is the optimal strategy of the whole process, such as the following properties: Regardless of the past state and decision-making, for the current state of the previous decision-making, the remaining decisions must constitute the optimal strategy.


Popular understanding is that sub-problem local optimization will lead to the overall problem of the global optimal, that is, the problem has the properties of the optimal sub-structure , that is, the optimal solution of a problem only depends on the optimal solution of its sub-problem, the non-optimal solution has no effect on the solution of the problem.


3.2 To satisfy the no-effect principle of dynamic programming the so-called no-validity principle. Refers to the nature of a certain stage of the state once determined, then the evolution of the process is no longer affected by the previous state and decision-making. That is, "the future has nothing to do with the past", the current state is a complete summary of previous history, the previous history can only through the current state to affect the future evolution of the process.

In detail, assuming that a problem is divided into various stages , the state of phase I can only be obtained by state transition equations in the phase i+1, and is not related to other states , especially to the state that is not occurring, which is no effect. Consider from the perspective of graph theory. Assuming that the state of the problem is defined as a vertex in the diagram, the transition between the two states is defined as an edge, and the weight increment in the transfer process is defined as the weight of the edge, then a weighted non-cyclic graph is formed, so the graph can be "topological sort", at least in the order in which they are sorted by topology.


4 Dynamic planning and Design method 4.1 general method Generalstarts from the initial state. Through the choice of intermediate stage decision, the end state is reached. These decisions form a sequence of decisions that, at the same time, determine an active route through which the entire process is completed. The steps are:
    1. The dividing stage: according to the question Time or the space characteristic, divides the question into several stages. During the partitioning phase. Note that the divided stage must be orderly or sortable. Otherwise, the problem cannot be solved.
    2. Determine state and state variables: the various objective situations in which the problem is developed into various stages are expressed in different states. Of course, the choice of state to meet no-no validity.

    3. Determine the decision and write out the state transition equation: There is a natural connection between decision making and state transfer. State transfer is the process of exporting the state of this phase based on the state and decision of the previous phase.

      So the hypothesis determines the decision, and the state transfer equation can be written out. But in fact it is often done in turn. The decision is determined based on the relationship between the states of the adjacent two segments.

    4. Finding the boundary condition: The state transition equation given is a recursive one, which requires a recursive terminating condition or boundary condition.
    5. Programming Realization: The main difficulty of dynamic programming lies in the theoretical design. Once the design is complete, the implementation section will be easy.
4.2 Inverse derivationThe inverse thinking method refers to the thinking method of backward pushing back to the initial state or boundary state from the problem target state. Assuming that the original problem can be decomposed into several essentially the same, small scale problems, it is very natural to think from the perspective of reverse thinking to find a solution to the problem. The biggest difference between the dynamic programming and the divide-and-conquer method lies in the different properties of each sub-problem:
    • Divide and conquer law requires each sub-problem is independent (that is, does not include the common sub-problem), so once the solution of the sub-problems can be obtained recursively, the solution of the sub-problem will be merged into the solution of the original problem from bottom to back. Assuming that each sub-problem is not independent, then divide and conquer the law will do a lot of unnecessary work, repeatedly solve the problem of the public.
    • The difference between dynamic planning and divide-and-conquer method is that dynamic planning agrees that these sub-problems are not independent (that is, each sub-problem can include a common sub-problem), it only once for each sub-problem, and save the result, so as to avoid repeated calculation each time it encounters. This is one reason why dynamic planning is efficient.
Reverse deduction steps for dynamic programming:
    1. Analyze the structure of the optimal value. describe its structural characteristics;
    2. To define the optimal value recursively;
    3. Calculate the optimal value in the way of bottom-up or top-down memory;
4.3 forward inference The forward thinking method refers to starting from the initial state or the boundary state. The method of using a rule to reach a new state continuously until the problem is in the desired state.

The forward thinking method of dynamic programming. It is from the initial state or boundary state of the known optimal value, to traverse the whole state space according to certain order, and to pass the optimal value of the corresponding problem of each state.


In the forward thinking method. No longer distinguish between the original problem and the sub-problem, the process of dynamic planning is regarded as transition from state to state. A state space is constructed for all States and a state network is conceived in the state space . If the i,j of two states is present, a decision variable di makes T (i,di) =j. A forward edge is added to the state network.

Given the initial state or boundary state of a known optimal value, it is possible to generalize to a new state of unknown optimal value along a forward edge. The optimal value of state variable of new state is obtained by state transfer equation.

We are able to traverse the entire state space in such a way that we can get the optimal value of the state variable for each state.
Forward deduction steps for dynamic planning:

    1. Construction State Network;
    2. A recursive formula for optimal values based on state transition relationship and state transition equation:
    3. Calculate the optimal value of each state according to the order of the stage;

Dynamic planning requires traversing the entire state space by stages. Therefore, the efficiency of the dynamic planning depends on the size of the state space and the cost of calculating the optimal value of each state : Assuming that the size of the state space is polynomial, then the algorithm using dynamic programming is polynomial time, and the algorithm using dynamic programming is exponential time, assuming the size of the state space is exponential. Therefore, it is very important to find a good state partition for the efficiency of dynamic programming.


5 Small experiment for change problem solving inverse State transfer equation: The amount of money in a is replaced by n coins in different ways equal to:
    • The amount of money for a is replaced by the number of different ways of n-1 coins (which must not include the first coin) other than the first coin, plus,
    • The amount of money for a-d (which must include the first coin) is replaced by the number of different ways of all coins. D is the currency of the first coin
Reverse to the initial state:
    • Assuming that the money is 0, the exact change is done, is a change method,
    • Assuming that the money is negative, or the type has been handed down to 0 kinds, then it is not just a change, not a method, return 0;

It is also possible to deduce forward. The table records the values that have been calculated.

Python implementation

num = 0def count_change (amount, money, kinds): ' Tree recursion exists redundancy ' ' Global NUM if Amou NT = = 0:num+=1 return 1 if amount < 0 or kinds = = 0:num+=1 return 0 num+=1 retur n Count_change (amount, money, kinds-1) + Count_change (amount-money[kinds-1], money, kinds) def count_dy (Amount,money, kinds): "Dynamic planning, playing table records already computed values '" table = [[0 for Col in range (kinds)] for row in range (amount+1)] table[0] = [1]*kin DS for I in Range (1,amount+1): for j in range (kinds): # includes money[j] x = table[i-money[j] ][J] If I-MONEY[J] >= 0 Else 0 # does not include money[j] y = table[i][j-1] If j>=1 else 0 tabl E[I][J] = x+y return table[amount][kinds-1]if __name__ = = ' __main__ ': Money = [1, 5, ten, +] print (Count_chan GE (+, Money, Len), ' Time: ', NUM) print (Count_dy (+, Money, Len), ' Time: ', 100*len (money)) ' 292 Time: 15499292 time:500 ' 

Scheme implementation in SICP (Racket)

"Address: http://blog.csdn.net/thisinnocence/article/details/41073275"


Algorithm Learning Notes (eight) General solution method for dynamic programming

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.