Escolar Documentos
Profissional Documentos
Cultura Documentos
\
|
+
|
|
.
|
\
|
+
=
2
5 1
2
5 1
2 1
o o
for some constants o
1
and o
2
.
We can determine values for these constants so
that the sequence meets the conditions f
0
= 0
and f
1
= 1:
0
2 1 0
= + = o o f
1
2
5 1
2
5 1
2 1 1
=
|
|
.
|
\
|
+
|
|
.
|
\
|
+
= o o f
You model the time function to calculate Fib(n) as sum of time to calculate Fib(n-1) plus the time to
calculate Fib(n-2) plus the time to add them together (O(1)).
T(n<=1) = O(1)
T(n) = T(n-1) + T(n-2) + O(1)
You solve this recurrence relation (using generating functions, for instance) and you'll end up with the
answer.
Alternatively, you can draw the recursion tree, which will have depth n and intuitively figure out that
this function is asymptotically O(2
n
). You can then prove your conjecture by induction.
Base: n = 1 is obvious
Assume T(n-1) = O(2
n-1
), therefore
T(n) = T(n-1) + T(n-2) + O(1) which is equal to
T(n) = O(2
n-1
) + O(2
n-2
) + O(1) = O(2
n
)
However, as noted in a comment, this is not the tight bound. An interesting fact about this function
is that the T(n) is asymptotically the same as the value of Fib(n) since both are defined as
f(n) = f(n-1) + f(n-2).
The leaves of the recursion tree will always return 1. The value of Fib(n) is sum of all values returned
by the leaves in the recursion tree which is equal to the count of leaves. Since each leaf will take
O(1) to compute, T(n) is equal to Fib(n) x O(1). Consequently, the tight bound for this function is the
Fibonacci sequence itself (~(1.6
n
)). You can find out this tight bound by using generating functions as
I'd mentioned above.
Fall 2002 CMSC 203 - Discrete Structures 25
Fall 2002 CMSC 203 - Discrete Structures 26
Solving Recurrence Relations
The unique solution to this system of two
equations and two variables is
5
1
,
5
1
2 1
= = o o
So finally we obtained an explicit formula for the
Fibonacci numbers:
n n
n
f
|
|
.
|
\
|
|
|
.
|
\
|
+
=
2
5 1
5
1
2
5 1
5
1
Fall 2002 CMSC 203 - Discrete Structures 27
Solving Recurrence Relations
But what happens if the characteristic equation
has only one root?
How can we then match our equation with the initial
conditions a
0
and a
1
?
Theorem: Let c
1
and c
2
be real numbers with c
2
= 0.
Suppose that r
2
c
1
r c
2
= 0 has only one root r
0
.
A sequence {a
n
} is a solution of the recurrence
relation a
n
= c
1
a
n-1
+ c
2
a
n-2
if and only if
a
n
= o
1
r
0
n
+ o
2
nr
0
n
, for n = 0, 1, 2, , where o
1
and o
2
are constants.
Fall 2002 CMSC 203 - Discrete Structures 28
Solving Recurrence Relations
Example: What is the solution of the recurrence
relation a
n
= 6a
n-1
9a
n-2
with a
0
= 1 and a
1
= 6?
Solution: The only root of r
2
6r + 9 = 0 is r
0
= 3.
Hence, the solution to the recurrence relation is
a
n
= o
1
3
n
+ o
2
n3
n
for some constants o
1
and o
2
.
To match the initial condition, we need
a
0
= 1 = o
1
a
1
= 6 = o
1
3 + o
2
3
Solving these equations yields o
1
= 1 and o
2
= 1.
Consequently, the overall solution is given by
a
n
= 3
n
+ n3
n
.