|
CS 3343/3341 Analysis of Algorithms Fall 2012 Recitation 2 In-Class Problems Week 2: Sep 4-6 |
// the recursive function f
// returns the nth Fibonacci number, n >= 0
int f(int n) {
if (n <= 1) return n;
return f(n-1) + f(n-2);
}
// Trees of function calls in calculating f(0),f(1),f(2),f(3).
A[0]: f(0), A[1]: f(1), A[2]: f(2) A[3]: f(3)
/ \ / \
f(0) f(1) f(1) f(2)
/ \
f(0) f(1)
A[4]: f(4)
/ \
f(2) f(3)
/ \ / \
f(0) f(1) f(1) f(2)
/ \
f(0) f(1)
|
| n | A[n] | F[n+1] |
|---|---|---|
| 0 | 1 | 1 |
| 1 | 1 | 1 |
| 2 | 3 | 2 |
| 3 | 3 | |
| 4 | ||
| 5 | ||
| 6 | ||
| 7 | ||
| 8 | ||
| 9 | ||
| 10 | ||
| 11 |