# Recursive Algorithms and Recursive Routines (Basic Algorithms)

** Contact us ** : ** Youdao technical team assistant ** : ydtech01 / ** mailbox ** : [ydtech@rd.netease.com]

I believe that students who understand algorithms often say that dynamic programming is too difficult, and they don’t know where to start when they see the problem, or they say "the problem will be solved once you look at the problem, and the problem will be discarded when you look at the problem." In essence, it is because when learning dynamic programming, the learning method is wrong, which eventually leads to a different situation and fails to grasp the essence of it. The dynamic programming and recursive algorithm have an ambiguous relationship. We choose to start with the recursive algorithm and uncover the mystery of dynamic programming step by step.

** One, ** ** Recursive formula **

Every recursive algorithm has a recursive formula, through which we can understand the recursive algorithm more clearly.

### 1.1 ** Fibonacci number sequence deed recurrence formula **

** f(n) = f(n-1) + f(n-2) ** , this is the recurrence formula of our Fibonacci sequence. Many students may ask, what is the practical use of this formula? Next, let’s look at an algorithm problem directly: ** stairs **

#### LeetCode 70. Climbing stairs

How do we understand this question? If we want to climb to the nth staircase, the previous step may be at n-1, or it may be at n-2

Therefore, the solution to this problem is clear at a glance:

```
function climbStairs(n: number): number {
const res: number[] = [];
// 爬到第0层的方法就是一动不动，我们可以认为他只有一种
res[0] = 1;
// 爬上第一层阶梯的可能性只有1个，就是走一步
res[1] = 1;
// 因此，后面的爬楼方式，我们就可以通过地推方式计算出来
for(let i=2;i<=n;i++) {
res[i] = res[i-1] + res[i-2];
}
return res[n];
};
```

** Two, ** ** Mathematical induction **

The above took everyone to learn about the practical application of the Fibonacci sequence recurrence formula. So, why can the above formula describe the characteristics of this type of problem? This is going to talk about ** mathematical induction ** .

** Mathematical induction ** is very important in the entire computer science, mainly divided into the following steps:

**verifies that k0 is established (boundary conditions);****prove that if k(i) is true, then k(i+1) is also true;****combines step 1 and step 2 to prove that k0~k(n) holds.**

I don’t know if you still remember that when we were learning binary trees, we had extended the ** recursive program design ** , and the ** recursive program design ** main point is ** mathematical induction ** in the generalized application, also known as ** Law ** .

So, let's take a look at the above stairs climbing problem, how to use ** mathematical induction method ** analysis.

**verifies that k0 holds**: In the stair climbing problem, our boundary conditions are when n is 0 and n is 1.**proves that if k(i) is true, then k(i+1) is also true**: We assume that res[i-1] and res[i-2] are correct, then res[i] is also true.**combines step 1 and step 2, and proves that k0~k(n) is established.**: Since step 1 and step 2 are combined, res[n] must be established.

** III. ** ** How to solve recursive problems (recursive problem solving routines) **

On the importance of solving routines: solving routines is the learning method of recursive algorithms. If the learning method is wrong, it is likely to go the wrong way and spend more time than others, but master less. Just like during fitness, if you have mastered some movement essentials, you may have muscles in 1~2 months, but if you don’t master the movement essentials and practice wrongly, not only will the muscles grow into fat, but they may also be injured. Own.

**Determine the recurrence state (emphasis)**A kind of function symbol

**f(x)**and give the function symbol a meaning- For example, in the Fibonacci number solution above, we have to give f(x) a meaning: the total number of ways to climb the x-th staircase

The value corresponding to the function is the answer we need to solve

- For example, in f(x) = y,
**x**is the independent variable, and**y**is the dependent variable. In the above problem of climbing stairs, the independent variable**x**is the number of stairs to be climbed, and the dependent variable y is the total number of ways to climb the stairs of x steps. Therefore, when we solve the problem, the final thing is to determine which is the independent variable and which is the dependent variable. Usually,**value of the dependent variable**is what we demand value solution. - So, how do we analyze the independent variable in the question? We need to determine what factors will affect the dependent variable. Issues such as climbing stairs, only a few stairs to climb impact on our current total number of methods, therefore,
**argument is**number of stairs x.

- For example, in f(x) = y,
**Thinking exercise**

- First, let's analyze what the recurrence state is.

First of all, the first independent variable that will affect the total number of our methods is n, that is, the room is divided into several blocks. Second, since the room is circular, in order to prevent the first and last color items, we also need to record the first and last colors. In our recurrence state, then, we get the following formula:

f(n, i, j) = y, where n represents a room is divided into several blocks, i and j represent the color of the head and tail, and y represents the total number of methods. This formula means: ** A room with a total of n blocks, the first block is painted with the i-th color, the last block is painted with the j-th color, and the total number of methods with different adjacent colors is y **

Recurrence formula

The above analysis yielded

**f(n, i, j) = y**. Now, we need to determine how to calculate f(n, i, j)

Note that the above three recursive formulas are all correct, but we are constantly optimizing our recursive formulas to improve the efficiency of the program, but the correct answers can be solved in all three ways.

**Determine the recurrence formula**- Determine which f(i) values f(n) depends on

**Analyze boundary conditions (k0)****program implementation****recursive****loop**

** IV, ** ** Recursion and dynamic programming **

** dynamic programming problem is actually to solve the optimization recursive problem. Compared with the ordinary recursive problem, the dynamic programming problem has an extra decision-making process **

The concept of empty talk is a bit abstract, let's analyze it in combination with specific issues. The problem of climbing stairs is still there, but it costs one more physical effort than the previous stairs.

### stairs with minimal cost 16166b53e00a9f

This question is similar to the simple stair-climbing problem above. The difference is that we need to spend a certain amount of stamina every time we go up the stairs, and we are required to find the least stamina.

Through the above analysis, we can draw the following ** formula ** : DP [n-] = min (DP [2-n-] + cost [2-n-], DP [n--. 1] + cost [-n-. 1])

Translate the above formula: ** The overall effort cost to climb up the nth stairway should be equal to the minimum physical effort cost for the last step to climb from the n- **

```
function minCostClimbingStairs(cost: number[]): number {
const n = cost.length;
const dp: number[] = [];
// 由于题目给定可以从第0层或第1层开始爬，因此，我们初始化第0层和第1层的体力花费为0
dp[0] = 0;
dp[1] = 0;
// 我们从第二层的体力花费开始递推
for(let i=2;i<=n;i++) {
// 第i层的体力花费是我最后一步从i-1层爬上来的体力花费与从i-2层趴上来的体力花费的最小值
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
}
return dp[n];
};
```

** V. Conclusion **

Everyone thinks that dynamic programming is difficult to learn, mainly because we need to learn dynamic programming well, we need to start and learn from three major directions ** recursive state definition, state transition equation derivation, program realization ** Both directions are hard to learn. Today, through the relevant learning of recursive algorithm and recursive formula, as well as a preliminary understanding of the relationship between recursive algorithm and dynamic programming. These are the basis for our follow-up study of dynamic programming, of which the most important is the understanding and application of mathematical induction. "Just say not to practice fake handles", most of what I said today is theory, ** next article ** "Recursive Algorithms and Recursive Routines (Hand-Teared Algorithms)" will directly start from some recursive or dynamic programming Start with the topic, learn the solving routines of the recursion program or dynamic programming program, so that you can see that recursion and motion rules are no longer at a loss. Stay tuned!

##### 有道技术团队

**651**声望

**7.9k**粉丝

0 条评论