0

I'm reading through SICP. It's my first introduction to computer science.

The book presents the Fibonacci algorithm below:

(define (fib n)
  (cond 
    ((= n 0) 0)
    ((= n 1) 1)
    (else (+ (fib (- n 1)) 
             (fib (- n 2))))))

The book mentions that the space this algorithm takes up is 𝜃(n) and the time it takes is 𝜃n). I understand the space complexity since it is the max depth of a tree, but I can't seem to wrap my head around how the time complexity is derived.

1 Answer 1

1

If you write down how much time it takes in each case (1 is constant),

T(0) = 1
T(1) = 1
T(n) = T(n-1) + T(n-2)

you see that it is exactly the same as the Fibonacci numbers.
And as the text (almost) says, Fib(n) is 𝜃(φn).

Sign up to request clarification or add additional context in comments.

2 Comments

where φ = (1 + sqrt(5)) / 2? But that is the approximate size of the fib(n), not the complexity of computing it.
@PresidentJamesK.Polk If you take f(10) and use the substitution model you'll end up with a line adding 1's together f(10) times. Thus the complexity of fib(n) is O(fib(n))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.