![]() Unlike other Big O questions there is no variability in the input and both the algorithm and implementation of the algorithm are clearly defined. The Big O is O(Z^n) where Z is the golden ratio or about 1.62.īoth the Leonardo numbers and the Fibonacci numbers approach this ratio as we increase n. Simply add the function calls for each value of n and look at how the number grows. It is simple to calculate by diagramming function calls. Hence total work done will sum of work done at each level, hence it will be 2^0+2^1+2^2+2^3.+2^(n-1) since i=n-1.īy geometric series this sum is 2^n, Hence total time complexity here is O(2^n) Since i=n-1 is height of the tree work done at each level will be i work Now lets see how much work is done for each of n layers in tree.Note that each step takes O(1) time as stated in recurrence relation. Lets say at particular value of i, the tree ends, that case would be when n-i=1, hence i=n-1, meaning that the height of the tree is n-1. Note that each step takes O(1) meaning constant time,since it does only one comparison to check value of n in if block.Recursion tree would look like n ![]() ![]() Recursive algorithm's time complexity can be better estimated by drawing recursion tree, In this case the recurrence relation for drawing recursion tree would be T(n)=T(n-1)+T(n-2)+O(1) Now, in terms of complexity: O( F(6) ) = O(2^6) Which means, total times F() gets called when n=6 is 2x32=64=2^6. If we mentally move all the *'s from F(6) to F(2) lines into F(1) line, we see that F(1) and F(0) lines are now equal in length. Now, we want to know how many times F(x) gets called at all, and we can see the number of times F(0) is called is only a part of that. We see F(0) gets called 32 times, which is 2^5, which for this sample case is 2^(n-1). Now, the question is, how fast is the base of this pyramid enlarging as n grows? So, when F() is called for a number n, the number of times F() is called for a given number between 0 and n-1 grows as we approach 0.Īs a first impression, it seems to me that if we put it in a visual way, drawing a unit per time F() is called for a given number, wet get a sort of pyramid shape (that is, if we center units horizontally). If it gets called once per number in the sequence 0 to n, then we have O(n), if it gets called n times for each number, then we get O(n*n), or O(n^2), and so on. I came to the same conclusion by a rather simplistic but I believe still valid reasoning.įirst, it's all about figuring out how many times recursive fibonacci function ( F() from now on ) gets called when calculating the Nth fibonacci number. I agree with pgaur and rickerbh, recursive-fibonacci's complexity is O(2^n). You can find out this tight bound by using generating functions as I'd mentioned above. Consequently, the tight bound for this function is the Fibonacci sequence itself (~ θ(1.6 n )). Since each leaf will take O(1) to compute, T(n) is equal to Fib(n) x O(1). The value of Fib(n) is sum of all values returned by the leaves in the recursion tree which is equal to the count of leaves. ![]() The leaves of the recursion tree will always return 1. An interesting fact about this function is that the T(n) is asymptotically the same as the value of Fib(n) since both are defined as However, as noted in a comment, this is not the tight bound. You can then prove your conjecture by induction. You solve this recurrence relation (using generating functions, for instance) and you'll end up with the answer.Īlternatively, you can draw the recursion tree, which will have depth n and intuitively figure out that this function is asymptotically O(2 n ). ![]() This is assuming that repeated evaluations of the same Fib(n) take the same time - i.e. You model the time function to calculate Fib(n) as sum of time to calculate Fib(n-1) plus the time to calculate Fib(n-2) plus the time to add them together ( O(1)). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |