Computational complexity of Fibonacci Sequence

后端 未结 11 1644
慢半拍i
慢半拍i 2020-11-21 10:27

I understand Big-O notation, but I don\'t know how to calculate it for many functions. In particular, I\'ve been trying to figure out the computational complexity of the nai

11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-21 10:27

    It is simple to calculate by diagramming function calls. Simply add the function calls for each value of n and look at how the number grows.

    The Big O is O(Z^n) where Z is the golden ratio or about 1.62.

    Both the Leonardo numbers and the Fibonacci numbers approach this ratio as we increase n.

    Unlike other Big O questions there is no variability in the input and both the algorithm and implementation of the algorithm are clearly defined.

    There is no need for a bunch of complex math. Simply diagram out the function calls below and fit a function to the numbers.

    Or if you are familiar with the golden ratio you will recognize it as such.

    This answer is more correct than the accepted answer which claims that it will approach f(n) = 2^n. It never will. It will approach f(n) = golden_ratio^n.

    2 (2 -> 1, 0)
    
    4 (3 -> 2, 1) (2 -> 1, 0)
    
    8 (4 -> 3, 2) (3 -> 2, 1) (2 -> 1, 0)
                (2 -> 1, 0)
    
    
    14 (5 -> 4, 3) (4 -> 3, 2) (3 -> 2, 1) (2 -> 1, 0)
                (2 -> 1, 0)
    
                (3 -> 2, 1) (2 -> 1, 0)
    
    22 (6 -> 5, 4)
                (5 -> 4, 3) (4 -> 3, 2) (3 -> 2, 1) (2 -> 1, 0)
                            (2 -> 1, 0)
    
                            (3 -> 2, 1) (2 -> 1, 0)
    
                (4 -> 3, 2) (3 -> 2, 1) (2 -> 1, 0)
                            (2 -> 1, 0)
    

提交回复
热议问题