Discussion on recursive process and recursive Optimization

Source: Internet
Author: User

Recently, I have read over the section of the publication. I am deeply touched by the recursion part. I want to write down what I want to know. If I have any criticism, I hope you will not be able to give me some advice.

Recursion is implementationProgramOne of the basic models used to describe the process in the calculation process, we must be very careful before discussing the recursion problem, because recursion contains two aspects, one is the recursive calculation process, the first is a recursive process, the latter is a fact in syntax, and the former is a conceptual computing process. In fact, we may use loops in the program.

We generally like to use the Fibonacci number as an example when discussing recursion. We will describe the Fibonacci number as follows:

 

ImplementedCode:

Def fib (n ):

If (n <1 ):

Return 0

Elif (n <3 ):

Return 1

Else:

Return fib (n-1) + fib (n-2)

We can see that thisAlgorithmIt is almost a literal translation of the definition. Suppose n = 6, then the calculation process is that to calculate fib (6), we have to calculate fib (5) and FIB (4 ), and so on, such:

We can see that the process is like an inverted tree. This method is called tree recursion and linear recursion. This recursive method is very straightforward and has a good understanding of its computing process. Generally, many people write recursion in this way subconsciously. However, the disadvantage is obvious. From the computing process, we can see that after a lot of redundant computing and a large number of call stacks are consumed, this consumption increases exponentially, it is often said that the call stack is easy to consume in a very short recursive process, most of which is caused by linear recursion. The process of linear recursion can be clearly described. The process of expansion and collapse can be clearly seen:

 

In addition to this recursive method, there is another method to implement recursion. The above Fibonacci number is also used as an example. This time we will not start with the definition of Fibonacci, we can start from the process of producing a series normally. If we are 0, 1, we can simply return the result directly. The subsequent calculation process is accumulation. We need to maintain the state in the recursive process, this State requires three numbers, that is, the last two numbers and the number of iterations. Therefore, we define the method

Def fib (n, b1 = 1, b2 = 1, C = 3 ):

If n <3:

Return 1

Else:

If n = C:

Return B1 + b2

Else:

Return fib (n, b1 = B2, b2 = b1 + B2, C = C + 1)

This method maintains the state of the last calculation in every recursive process, so it is called "linear iteration process", also known as tail recursion. Since each step of computing is in the state, redundant computing is eliminated, so the efficiency of this method is significantly higher than that of the previous one. The calculation process is as follows:

FIB (6)

FIB 0, 0, 1

FIB 0, 1, 2

FIB 1, 2, 3

FIB 2, 3, 4

FIB 3, 5, 5

FIB 5, 8, 6

The two recursive methods can be converted. Any recursive process that describes the intermediate computing process through a fixed number of States can be expressed through linear iteration.

We can find that the tail recursion process is basically equivalent to the loop. We can easily replace the tail recursion process with the loop, therefore, many languages provide compilation-level optimization for tail recursion, that is, converting tail recursion into cyclic code during compilation. However, it makes sense for languages that do not provide tail recursion optimization. For example, Python's default call stack length is 1000. If linear recursion is used, it will soon consume light, but tail recursion will not, for example, the FIB function of tail recursion can be called with fib (1001) and run fast. The stack overflow occurs only when fib (1002) is used. However, when the linear recursion method is used to calculate n = 30, the speed will obviously become slower, and the speed will basically go down if it is more than 40.

here I have no intention of comparing the advantages and disadvantages of the two methods. Maybe there is a gap in linear regression, but it is very readable, almost equivalent to the direct description of the formula, therefore, you can choose based on the computing scale.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.