Example

Example - Fibonacci numbers Fibonacci numbers 0 1 1 2 3 5 8...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Fibonacci numbers Fibonacci numbers : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . .. where each number is the sum of the preceding two. Recursive definition: F(0) = 0; F(1) = 1; F(number) = F(number-1)+ F(number- 2);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Redundant Calculations I To compute fib(n), we recursively compute fib(n-1). When the recursive call return, we compute fib(n-2) using another recursive call We have already computed fib(n-2) in the process of computing fib(n-1) We make two calls to fib(n-2)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Redundant Calculations II Making two method calls would double the running time Compounding effect: each recursive call does more and more redundant work Each call to fib(n-1) and each call to fib(n-2) makes a call to fib(n-3); there are 3 calls to fib(n-3) Each call to fib(n-2) or fib(n-3) results in a call to fib(n-4), so 5 calls to fib(n-4)
Background image of page 4
C(n): number of calls to fib method C(0)=C(1)=1; For n>=2, we call fib(n) and plus all the calls needed to evaluate fib(n-1) and fib(n-
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

Example - Fibonacci numbers Fibonacci numbers 0 1 1 2 3 5 8...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online