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

后端 未结 9 1854
深忆病人
深忆病人 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 06:05

    I cannot resist the temptation of connecting a linear time iterative algorithm for Fib to the exponential time recursive one: if one reads Jon Bentley's wonderful little book on "Writing Efficient Algorithms" I believe it is a simple case of "caching": whenever Fib(k) is calculated, store it in array FibCached[k]. Whenever Fib(j) is called, first check if it is cached in FibCached[j]; if yes, return the value; if not use recursion. (Look at the tree of calls now ...)

提交回复
热议问题