Recursion / dynamic programming / iteration

basic concept

Recursion : The programming skill of the program calling itself is called recursion, which is the function calling itself.
  A function directly or indirectly calls itself in its definition. It usually transforms a large and complex problem into a smaller problem similar to the original problem to solve, which can greatly reduce the code size. The ability of recursion is to define an infinite set of objects with limited sentences.

There are two points to note when using recursion:
1) Recursion is to call itself in a procedure or function
2) When using recursion, there must be a clear end condition for recursion, called recursion exit

Recursion is divided into two stages:
1) Recursion: push the solution of complex problems to the solution of simpler problems than the original problem
2) Regression: when the simplest case is obtained, return gradually, and get complex solutions in turn

Because recursion causes a series of function calls; and there may be a series of repeated calculations, the execution efficiency of the recursive algorithm is relatively low.

Iteration : Use the original value of the variable to calculate a new value of the variable. If the recursion calls itself, the iteration is that A keeps calling B.

  The prerequisite for using a recursive algorithm is that the recursive algorithm can be used if and only if there is expected convergence, otherwise, the recursive algorithm cannot be used. Recursion is actually convenient for programmers to make it difficult for the machine. Recursion can be easily converted into programs through mathematical formulas. Its advantages are easy to understand and easy to program. However, recursion is implemented using a stack mechanism. Each deep layer takes up a stack of data area. For some deep nesting algorithms, the recursion will be ineffective, and the space will end in memory collapse, and the recursion will also bring A large number of function calls come, which also has a lot of additional time overhead. So when the depth is large, its spatiotemporal properties are not good. Although the efficiency of iteration is high, the running time only increases due to the increase of the number of cycles, there is no additional overhead, and there is no increase in space, but the disadvantage is that it is not easy to understand, and it is difficult to write complex problems.

definition advantage Disadvantages
Recursive Program calling its own programming skills is called recursion 1) The big problem is reduced to a small problem, which can greatly reduce the amount of code. 2) Use a limited statement to define an infinite collection of objects. 3) The code is more concise and clear, and the readability is better. 1) Recursively calling functions, wasting space 2) Too deep recursion is easy to cause stack overflow
Iterate Use the original value of the variable to calculate a new value of the variable. The iteration is that A keeps calling B 1) The iteration efficiency is high, and the running time only increases due to the increase in the number of cycles. 2) There is no additional overhead, and there is no increase in space. 1) Not easy to understand 2) Code is not as simple as recursion 3) Difficulty when writing complex problems

The relationship between the two:
1) There must be iterations in recursion, but there is not necessarily recursion in iterations, most of them can be converted to each other
2) Can use iteration without recursion, recursively call functions, waste space, and too deep recursion is easy to cause stack overflow

——————————————————————————————————————
A difference between recursion and dynamic programming (recursion is top-down , And then return to the calculation; dynamic programming is calculated from low to upward). The recursive structure is easier to understand. The addition of a record table to dynamic programming is equivalent to the practice of using space for time.

Published 162 original articles · praised 58 · 90,000 views

Guess you like

Origin blog.csdn.net/ThreeAspects/article/details/105532803