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

后端 未结 9 1828
深忆病人
深忆病人 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:42

    Look at it like this. Assume the complexity of calculating F(k), the kth Fibonacci number, by recursion is at most 2^k for k <= n. This is our induction hypothesis. Then the complexity of calculating F(n + 1) by recursion is

    F(n + 1) = F(n) + F(n - 1)
    

    which has complexity 2^n + 2^(n - 1). Note that

    2^n + 2^(n - 1) = 2 * 2^n / 2 + 2^n / 2 = 3 * 2^n / 2 <= 2 * 2^n = 2^(n + 1).
    

    We have shown by induction that the claim that calculating F(k) by recursion is at most 2^k is correct.

提交回复
热议问题