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: given half a dollar, one-fourth dollars, 10 cents, 5 cents and 1 cents of coins, will be 1 dollars for change, altogether how many different ways? The more general question is, given any amount of cash, can we write a program that calculates the number of different kinds of change? This problem is a dynamic programming problem.


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 can not be arbitrarily determined, it depends on the current situation, but also affect 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 in each stage and determine the state transition equation
3 General prerequisites for using dynamic Programming 3.1 The optimization principle of dynamic programming is the best strategy of the whole process, which has the following properties: Regardless of the past state and decision-making, the remaining decisions must form the optimal strategy for the current state of the previous decision.

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 substructure , that is to say, 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 Satisfying the no-no-validity principle of dynamic programming the so-called no-validity principle refers to the nature of a phase when the state is determined, then the evolution of the process is no longer affected by previous states and decisions. 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.

Specifically, if a problem is divided into stages , the state of phase I can only be obtained by state transition equations in the state of the stage i+1, which is not related to other States, especially to the non-occurrence state, which is no effect. From the perspective of graph theory, if the state of the problem is defined as a vertex in the graph, the transfer 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 graph is formed, so the graph can be "topological sort", At least the stages can be divided in the order in which they are topologically sorted.


4 Dynamic planning and Design method 4.1 general method Generalstarting from the initial state, through the selection of intermediate stage decision, reached the end state. These decisions form a sequence of decisions, while defining an active route to complete the process. The steps are:
    1. The dividing stage: according to the question Time or the space characteristic, divides the question into several stages. In the partitioning phase, note that after the division of the stage must be ordered or sortable, otherwise the problem can not 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: Because decision-making and state transfer have a natural connection, state transfer is to export the state of this stage according to the state and decision of the previous stage. So if the decision is made, the state transfer equation can be written out. In fact, it is often done in turn, depending on the relationship between the two adjacent paragraphs to determine the decision.
    4. Looking for boundary conditions: The given state transition equation is a recursive type that requires a recursive termination 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 part will be very simple.
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. If the original problem can be broken down into several essentially the same, small-scale problems, it will naturally be associated with the reverse thinking from the perspective of the solution to seek 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. If the sub-problems are not independent, then the division of the law will do a lot of unnecessary work, repeated to solve the problem of the public.
    • The difference between dynamic programming and divide-and-conquer method is that dynamic programming allows these sub-problems to be independent (that is, each sub-problem can contain common sub-problems), it only once for each sub-problem, and saves the result, and avoids repeating the calculation every time it encounters. This is one reason why dynamic planning is efficient.
Reverse deduction steps for dynamic programming:
    1. The structure of the optimal value is analyzed and its structural characteristics are characterized.
    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 forward thinking is a method that starts from the initial state or boundary state, and uses a certain rule to reach the new state continuously until the problem target state. The forward thinking method of dynamic programming, which starts from the initial state or boundary state of the known optimal value, traverses the whole state space in a certain order, and presents the optimal value of each state's corresponding problem.
Inin forward thinking, the process of dynamic planning is regarded as the transfer from state to state without distinguishing between the original problem and the sub-problem. Constructs a state space for all States and envisages a state network in the state space, if a decision variable di makes T (I,DI) =j to two states I,j, then a forward edge is added to the state network. Given the initial state or boundary state of the optimal value, the optimal value of the state variable of the new state can be obtained by using the state transfer equation to promote the new state of the unknown optimal value along the forward edge. We can traverse the entire state space in this way to 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 needs to traverse the entire state space by stages, so the efficiency of dynamic planning depends on the size of the state space and the cost of calculating the optimal value of each state : If the size of the state space is polynomial, then the algorithm using dynamic programming is polynomial time; if the size of the state space is exponential, Then the algorithm using dynamic programming is also exponential time. Therefore, it is very important to find a good state partition for the efficiency of dynamic programming.


5 Small experiment for change problem the inverse derivation of state transfer equation by dynamic programming: The amount of money in a is changed into 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 other than the first coin Plus,
    • The amount of money for a-d is replaced by the number of different ways of all coin types, and D is the value of the first coin.
Reverse to the initial state:
    • If the money is 0, it means that the change is complete, is a change method,
    • If 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;

Python implementation

def count_change (amount, money, kinds):    if Amount = = 0:        return 1    If amount < 0 or kinds = = 0:        return 0< C6/>return Count_change (Amount, money, kinds-1) + Count_change (amount-money[kinds-1], money, kinds) if __name__ = = ' _ _main__ ': Money    = [1, 5, ten, +]    print (Count_change (+, Money, Len)) # Run Result: 292

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.