On the comprehension of recursion and the analysis of the complexity of recursive expression (to solve greatest common divisor as an example)

Source: Internet
Author: User
Tags benchmark greatest common divisor

One, the four basic laws of recursion:

① Benchmark Scenario

Baseline scenarios are those that do not require recursion (which does not require a function call) to exit. It guarantees the end of recursion.

② continues to advance

Each recursion is followed by a baseline situation, and the scale of the problem is getting smaller and closer in the process.

③ Design Rules

The book says: Assume that all recursive calls can run-----"not particularly understood"

④ synthesis Benefit Rule

Do not repeat the work in different recursive calls.

Two, examples

Solving greatest common divisor--using Euclidean algorithm

1  Public Static intGcd_recursive (intMintN) {2         if(M <N)3         {4             intTMP =m;5m =N;6n =tmp;7         }8         9         if(n = = 0)Ten             returnM//Benchmark Conditions One         returnGcd_recursive (n, m%n);//continue to promote A}

Analysis:

第9-10, which is a recursive reference condition. If n=0, the function executes to 10 and does not perform a recursive call to 11 rows.

Line 11th, where the recursive call is made. It is constantly advancing because the parameters of the recursive call become smaller in the direction of the datum condition, such as:

Gcd_recursive (16,12)---->gcd_recursive (12,4)--->gcd_recursive (4,0)

The size of the problem is getting smaller with each recursive call.

Time Complexity Analysis:

By formula: M%N<=M/2: Each recursive call, the size of the problem is reduced by half, similar to binary lookup, which is obviously a very good algorithm.

Due to the 2–5 line, the time spent is constant time, similarly, the IF statement in the 第9-10 line is also the constant time spent, recursive calls on line 11th, the problem size is reduced by half.

It can be concluded that T (N) = t (N/2) +o (1) Introduced: Time complexity O (LOGN)

-------------------------------------------------------------

Analysis of recursive logic:

For Gcd_recursive (16,12), line 9th is not established and enters the 11th line recursively

For Gcd_recursive (12,4), line 9th is not established and enters the 11th line recursively

For Gcd_recursive (4,0), direct execution to line 9th returns, the value returned is 4

After returning, the program executes to the 11th line in Gcd_recursive (12,4) (that is, the last line, not the 9th line interference!). Line 9th is not executed at all in Gcd_recursive (12,4)!! )

The 11th line of code is: Gcd_recursive (4,0)

Because the result of gcd_recursive (4,0) is 4, the return gcd_recursive (4,0) returns 4. That is, gcd_recursive (12,4) returns 4 after execution is complete.

Returns 4 after execution of the above gcd_recursive (12,4), then the 11th line of code return gcd_recursive (12,4) of gcd_recursive (16,12)

When execution is complete,

The entire program is finished, and the result of the return is finally 4.

This form of recursion is also called tail recursion. As you can see, the final return value of the tail recursive program is the value of the innermost recursive call.

In this article: character arrays are converted into numbers

The temporal complexity of recursion is analyzed as follows:

return Recurse (c, len-1) * + (c[len-1]-' 0 ');

The size of the problem is reduced by 1 per recursion, and the subsequent + (c[len-1]-' 0 ') operation can be considered a constant time, so complexity:

T (n) = t (N-1) +o (1) Get T (n) =o (n)

Conclusion:

For recursive operations, if each recursion causes the size of the problem to be halved, and the other operations are constant time

T (n) =t (N/2) +o (1), T (n) =o (LOGN)

If the size of each recursive usage problem is reduced by 1, and the other operation is constant time

T (n) =t (N-1) +o (1), T (n) =o (n)

If each recursion causes the size of the problem to be halved, while the other operation is linear time, t (n) = t (N/2) +o (n)

Then T (N) =o (NLOGN)

On the comprehension of recursion and the analysis of the complexity of recursive expression (to solve greatest common divisor as an example)

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.