Dynamic programming algorithm [summary]

Dynamic Programming (DP: Dynamic Programming)

Dynamic programming is solving an optimization method contains duplicate sub-problems, the original problem into sub-problems are relatively simple. Dynamic programming can only be applied have optimal substructure problems (ie local optimal solution can determine the global optimal solution, or a problem can be broken down into sub-problems to solve).

The basic idea

Similar to the original problem into sub-problems, then the combined solutions to subproblems to arrive at the solution of the original problem. Dynamic Programming in the process of solving the solution of the original problem is obtained by solving saving sub-problem (as opposed to simply divide and rule), only to solve each sub-problem once, thereby reducing the amount of calculation : Solution Once a given sub-questions have been calculated, then it is memory storage , so next time you need the same sub-look-up table when the solution of the problem directly. In this approach the number of repeats questions about the size of the input was particularly useful when exponential growth.

Dynamic Programming = memory of the search

Divide and Conquer and Dynamic Programming

In common : both require the original problem has a sub-optimal structural properties, are the original problem of divide and rule, broken down into a number of smaller-scale (small program to easily resolved) sub-problems and then the combined solutions to subproblems. to form a solution of the original problem.

Is different: the partition method subproblems decomposed as independent, done by recursively.

     The dynamic programming sub-problems decomposed understood to have contact with each other, overlapping part, need to remember, usually an iterative do.

Characteristics problems

Optimal substructure: When the optimal solution contains optimal solution to the problem of his son, said the problem has structural sub-optimal.

Overlapping sub-problems: when a top-down recursive algorithm Solutions, each sub-problem are not always produced new problems, some sub-problems are of iterations. Dynamic programming algorithm is the use of the overlapping nature of this sub-problems, the solution only once for each sub-problem, and its solution will save a table, after the solution of these sub-problems using as much as possible.

Recursion is the easiest problem to solve a class action compliance issues, the state transition equation is the recurrence equation.

General idea of ​​solving dynamic regulation

1. The original problem into sub-problems

  • The original problem is divided into several sub-problems, sub-problems and often form similar to the original problem, and sometimes exactly the same, but the scale is smaller.
  • Solution of the problem once obtained will be saved, so just to solve the problem once for each child.

2. Determine the state

  • And the problems associated with the sub-set of values ​​of the variables called a state. A state corresponding to one or more sub-problem.
  • "Value" in a state of so-called, is the solution of the corresponding sub-state problem.
  • The set of all states an issue of "state space." The size of the "state space" directly related to the time complexity of dynamic programming problem solving.
  • Using dynamic programming problem solving, often the case is: K integers can constitute a state variable, ranging from the K integer variable is N1, N2 ... Nk, then we can use a K-dimensional an array array [N1] [N2] ... [Nk] to store the value of the respective states . This value is not necessarily an integer or floating-point numbers, may require a structure to represent, then the array can be an array of structures.
  • A "value" of "state" is typically a solution or more sub-problems.

3. Determine the state transition equation

  • After definition of what is "state", and "value" in the "state", it is necessary to find out how to move between different states -> i.e. how to "value" known "state" from one or more of, obtaining another "state" in "value."
  • State transition can be represented by recursion formulas, this recursion formula may also be referred to as "state transition equation."

Typical problems

01 longest increasing subsequence

 problem:

Set L = <a1, a2, ..., an> is n different sequences of real numbers, L is incremented subsequence is a subsequence Lin = <ak1, ak2, ..., akm>, where k1 <k2 <... < km and aK1 <ak2 <... <akm. Seeking the maximum value of m.

Problem-solving ideas (1): to find sub-problems

Knapsack problem

There are N items V and a capacity of the backpack. Cost is the i-th items c [i], is the value of w [i]. Solving the items into a backpack which allows the total cost of these items does not exceed the capacity of the backpack, and the sum of the maximum value. 

F [i] [v] denotes the front i items placed exactly a maximum capacity value v backpack available. It is the state transition equation: f [i] [v] = max {f [i-1] [v], f [i-1] [vc [i]] + w [i]}.

I items placed in front of this capacity in the sub-problems backpack v ", when considering only the i-th policy items (or leave), then it can be converted to a i-1 just before the involved items problem If hold the i items, then the problem is transformed into "i-1 before the capacity of items placed in the backpack v"; if you put the i items, then the problem is transformed into "items before i-1 Add the remaining capacity VC [i] backpack ", the maximum value can be obtained at this time is f [i-1] [vc [i]] plus the value of w i-th obtained by putting items [i]. 

Enumeration, search, dynamic regulation の summary

Enumeration -> Search (systematic) -> dynamic programming (the memory of)

Achieve search

Mode 1: Recursive - pruning

  • The entire search process, the final state is always the same.
  • Do not consider the obvious path to reach the final state.

Mode 2: Dynamic Programming

      purpose:

  • In the search process, the result of the calculation remains.
  • Later during a search, the results of previous efforts to use the search process, to avoid double counting.

      method:

  • The ultimate goal is decomposed into a number of relatively simple goals.
  • Relatively simple to achieve these objectives, to achieve the ultimate goal on this basis.

Which way the specific use? Case may be

  • There is no repeat of the calculation can be used: recursion, to keep it simple.
  • Repeat calculations accounted for a large proportion: dynamic programming, in order to improve efficiency.

 

Guess you like

Origin www.cnblogs.com/yun-an/p/11032975.html