"Algorithm"-recursive algorithm

1. Overview of recursion

  • Recursion, as the name implies, recursion is called recursion if there is recursion.

  • If an object partially contains itself, or uses itself to define itself, the object is said to be recursive;

  • If a process calls itself directly or indirectly, it is said to be a recursive process.

  • To put it bluntly, recursion is a function: in a function, the function itself is called, and we call such a function a recursive function.

  • The basic idea of ​​recursion is "call yourself"

2. Recursive process

\quad \quad The recursive process can be understood as the recursive process and the recursive process, that is, the stacking process.

Insert picture description here

Insert picture description here

  • The following figure shows the specific recursive calling process for calculating the factorial of 3
  • The red line is the pass-to-stack process
  • The blue line is the process of returning to the stack
    Insert picture description here

2.1 "Pass"

  • The process of passing is the process from calling to finding the internal termination condition of the calling method.
  • It is equivalent to the stacking process: each time it is called, it needs to record the current parameter value and the end position, that is, the suspended code position and merge it into the stack.
  • Stack:

Insert picture description here

2.2 "Return"

  • The process of returning is the process of returning to the calling method of the calling method when the innermost method is executed from the termination condition.
  • Unstack:

Insert picture description here

  • For each recursive call, additional storage space needs to be allocated for the parameters and local variables used in the process.
  • Recursively downwards layer by layer, the order when exiting is just the opposite
  • Therefore, the space to be allocated for each recursive call forms a recursive work record, organized by the advanced stack.

3. Applicable conditions of recursion

1. The problem can be solved by decomposing it into multiple sub-problems, and the subsequent situation can be deduced from the previous state.

2. The problem is the same as the sub-problem.

3. The problem has a termination condition. After finding the termination condition, the "return" process will begin.

4. Recursive program

4.1 Three elements

1. Recursive statement

  • Recursive statements are recursive formulas
  • The most important point in writing a recursive formula is to find the relationship between the problem and the sub-problem. How to find the relationship that exists between them? We only want the relationship between the last layer and the upper layer. For example, if I want to know the position of the current team, so I want the position of the previous person, and then +1 is my position. I don't care where he is, but let the recursion solve his position. We can write the recurrence formula as follows:
// f(n) 代表当前我在队伍中的位置
// f(n-1) 代表我前边那个人的位置
// 递推公式
f(n) = f(n-1) + 1 

2. The recursive exit is the recursive termination condition

  • We wrote the recursion formula very easily, but the recursive formula without a termination condition will be executed forever, so we need a termination condition to terminate the program. So how to find the termination condition?
  • The so-called termination conditions are known conditions . For example, in the above example of queuing for food, the first person is eating at the window, and there is no one in front of him, so he is the first one. The position of the first person is 1, how should we indicate it?
// 终止条件
f(1) = 1;

3. Number of recursion

  • Requires a limited number of recursions

4.2 Complete recursive program code

function f(n){
    
    
    // 终止条件
	if(n == 1) retun 1;
    // 递推公式
	return f(n-1) + 1;
}

5. Recursive characteristics

5.1 Advantages

  • Refined programming, clear logic, approximate mathematical recursion formula, good readability

5.2 Disadvantages

  • Implementation efficiency is low: when you call yourself, memory is allocated to save parameter values ​​every time. Lead to time and memory consumption. Thereby reducing efficiency.

  • Excessive use of memory may lead to the risk of memory overflow.

Guess you like

Origin blog.csdn.net/weixin_45666566/article/details/113717773