Recursion: two conditions that must be met by recursion

Source: Internet
Author: User
Tags exit in
Recursion is not a simple "self-called yourself", nor a simple "interactive call ". It is a method and idea for analyzing and solving problems. To put it simply, recursive thinking is to break down the problem into smaller ones with the same solution as the original one. For example, the binary search algorithm constantly scales down the problem (half of the original problem), and the new problem has the same solution as the original problem.

Many people do not have a deep understanding of recursion. It remains to the extent that "you call yourself. This is actually just a recursive representation (strictly speaking, the representation is not comprehensive, because in addition to the recursion of "self-called yourself", there is also the recursion of interactive calls ). Recursive thinking is far more simple than that.

Recursion is not a simple "self-called yourself", nor a simple "interactive call ". It is a method and idea for analyzing and solving problems. To put it simply, recursive thinking is to break down the problem into smaller ones with the same solution as the original one. For example, the binary search algorithm constantly scales down the problem (half of the original problem), and the new problem has the same solution as the original problem.

Some problems are hard to solve or even have no solution by using traditional iterative algorithms, but recursion can easily solve them. For example, the Tower of Hanoi issue. However, recursive use also has its disadvantages because it requires multi-layer function calls, which consumes a lot of stack space and function call time.

Since the recursive idea is to break down the problem into a smaller scale and have the same solution as the original problem, can this problem be solved by recursion? The answer is No. Not all problems can be solved using recursion. So what problems can be solved using recursion? Generally, the problems solved by recursion must meet two conditions:

  • Recursive calling can be used to narrow down the problem scale, and the new problem is in the same form as the original problem.
  • There is a simple situation that enables recursion to exit in a simple situation.

If a problem does not meet the above two conditions, it cannot be solved by recursion.

For ease of understanding, take the Fibonacci series as an example: calculate the nth value of the Fibonacci series.

This is a classic problem. this problem must be mentioned when it comes to recursion. The Fibonacci series is defined as follows: f (0) = 0, f (1) = 1, for n> 1, f (n) = f (n-1) + f (n-2)

This is an obvious problem that can be solved using recursion. Let's take a look at how it meets two recursive conditions:

  1. For a n> 2, f (n) only needs to find f (n-1) and f (n-2), that is to say, the problem of scale n is transformed into a smaller problem;
  2. For n = 0 and n = 1, there is a simple situation: f (0) = 0, f (1) = 1.

Therefore, we can easily write the recursive program for calculating the nth entry of the Fibonacci series:

int fib(n){    if(n == 0)        return 0;    else if(n == 1)        return 1;    else        return f(n-1) + f(n-2);}

When writing a function called by recursion, you must write the judgment on a simple situation at the beginning to ensure that the function call can stop recursion in time when detecting a simple situation. otherwise, your function may be called recursively without stopping.

Additional reading

The topic list of this article is as follows:

  1. Recursive: recursive thinking
  2. Recursion: two conditions that must be met by recursion
  3. Recursion: recursive determination of string-to-text phenomena
  4. Recursive: recursive implementation of binary search algorithms
  5. Recursion: the efficiency of recursion
  6. Recursion: recursion and loop
  7. Let's talk about recursion: Is loop and iteration the same thing?
  8. Recursive computing and iterative computing
  9. On Recursion: Learn about tail recursion from Fibonacci
  10. Recursive: Tail recursion and CPS
  11. Recursion: add more knowledge about Continuation.
  12. Recursion: Tail recursion in PHP and its optimization
  13. Recursion: Tail recursion optimization from the perspective of Assembly

This article is available at http://www.nowamagic.net/librarys/veda/detail/2315.

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.