Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 22, 2021 12:31 pm GMT

Making recursions faster, 7 million times...

The title of the post is not misguiding at all. Take a look at the time-taken numbers for the two recursion codes, and the huge difference in them -

Time taken by conventional recursion = 720000000s (12 mins)Time taken by improved recursion = 92sFaster by = 720000000/92
Enter fullscreen mode Exit fullscreen mode

The 'improved' recursion code is faster by well over 7 million times. Read on to understand how it is done.

Most of the programmers are familiar with recursive code, and almost all of us have solved recursive problems like Fibonacci series or Factorial during our CS courses. We know that if try higher numbers, the code crashes with StackOverflowError.

Dynamic programming or DP as it is popularly known is a blessing for many problems requiring either recursive or iterative algorithms. In short, dynamic programming is an approach of solving complex problems by breaking them into several smaller sub-problems, where the sub-problems are overlapping sub-problems. It can make lot of recursive problems much more efficient. Dynamic programming approach is similar to recursive programming, however in dynamic programming the intermediate results are cached/stored for future calls.

If we consider recursive programing for Fibonacci series, computing the nth number depends on the previous n-1 numbers and each call results in two recursive calls. Thus, the time complexity is

T(n) = T(n-1) + T(n-2) = O(2)

In other words, as we increase the Fibonacci number, the time taken to compute that Fibonacci number increases exponentially. On the other hand, if we use Dynamic programming for the Fibonacci number, since the earlier results are already cached, we simply have complexity as

Time Complexity = O(n)
Extra Space = O(n)

In fact, it is fairly easy to reduce additional space for Fibonacci number by just storing previous two results instead of storing all the previous results.

It means that Dynamic programming can drastically reduce the time taken to compute solutions that require several recursive/iterative calls. I've written this small piece of Python code that computes Fibonacci number using recursion as well as Dynamic programming. See the execution results posted below the code to know respective time taken while computing the 45th Fibonacci number. You can try this code with different numbers on your own computer as well, and see how you can make your recursions million times faster.

from timeit import default_timer as timerfrom datetime import timedeltafibo_cache = [0]*200def recursive_fibonacci(num):    if num in (0, 1):        return num    return recursive_fibonacci(num-1) + recursive_fibonacci(num-2)def dynamic_fibonacci(num):    if num in (0, 1):        return num    elif 0 != fibo_cache[num]:        return fibo_cache[num]    fibo_cache[num] = dynamic_fibonacci(num-1) + dynamic_fibonacci(num-2)    return fibo_cache[num]if __name__ == '__main__':    fibo_num = 45  # change as needed    start = timer()    print(f"Dynamic fibonacci answer = {dynamic_fibonacci(fibo_num)}")    dyna_end = timer()    print(f"Time for Dynamic = {timedelta(seconds=dyna_end-start)}")    print(f"Recursive fibonacci answer = {recursive_fibonacci(fibo_num)}")    rec_end = timer()    print(f"Time for Recursive = {timedelta(seconds=rec_end-start)}")
Enter fullscreen mode Exit fullscreen mode

Recursion using dynamic programmig

This code is hosted on GitHub here - fibo.py

Cover image gratitude: http://xkcdsw.com/1105


Original Link: https://dev.to/reclusivecoder/making-recursions-faster-7-million-times-3p42

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To