Computational complexity of Fibonacci Sequence

后端 未结 11 1469
慢半拍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:52

    The proof answers are good, but I always have to do a few iterations by hand to really convince myself. So I drew out a small calling tree on my whiteboard, and started counting the nodes. I split my counts out into total nodes, leaf nodes, and interior nodes. Here's what I got:

    IN | OUT | TOT | LEAF | INT
     1 |   1 |   1 |   1  |   0
     2 |   1 |   1 |   1  |   0
     3 |   2 |   3 |   2  |   1
     4 |   3 |   5 |   3  |   2
     5 |   5 |   9 |   5  |   4
     6 |   8 |  15 |   8  |   7
     7 |  13 |  25 |  13  |  12
     8 |  21 |  41 |  21  |  20
     9 |  34 |  67 |  34  |  33
    10 |  55 | 109 |  55  |  54
    

    What immediately leaps out is that the number of leaf nodes is fib(n). What took a few more iterations to notice is that the number of interior nodes is fib(n) - 1. Therefore the total number of nodes is 2 * fib(n) - 1.

    Since you drop the coefficients when classifying computational complexity, the final answer is θ(fib(n)).

提交回复
热议问题