Why is the complexity of computing the Fibonacci series 2^n and not n^2?

后端 未结 9 1839
深忆病人
深忆病人 2020-11-29 05:31

I am trying to find complexity of Fibonacci series using a recursion tree and concluded height of tree = O(n) worst case, cost of each level = cn,

9条回答
  •  旧巷少年郎
    2020-11-29 05:58

    The complexity of recursive Fibonacci series is 2^n:
    This will be the Recurrence Relations for recursive Fibonacci

     T(n)=T(n-1)+T(n-2)                  No  of elements  2
    

    Now on solving this relation using substitution method (substituting value of T(n-1) and T(n-2))

    T(n)=T(n-2)+2*T(n-3)+T(n-4)          No  of elements  4=2^2
    

    Again substituting values of above term we will get

    T(n)=T(n-3)+3*T(n-4)+3*T(n-5)+T(n-6)  No  of elements  8=2^3
    

    After solving it completely, we get

    T(n)={T(n-k)+---------+---------}----------------------------->2^k  eq(3) 
    

    This implies that maximum no of recursive calls at any level will be at most 2^n.
    And for all the recursive calls in equation 3 is ϴ(1) so time complexity will be 2^n* ϴ(1)=2^n

提交回复
热议问题