recursion and dynamic programming

Violent recursion:
1. Transform the problem into a subproblem of the same kind of problem reduced in size
2. There is a clear base case that does not need to continue recursion
3. There is a decision-making process when the result of the subproblem is obtained 4. Do not record the solution of each
sub -problem
Dynamic programming1
, come from violent recursion2,
record the solution of each sub-problem, avoid repeated calculation3
, abstract the process of violent recursion into state expression4
, and exist Simplified state representation, making it possible to make it more concise

Recursion can be changed to dynamic programming

There are many repeated states in the recursion, and the state of the repeated state is irrelevant (no aftereffect problem: after the variable parameters are determined in the recursive function, the return value is determined)

 

But the Tower of Hanoi, Queen n problem is an aftereffect problem

 

Use recursion to change dynamic programming routines:

1. Determine the size of the dp table (determine the dimension of the dp table according to the variable parameters of the recursive function , one-dimensional, two-dimensional, three-dimensional...)

2. In the dp[][] table, first identify the required position

3. Go back to the recursive function, in the base case , fill in the positions that are not dependent on

4. Then analyze the dependencies of common locations

  Look at the dependent position such as i, j which states need to depend on, such as the relationship between i + 1, j and i, j+1,

Then through this dependency relationship, use the undependent state in step 3, push it back , and get the position identified in step 2

 

Guess you like

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