1. Fall 2002 CMSC 203 - Discrete Structures 1
Now it’s Time for…
Now it’s Time for…
Recurrence
Recurrence
Relations
Relations
2. Fall 2002 CMSC 203 - Discrete Structures 2
Recurrence Relations
Recurrence Relations
A
A recurrence relation
recurrence relation for the sequence {a
for the sequence {an
n} is an
} is an
equation that expresses a
equation that expresses an
n is terms of one or
is terms of one or
more of the previous terms of the sequence,
more of the previous terms of the sequence,
namely, a
namely, a0
0, a
, a1
1, …, a
, …, an-1
n-1, for all integers n with
, for all integers n with
n
n
n
n0
0, where n
, where n0
0 is a nonnegative integer.
is a nonnegative integer.
A sequence is called a
A sequence is called a solution
solution of a recurrence
of a recurrence
relation if it terms satisfy the recurrence
relation if it terms satisfy the recurrence
relation.
relation.
3. Fall 2002 CMSC 203 - Discrete Structures 3
Recurrence Relations
Recurrence Relations
In other words, a recurrence relation is like a
In other words, a recurrence relation is like a
recursively defined sequence, but
recursively defined sequence, but without
without
specifying any initial values (initial conditions)
specifying any initial values (initial conditions).
.
Therefore, the same recurrence relation can have
Therefore, the same recurrence relation can have
(and usually has)
(and usually has) multiple solutions
multiple solutions.
.
If
If both
both the initial conditions and the recurrence
the initial conditions and the recurrence
relation are specified, then the sequence is
relation are specified, then the sequence is
uniquely
uniquely determined.
determined.
4. Fall 2002 CMSC 203 - Discrete Structures 4
Recurrence Relations
Recurrence Relations
Example:
Example:
Consider the recurrence relation
Consider the recurrence relation
a
an
n = 2a
= 2an-1
n-1 – a
– an-2
n-2 for n = 2, 3, 4, …
for n = 2, 3, 4, …
Is the sequence {a
Is the sequence {an
n} with a
} with an
n=3n a solution of this
=3n a solution of this
recurrence relation?
recurrence relation?
For n
For n
2 we see that
2 we see that
2a
2an-1
n-1 – a
– an-2
n-2 = 2(3(n – 1)) – 3(n – 2) = 3n = a
= 2(3(n – 1)) – 3(n – 2) = 3n = an
n.
.
Therefore, {a
Therefore, {an
n} with a
} with an
n=3n is a solution of the
=3n is a solution of the
recurrence relation.
recurrence relation.
5. Fall 2002 CMSC 203 - Discrete Structures 5
Recurrence Relations
Recurrence Relations
Is the sequence {a
Is the sequence {an
n} with a
} with an
n=5 a solution of the
=5 a solution of the
same recurrence relation?
same recurrence relation?
For n
For n
2 we see that
2 we see that
2a
2an-1
n-1 – a
– an-2
n-2 = 2
= 2
5 - 5 = 5 = a
5 - 5 = 5 = an
n.
.
Therefore, {a
Therefore, {an
n} with a
} with an
n=5 is also a solution of the
=5 is also a solution of the
recurrence relation.
recurrence relation.
6. Fall 2002 CMSC 203 - Discrete Structures 6
Modeling with Recurrence Relations
Modeling with Recurrence Relations
Example:
Example:
Someone deposits $10,000 in a savings account at
Someone deposits $10,000 in a savings account at
a bank yielding 5% per year with interest
a bank yielding 5% per year with interest
compounded annually. How much money will be in
compounded annually. How much money will be in
the account after 30 years?
the account after 30 years?
Solution:
Solution:
Let P
Let Pn
n denote the amount in the account after n
denote the amount in the account after n
years.
years.
How can we determine P
How can we determine Pn
n on the basis of P
on the basis of Pn-1
n-1?
?
7. Fall 2002 CMSC 203 - Discrete Structures 7
Modeling with Recurrence Relations
Modeling with Recurrence Relations
We can derive the following
We can derive the following recurrence relation
recurrence relation:
:
P
Pn
n = P
= Pn-1
n-1 + 0.05P
+ 0.05Pn-1
n-1 = 1.05P
= 1.05Pn-1
n-1.
.
The initial condition is P
The initial condition is P0
0 = 10,000.
= 10,000.
Then we have:
Then we have:
P
P1
1 = 1.05P
= 1.05P0
0
P
P2
2 = 1.05P
= 1.05P1
1 = (1.05)
= (1.05)2
2
P
P0
0
P
P3
3 = 1.05P
= 1.05P2
2 = (1.05)
= (1.05)3
3
P
P0
0
…
…
P
Pn
n = 1.05P
= 1.05Pn-1
n-1 = (1.05)
= (1.05)n
n
P
P0
0
We now have a
We now have a formula
formula to calculate P
to calculate Pn
n for any natural
for any natural
number n and can avoid the iteration.
number n and can avoid the iteration.
8. Fall 2002 CMSC 203 - Discrete Structures 8
Modeling with Recurrence Relations
Modeling with Recurrence Relations
Let us use this formula to find P
Let us use this formula to find P30
30 under the
under the
initial condition P
initial condition P0
0 = 10,000:
= 10,000:
P
P30
30 = (1.05)
= (1.05)30
30
10,000 = 43,219.42
10,000 = 43,219.42
After 30 years, the account contains $43,219.42.
After 30 years, the account contains $43,219.42.
9. Fall 2002 CMSC 203 - Discrete Structures 9
Modeling with Recurrence Relations
Modeling with Recurrence Relations
Another example:
Another example:
Let a
Let an
n denote the number of bit strings of length
denote the number of bit strings of length
n that do not have two consecutive 0s (“valid
n that do not have two consecutive 0s (“valid
strings”). Find a recurrence relation and give
strings”). Find a recurrence relation and give
initial conditions for the sequence {a
initial conditions for the sequence {an
n}.
}.
Solution:
Solution:
Idea: The number of valid strings equals the
Idea: The number of valid strings equals the
number of valid strings ending with a 0 plus the
number of valid strings ending with a 0 plus the
number of valid strings ending with a 1.
number of valid strings ending with a 1.
10. Fall 2002 CMSC 203 - Discrete Structures 10
Modeling with Recurrence Relations
Modeling with Recurrence Relations
Let us assume that n
Let us assume that n
3, so that the string
3, so that the string
contains at least 3 bits.
contains at least 3 bits.
Let us further assume that we know the number
Let us further assume that we know the number
a
an-1
n-1 of valid strings of length (n – 1).
of valid strings of length (n – 1).
Then how many valid strings of length n are there,
Then how many valid strings of length n are there,
if the string ends with a 1?
if the string ends with a 1?
There are a
There are an-1
n-1 such strings, namely the set of valid
such strings, namely the set of valid
strings of length (n – 1) with a 1 appended to
strings of length (n – 1) with a 1 appended to
them.
them.
Note:
Note: Whenever we append a 1 to a valid string,
Whenever we append a 1 to a valid string,
that string remains valid.
that string remains valid.
11. Fall 2002 CMSC 203 - Discrete Structures 11
Modeling with Recurrence Relations
Modeling with Recurrence Relations
Now we need to know: How many valid strings of
Now we need to know: How many valid strings of
length n are there, if the string ends with a
length n are there, if the string ends with a 0
0?
?
Valid strings of length n ending with a 0
Valid strings of length n ending with a 0 must
must
have a 1 as their (n – 1)st bit
have a 1 as their (n – 1)st bit (otherwise they
(otherwise they
would end with 00 and would not be valid).
would end with 00 and would not be valid).
And what is the number of valid strings of length
And what is the number of valid strings of length
(n – 1) that end with a 1?
(n – 1) that end with a 1?
We already know that there are a
We already know that there are an-1
n-1 strings of
strings of
length n that end with a 1.
length n that end with a 1.
Therefore, there are a
Therefore, there are an-2
n-2 strings of length (n – 1)
strings of length (n – 1)
that end with a 1.
that end with a 1.
12. Fall 2002 CMSC 203 - Discrete Structures 12
Modeling with Recurrence Relations
Modeling with Recurrence Relations
So there are a
So there are an-2
n-2 valid strings of length n that end
valid strings of length n that end
with a 0 (all valid strings of length (n – 2) with 10
with a 0 (all valid strings of length (n – 2) with 10
appended to them).
appended to them).
As we said before, the number of valid strings is
As we said before, the number of valid strings is
the number of valid strings ending with a 0 plus
the number of valid strings ending with a 0 plus
the number of valid strings ending with a 1.
the number of valid strings ending with a 1.
That gives us the following
That gives us the following recurrence relation
recurrence relation:
:
a
an
n = a
= an-1
n-1 + a
+ an-2
n-2
13. Fall 2002 CMSC 203 - Discrete Structures 13
Modeling with Recurrence Relations
Modeling with Recurrence Relations
What are the
What are the initial conditions
initial conditions?
?
a
a1
1 = 2 (0 and 1)
= 2 (0 and 1)
a
a2
2 = 3 (01, 10, and 11)
= 3 (01, 10, and 11)
a
a3
3 = a
= a2
2 + a
+ a1
1 = 3 + 2 = 5
= 3 + 2 = 5
a
a4
4 = a
= a3
3 + a
+ a2
2 = 5 + 3 = 8
= 5 + 3 = 8
a
a5
5 = a
= a4
4 + a
+ a3
3 = 8 + 5 = 13
= 8 + 5 = 13
…
…
This sequence satisfies the same recurrence
This sequence satisfies the same recurrence
relation as the
relation as the Fibonacci sequence
Fibonacci sequence.
.
Since a
Since a1
1 = f
= f3
3 and a
and a2
2 = f
= f4
4, we have a
, we have an
n = f
= fn+2
n+2.
.
14. Fall 2002 CMSC 203 - Discrete Structures 14
Solving Recurrence Relations
Solving Recurrence Relations
In general, we would prefer to have an
In general, we would prefer to have an explicit
explicit
formula
formula to compute the value of a
to compute the value of an
n rather than
rather than
conducting n iterations.
conducting n iterations.
For one class of recurrence relations, we can
For one class of recurrence relations, we can
obtain such formulas in a systematic way.
obtain such formulas in a systematic way.
Those are the recurrence relations that express
Those are the recurrence relations that express
the terms of a sequence as
the terms of a sequence as linear combinations
linear combinations of
of
previous terms.
previous terms.
15. Fall 2002 CMSC 203 - Discrete Structures 15
Solving Recurrence Relations
Solving Recurrence Relations
Definition:
Definition: A linear homogeneous recurrence
A linear homogeneous recurrence
relation of degree k with constant coefficients is
relation of degree k with constant coefficients is
a recurrence relation of the form:
a recurrence relation of the form:
a
an
n = c
= c1
1a
an-1
n-1 + c
+ c2
2a
an-2
n-2 + … + c
+ … + ck
ka
an-k
n-k,
,
Where c
Where c1
1, c
, c2
2, …, c
, …, ck
k are real numbers, and c
are real numbers, and ck
k
0.
0.
A sequence satisfying such a recurrence relation
A sequence satisfying such a recurrence relation
is uniquely determined by the recurrence relation
is uniquely determined by the recurrence relation
and the k initial conditions
and the k initial conditions
a
a0
0 = C
= C0
0, a
, a1
1 = C
= C1
1, a
, a2
2 = C
= C2
2, …, a
, …, ak-1
k-1 = C
= Ck-1
k-1.
.
16. Fall 2002 CMSC 203 - Discrete Structures 16
Solving Recurrence Relations
Solving Recurrence Relations
Examples:
Examples:
The recurrence relation P
The recurrence relation Pn
n = (1.05)P
= (1.05)Pn-1
n-1
is a linear homogeneous recurrence relation of
is a linear homogeneous recurrence relation of
degree one
degree one.
.
The recurrence relation f
The recurrence relation fn
n = f
= fn-1
n-1 + f
+ fn-2
n-2
is a linear homogeneous recurrence relation of
is a linear homogeneous recurrence relation of
degree two
degree two.
.
The recurrence relation a
The recurrence relation an
n = a
= an-5
n-5
is a linear homogeneous recurrence relation of
is a linear homogeneous recurrence relation of
degree five
degree five.
.
17. Fall 2002 CMSC 203 - Discrete Structures 17
Solving Recurrence Relations
Solving Recurrence Relations
Basically, when solving such recurrence relations,
Basically, when solving such recurrence relations,
we try to find solutions of the form
we try to find solutions of the form a
an
n = r
= rn
n
, where
, where
r is a constant.
r is a constant.
a
an
n = r
= rn
n
is a solution of the recurrence relation
is a solution of the recurrence relation
a
an
n = c
= c1
1a
an-1
n-1 + c
+ c2
2a
an-2
n-2 + … + c
+ … + ck
ka
an-k
n-k if and only if
if and only if
r
rn
n
= c
= c1
1r
rn-1
n-1
+ c
+ c2
2r
rn-2
n-2
+ … + c
+ … + ck
kr
rn-k
n-k
.
.
Divide this equation by r
Divide this equation by rn-k
n-k
and subtract the right-
and subtract the right-
hand side from the left:
hand side from the left:
r
rk
k
- c
- c1
1r
rk-1
k-1
- c
- c2
2r
rk-2
k-2
- … - c
- … - ck-1
k-1r - c
r - ck
k = 0
= 0
This is called the
This is called the characteristic equation
characteristic equation of the
of the
recurrence relation.
recurrence relation.
18. Fall 2002 CMSC 203 - Discrete Structures 18
Solving Recurrence Relations
Solving Recurrence Relations
The solutions of this equation are called the
The solutions of this equation are called the
characteristic roots
characteristic roots of the recurrence relation.
of the recurrence relation.
Let us consider linear homogeneous recurrence
Let us consider linear homogeneous recurrence
relations of
relations of degree two
degree two.
.
Theorem:
Theorem: Let c
Let c1
1 and c
and c2
2 be real numbers. Suppose
be real numbers. Suppose
that r
that r2
2
– c
– c1
1r – c
r – c2
2 = 0 has two distinct roots r
= 0 has two distinct roots r1
1 and r
and r2
2.
.
Then the sequence {a
Then the sequence {an
n} is a solution of the
} is a solution of the
recurrence relation a
recurrence relation an
n = c
= c1
1a
an-1
n-1 + c
+ c2
2a
an-2
n-2 if and only if a
if and only if an
n
=
=
1
1r
r1
1
n
n
+
+
2
2r
r2
2
n
n
for n = 0, 1, 2, …, where
for n = 0, 1, 2, …, where
1
1 and
and
2
2 are
are
constants.
constants.
See pp. 321 and 322 for the proof.
See pp. 321 and 322 for the proof.
19. Fall 2002 CMSC 203 - Discrete Structures 19
Solving Recurrence Relations
Solving Recurrence Relations
Example:
Example: What is the solution of the recurrence
What is the solution of the recurrence
relation a
relation an
n = a
= an-1
n-1 + 2a
+ 2an-2
n-2 with a
with a0
0 = 2 and a
= 2 and a1
1 = 7 ?
= 7 ?
Solution:
Solution: The characteristic equation of the
The characteristic equation of the
recurrence relation is r
recurrence relation is r2
2
– r – 2 = 0.
– r – 2 = 0.
Its roots are r = 2 and r = -1.
Its roots are r = 2 and r = -1.
Hence, the sequence {a
Hence, the sequence {an
n} is a solution to the
} is a solution to the
recurrence relation if and only if:
recurrence relation if and only if:
a
an
n =
=
1
12
2n
n
+
+
2
2(-1)
(-1)n
n
for some constants
for some constants
1
1 and
and
2
2.
.
20. Fall 2002 CMSC 203 - Discrete Structures 20
Solving Recurrence Relations
Solving Recurrence Relations
Given the equation a
Given the equation an
n =
=
1
12
2n
n
+
+
2
2(-1)
(-1)n
n
and the initial
and the initial
conditions a
conditions a0
0 = 2 and a
= 2 and a1
1 = 7, it follows that
= 7, it follows that
a
a0
0 = 2 =
= 2 =
1
1 +
+
2
2
a
a1
1 = 7 =
= 7 =
1
1
2 +
2 +
2
2
(-1)
(-1)
Solving these two equations gives us
Solving these two equations gives us
1
1 = 3 and
= 3 and
2
2 = -1.
= -1.
Therefore, the solution to the recurrence relation and
Therefore, the solution to the recurrence relation and
initial conditions is the sequence {a
initial conditions is the sequence {an
n} with
} with
a
an
n = 3
= 3
2
2n
n
– (-1)
– (-1)n
n
.
.
21. Fall 2002 CMSC 203 - Discrete Structures 21
Solving Recurrence Relations
Solving Recurrence Relations
a
an
n = r
= rn
n
is a solution of the linear homogeneous
is a solution of the linear homogeneous
recurrence relation
recurrence relation
a
an
n = c
= c1
1a
an-1
n-1 + c
+ c2
2a
an-2
n-2 + … + c
+ … + ck
ka
an-k
n-k
if and only if
if and only if
r
rn
n
= c
= c1
1r
rn-1
n-1
+ c
+ c2
2r
rn-2
n-2
+ … + c
+ … + ck
kr
rn-k
n-k
.
.
Divide this equation by r
Divide this equation by rn-k
n-k
and subtract the right-
and subtract the right-
hand side from the left:
hand side from the left:
r
rk
k
- c
- c1
1r
rk-1
k-1
- c
- c2
2r
rk-2
k-2
- … - c
- … - ck-1
k-1r - c
r - ck
k = 0
= 0
This is called the
This is called the characteristic equation
characteristic equation of the
of the
recurrence relation.
recurrence relation.
22. Fall 2002 CMSC 203 - Discrete Structures 22
Solving Recurrence Relations
Solving Recurrence Relations
The solutions of this equation are called the
The solutions of this equation are called the
characteristic roots
characteristic roots of the recurrence relation.
of the recurrence relation.
Let us consider linear homogeneous recurrence
Let us consider linear homogeneous recurrence
relations of
relations of degree two
degree two.
.
Theorem:
Theorem: Let c
Let c1
1 and c
and c2
2 be real numbers. Suppose
be real numbers. Suppose
that r
that r2
2
– c
– c1
1r – c
r – c2
2 = 0 has two distinct roots r
= 0 has two distinct roots r1
1 and r
and r2
2.
.
Then the sequence {a
Then the sequence {an
n} is a solution of the
} is a solution of the
recurrence relation a
recurrence relation an
n = c
= c1
1a
an-1
n-1 + c
+ c2
2a
an-2
n-2 if and only if a
if and only if an
n
=
=
1
1r
r1
1
n
n
+
+
2
2r
r2
2
n
n
for n = 0, 1, 2, …, where
for n = 0, 1, 2, …, where
1
1 and
and
2
2 are
are
constants.
constants.
See pp. 321 and 322 for the proof.
See pp. 321 and 322 for the proof.
23. Fall 2002 CMSC 203 - Discrete Structures 23
Solving Recurrence Relations
Solving Recurrence Relations
Example:
Example: Give an explicit formula for the Fibonacci
Give an explicit formula for the Fibonacci
numbers.
numbers.
Solution:
Solution: The Fibonacci numbers satisfy the
The Fibonacci numbers satisfy the
recurrence relation f
recurrence relation fn
n = f
= fn-1
n-1 + f
+ fn-2
n-2 with initial conditions
with initial conditions
f
f0
0 = 0 and f
= 0 and f1
1 = 1.
= 1.
The characteristic equation is r
The characteristic equation is r2
2
– r – 1 = 0.
– r – 1 = 0.
Its roots are
Its roots are
2
5
1
,
2
5
1
2
1
r
r
24. Fall 2002 CMSC 203 - Discrete Structures 24
Solving Recurrence Relations
Solving Recurrence Relations
Therefore, the Fibonacci numbers are given by
Therefore, the Fibonacci numbers are given by
n
n
n
f
2
5
1
2
5
1
2
1
for some constants
for some constants
1
1 and
and
2
2.
.
We can determine values for these constants so
We can determine values for these constants so
that the sequence meets the conditions f
that the sequence meets the conditions f0
0 = 0 and
= 0 and
f
f1
1 = 1:
= 1:
0
2
1
0
f
1
2
5
1
2
5
1
2
1
1
f
25. Fall 2002 CMSC 203 - Discrete Structures 25
Solving Recurrence Relations
Solving Recurrence Relations
The unique solution to this system of two
The unique solution to this system of two
equations and two variables is
equations and two variables is
5
1
,
5
1
2
1
So finally we obtained an explicit formula for the
So finally we obtained an explicit formula for the
Fibonacci numbers:
Fibonacci numbers:
n
n
n
f
2
5
1
5
1
2
5
1
5
1
26. Fall 2002 CMSC 203 - Discrete Structures 26
Solving Recurrence Relations
Solving Recurrence Relations
But what happens if the characteristic equation has
But what happens if the characteristic equation has
only one root?
only one root?
How can we then match our equation with the initial
How can we then match our equation with the initial
conditions a
conditions a0
0 and a
and a1
1 ?
?
Theorem:
Theorem: Let c
Let c1
1 and c
and c2
2 be real numbers with c
be real numbers with c2
2
0.
0.
Suppose that r
Suppose that r2
2
– c
– c1
1r – c
r – c2
2 = 0 has only one root r
= 0 has only one root r0
0.
.
A sequence {a
A sequence {an
n} is a solution of the recurrence
} is a solution of the recurrence
relation a
relation an
n = c
= c1
1a
an-1
n-1 + c
+ c2
2a
an-2
n-2 if and only if
if and only if
a
an
n =
=
1
1r
r0
0
n
n
+
+
2
2nr
nr0
0
n
n
, for n = 0, 1, 2, …, where
, for n = 0, 1, 2, …, where
1
1 and
and
2
2
are constants.
are constants.
27. Fall 2002 CMSC 203 - Discrete Structures 27
Solving Recurrence Relations
Solving Recurrence Relations
Example:
Example: What is the solution of the recurrence
What is the solution of the recurrence
relation a
relation an
n = 6a
= 6an-1
n-1 – 9a
– 9an-2
n-2 with a
with a0
0 = 1 and a
= 1 and a1
1 = 6?
= 6?
Solution:
Solution: The only root of r
The only root of r2
2
– 6r + 9 = 0 is r
– 6r + 9 = 0 is r0
0 = 3.
= 3.
Hence, the solution to the recurrence relation is
Hence, the solution to the recurrence relation is
a
an
n =
=
1
13
3n
n
+
+
2
2n3
n3n
n
for some constants
for some constants
1
1 and
and
2
2.
.
To match the initial condition, we need
To match the initial condition, we need
a
a0
0 = 1 =
= 1 =
1
1
a
a1
1 = 6 =
= 6 =
1
1
3 +
3 +
2
2
3
3
Solving these equations yields
Solving these equations yields
1
1 = 1 and
= 1 and
2
2 = 1.
= 1.
Consequently, the overall solution is given by
Consequently, the overall solution is given by
a
an
n = 3
= 3n
n
+ n3
+ n3n
n
.
.