Space for time, reducing the complexity of recursion to O (2n)

Source: Internet
Author: User

the time complexity of recursive algorithms is not linear unless there are only the first two items, and memory consumption. We use the most common Fibonacci series to describe:

FunctionFibonacci (n ){If(N = 0 | n = 1){ReturnN ;}Else{ReturnFiber-ACCI (n-1) + fiber-ACCI (n-2);}}

This is the most common writing method, which is extremely memory-consuming. When the parameter n is greater than 30, it will obviously feel that it takes a long time. If n is equal to 100, the browser is very likely to crash.

Let's analyze the memory-consuming and time-consuming reasons: First, store the variable values to the stack, keep using the stack, and save the scene until the recursive end condition is met, the variable Values to be calculated are extracted from the stack, and then the field is restored one by one to obtain the final result.

The following figure shows the recursive call process:

We can see that when n is 4, a total of 12 steps are performed, of which steps 6th, 8, 9, 10, and 11 are repeated. It is precisely these duplicates that cause this recursion inefficiency.

Since we have found the cause, how can we improve it?

The reason is that the previous computation results are not saved, causing Repeated Computation. Then, we save the previous computation results with a variable, and the modifiedCodeAs follows:

 VaR Fibonacci = ( Function  (){  VaR Temp = {}, Value;  Function  F (n ){  If (N In  Temp) {Value = Temp [N];}  Else  { If (N = 0 | n = 1 ) {Value = N ;}  Else  {Value = F (n-1) + f (n-2 );} Temp [N] = Value ;}  Return  Value ;}  Return  F ;})(); 

Here we introduce a variable temp to store the previous calculation results. This approach is to change the space for time. Since recursion always needs to reach the bottom layer and then return to the top layer, the minimum time complexity is O (2n). the time complexity of the modified recursive algorithm is O (2n ).

Finally, let's test the time and number of computations for Calculating N as 100:

We can see that the time is very short, and the number of computations is only 199. If no optimized code is used, my browser will crash.

Since the method in javascript can be nested, how can we implement it in other languages? I asked a question yesterday about the C # code provided by artwl. I think other languages can do the same.

 Static   Void Main ( String  [] ARGs) {console. writeline (maid (  3 )); //  2 Console. writeline (maid ( 60 )); // 1548008755920 Console. writeline (maid ( 100 )); //  3736710778780434371  Console. Read ();}  Static Dictionary < Int , Long > Memo = New Dictionary < Int , Long > ();  Static  Long Fibonacci ( Int  N ){  Long Value = 0  ;  If  (Memo. containskey (N) Value = Memo [N];  Else  {  If (N = 0 | N = 1  ) Value = N;  Else  Value = Maid (n- 1 ) + Maid (n- 2  ); Memo [N] = Value ;}  Return  Value ;} 

I hope this article will help you. Please leave a message to discuss it.

This article first blog Park: http://jscode.cnblogs.com, reprint please indicate the source.

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.