77221

Time Complexity of Fibonacci Algorithm [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/360748/computational-complexity-of-fibonacci-sequence" dir="ltr" rel="nofollow">Computational complexity of Fibonacci Sequence</a> <span class="question-originals-answer-count"> 12 answers </span> </li> </ul>

So, i've got a recursive method in Java for getting the 'n'th fibonacci number - The only question i have, is: what's the time complexity? I think it's O(2^n), but i may be mistaken? (I know that iterative is way better, but it's an exercise)

public int fibonacciRecursive(int n) { if(n == 1 || n == 2) return 1; else return fibonacciRecursive(n-2) + fibonacciRecursive(n-1); }

Answer1:

Your recursive code has exponential runtime. But I don't think the base is 2, but probably the golden ratio (about 1.62). But of course O(1.62^n) is automatically O(2^n) too.

The runtime can be calculated recursively:

t(1)=1 t(2)=1 t(n)=t(n-1)+t(n-2)+1

This is very similar to the recursive definition of the fibonacci numbers themselves. The +1 in the recursive equation is probably irrelevant for large n. S I believe that it grows approximately as fast as the fibo numbers, and those grow exponentially with the golden ratio as base.

You can speed it up using memoization, i.e. caching already calculated results. Then it has O(n) runtime just like the iterative version.

<hr />

Your iterative code has a runtime of O(n)

You have a simple loop with O(n) steps and constant time for each iteration.

Answer2:

You can use this

<img alt="alt text" class="b-lazy" data-src="https://i.stack.imgur.com/cygHc.png" data-original="https://i.stack.imgur.com/cygHc.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

to calculate Fn in O(log n)

Answer3:

Each function call does exactly one addition, or returns 1. The base cases only return the value one, so the total number of additions is fib(n)-1. The total number of function calls is therefore 2*fib(n)-1, so the time complexity is Θ(fib(N)) = Θ(phi^N), which is bounded by O(2^N).

Answer4:

O(2^n)? I see only O(n) here.

I wonder why you'd continue to calculate and re-calculate these? Wouldn't caching the ones you have be a good idea, as long as the memory requirements didn't become too odious?

Since they aren't changing, I'd generate a table and do lookups if speed mattered to me.

Answer5:

It's easy to see (and to prove by induction) that the total number of calls to fibonacciRecursive is exactly equal to the final value returned. That is indeed exponential in the input number.

Recommend

  • How to enable anonymous access to part of the website in SiteMinder
  • how to get image map from Jfree chart's Image
  • SBT Plugin: How to add compiler plugin as a dependency that is not propagated downstream?
  • How to reference a parameter of a different method?
  • Automapper null string to empty
  • Communication between 2 apps on same device iOS/Android with Xamarin
  • How to find out if a lazy relation isn't loaded yet, with SQLAlchemy?
  • Conditional Redirect on Login
  • How to create a Flutter plugin that works in another Flutter project?
  • pyodbc fails without error
  • set clipboard content in any browser
  • jquery: Disable/Enable button not working after reset
  • Stop the background service after particular time in android
  • Sending in an object of type Object instead of String - Polymorphism
  • JQuery and PHP validation problem?
  • Old data appears in SQLite database when reinstalling app
  • Python: Why am I getting a UnicodeDecodeError?
  • How to create 2 svg's on one page?
  • NextJS auth with an external server
  • Multiple JSliders reacting to each other to always equal 100 percent
  • AppEngine Paypal integration giving SSLCertificateError on localhost, using Python
  • Android: Mediaplayer stop / start playing raw resource
  • Highcharts Drawing a line with chart.renderer.path
  • Windows 8 Flexboxes - Nesting Flexboxes with overflow enabled
  • android 3G signal strength
  • device tree overlay phandle
  • Hiding prices based on visitor location backend bug in Woocommerce
  • List using with references, changes behavior when used as a member
  • Windows: How do I get the mode/access rights of an already opened file?
  • VS2010: Ctrl-PgUp / -PgDown like in browsers
  • Was there ever a proposal to include the URL fragment into the HTTP request?
  • Bundling python(“.py”)files along with java class files for a web application
  • Issue with Terrain Collision using Three.js
  • JQuery Mobile Ajax Navigation in Single-Page Template
  • Request Access Token in Postman for Azure Function App protected by Azure AD B2C
  • Disable account chooser FirebaseUI React
  • 'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs