Solving Recurrences
Solving Recurrences
Solving Recurrences
r
n
r
n 1
+r
n 2
r
2
r + 1
r
2
r 1 0
r
1
1+ 5
2
; r
2
1 5
2
Well, so the guess that
T
n
r
n
is a good one provided that r be equal to one of the two roots above. In
fact, any linear combination of
1+ 5
2
_
,
n
and
1 5
2
_
,
n
will also satisfy the recurrence if you ignore the
initial conditions for a momentthat is,
T
n
b
1
1+ 5
2
_
,
n
+ b
2
1 5
2
_
,
n
for any constant coefficients as
factors. Its only when you supply the initial conditions that
b
1
and
b
2
are required to adopt a specific
value. In fact, the initial conditions here dictate that:
2
b
1
+ b
2
1
b
1
1 + 5
2
+ b
2
1 5
2
1
And after solving for
b
1
and
b
2
do we finally arrive at the unique solution to our recurrence problem:
T
n
5 + 5
10
1 + 5
2
_
,
n
+
5 5
10
1 5
2
_
,
n
How very satisfying.
Problem: Find a closed form solution to the bit string problem modeled in our last
handout.
Welllllll, the recurrence relation, save the initial conditions, is exactly the same as it that for the tiling
problem; that translates to a solution of the same basic form. But the fact that the initial conditions are
different hints that the constants multiplying the individual terms:
note same form, but possibly
different constants
The different initial conditions place different constraint on the values of
b
1
and
b
2
do we. Now the
following system of equations must be solved:
b
1
+ b
2
1
b
1
1 + 5
2
+ b
2
1 5
2
2
We end up with something like this as our solution in this case:
B
n
5 + 3 5
10
1 + 5
2
_
,
n
+
5 3 5
10
1 5
2
_
,
n
Problem: Solve the recurrence relation given below
J
n
J
n2
J
0
3J
1
5
You may be asking, Jerry, where in the world is the
J
n 1
term? Relaxits in thereits just that its
multiplying coefficient is zero, so I didnt bother writing it in. Its still a homogeneous equation of degree
two, so I solve it like any other recurrence of this type.
B
n
b
1
1+ 5
2
_
,
n
+ b
2
1 5
2
_
,
n
3
J
n
J
n2
J
0
3J
1
5
J
n
J
n2
r
2
1
r
2
1 0
r
1
1;r
2
1
J
n
b
1
1 ( )
n
+ b
2
1 ( )
n
b
1
+ b
2
1 ( )
n
Funny little twistdropping the
1 ( )
n
, since its transparent to us anyway.
b
1
+ b
2
5
b
1
b
2
5
3
b
1
10
3
,b
2
5
3
J
n
10
3
+
5
3
1 ( )
n
Solving Coupled Recurrence Relations
The first recurrences handout included examples where solutions involved coupled recurrence relations.
The circular Tower of Hanoi Problem, the
n 3 tiling problem, the
2 2 npillar problem, and the
Martian DNA problem were all complex enough to require (or at least benefit from) the invention of a
second counting problem and a second recurrence variable. Remember the
2 2 n pillar recurrence? If not,
here it is once more:
S
n
1
2
2S
n1
+S
n 2
+ 4T
n1
n 0
n 1
n 2
'
T
n
0
1
S
n1
+T
n1
n 0
n 1
n 2
'
Because S and T are defined in terms of one another, its possible to use the second recurrence of the two to
eliminate all occurrences or T from the first. It takes a algebra and a little ingenuity, but when we rid of
the second variable, we are often left with a single recurrence relation which can be solved like other
recurrences weve seen before. The above system of recurrences reduces to a single linear, homogeneous,
constant-coefficient equation once we get rid of T. Dont believe me? Read on, Thomas.
Problem: Solve the above system of recurrences for
S
n
by eliminating
T
n
and
solving the linear equation that results.
I want to completely eliminate all traces of
T and arrive at (what will turn out to
be) a (cubic) recurrence relation for just
S
n
, and then solve it. First things first: We
want to get rid of the
T
n1
term from the recurrence relation for
S
n
, and to do that, I make the neat little
observation that the second equality below follows from the first by replacing n by n-1:
S
n
2S
n1
+S
n 2
+ 4T
n1
S
n1
2S
n 2
+ S
n3
+ 4T
n2
Subtracting the second equation from the first, we get:
4
S
n
S
n1
2S
n 1
+S
n 2
+ 4T
n1 ( ) 2S
n2
+S
n 3
+ 4T
n 2 ( )
2S
n1
S
n2
S
n 3
+4T
n 1
4T
n 2
S
n
3S
n1
S
n2
S
n 3
+4T
n1
4T
n 2
3S
n1
S
n2
S
n 3
+4 T
n 1
T
n 2
( )
Believe it or not, this is progress, because I have something very interesting to say about
T
n1
T
n 2
its
always equal to
S
n 2
. Just look at the crafty manipulations I work up from the
T
n
recurrence relation:
T
n
S
n1
+ T
n1
T
n1
S
n 2
+ T
n 2
T
n1
T
n 2
S
n 2
+ T
n 2
T
n2
S
n 2
How crafty! Now I can eradicate any mention of
T from the
S
n
recurrence relation, and I do so like-a this:
S
n
3S
n1
S
n 2
S
n3
+ 4 T
n1
T
n 2 ( )
3S
n1
S
n 2
S
n3
+ 4S
n2
3S
n1
+3S
n 2
S
n3
Therefore, an uncoupled recurrence relation for
S
n
can be expressed as follows:
S
n
1
2
2S
1
+ S
0
+ 4T
1
9
3S
n1
+3S
n 2
S
n3
n 0
n 1
n 2
n 3
'
Admittedly, that was a lot of work, but this is pretty exciting, because were on the verge of taking what
is clearly a homogeneous, constant-coefficient equation and coming up with a closed form solution. As
always, guess a solution of the form
S
n
a
n
and substitute to see what values of
a make everything work
out regardless of the initial conditions. More algebra:
S
n
S n a
n 3S
n1
+ 3S
n 2
S
n3
Sn a
n
a
n
3a
n1
+ 3a
n 2
a
n3
a
3
3a
2
+ 3a 1
0 a
3
3a
2
3a +1
0 a + 1 ( ) a
2
4a +1
( )
0 a + 1 ( ) a 2 + 3
( )
a 2 3
( )
a 1, 2 t 3
That means that the general solution to our recurrence (ignoring the initial conditions) is
S
n
x 2 + 3 ( )
n
+y 2 3 ( )
n
+ z 1 ( )
n
, where x, y, and z can be any real numbers. Boundary conditions
require that:
5
S
0
x + y+ z 1
S
1
2 + 3 ( )x + 2 3 ( )y z 2
S
2
7 + 4 3
( )
x + 1 4 3
( )
y + z 9
This is a system of three linear equations for three unknowns, and it admits exactly one solution. Solving
for x, y, and z is a matter of algebra, and after all that algebra is over, we converge on a closed formula of
S
n
1
6
2 + 3 ( )
n+1
+
1
6
2 3 ( )
n+1
+
1
3
1 ( )
n
, which is
1
6
2 + 3 ( )
n+1
rounded to the nearest integer. Neat!
Problem: Revisit the Martian DNA problem, and show that the number of valid
Martian DNA strands of length n is given as
a
n
+ b
n
F
3n + 2
(yes, the Fibonacci
number!)
Lets bring back the recurrence that defined a and b.
a
n
0 n 0
2 n 1
a
n1
+2b
n1
n 2
'
b
n
1 n 0
3 n 1
2a
n 1
+ 3b
n1
n 2
'
Eliminate one of the recurrence terms in order to get a closed solution (though this time well ultimately
need to solve for both variables, because youre interested in their sum.)
a
n
a
n1
+ 2b
n 1
b
n
2a
n1
+3b
n1
Notice that the first one tells us something about
a
n
a
n1
, so lets subtract a shifted version of the second
equation from the original to get at something thats all b and no a.
b
n
2a
n 1
+ 3b
n1
b
n1
2a
n 2
+3b
n2
b
n
b
n1
2 a
n1
a
n 2 ( ) +3b
n1
3b
n 2
b
n
b
n1
+ 2 a
n1
a
n 2
( ) + 3b
n1
3b
n 2
The first equation tells us that
a
n1
a
n 2
2b
n 2
. Substitution yields
b
n
b
n1
+ 4b
n 2
+3b
n1
3b
n 2
4b
n1
+b
n 2
The characteristic equation here is
r
2
4r 1 0 ;
b
n
c
1
2 + 5 ( )
n
+ c
2
2 5 ( )
n
. Because
b
0
1 and
b
1
3 , we determine
c
1
,
c
2
by solving the following two equations:
6
c
1
+c
2
1
2 + 5
( )
c
1
+ 2 5
( )
c
2
3
c
1
5 + 5
10
,
c
2
5 5
10
.
b
n
5 + 5
10
2 + 5 ( )
n
+
5 5
10
2 5 ( )
n
.
You might think you have to do the same thing for a, and in theory youre right in that you need to solve
it, but we more or less have. Because
b
n
2a
n1
+3b
n1
we actually know that
b
n
2a
n1
+ 3b
n1
2a
n1
b
n
3b
n1
a
n1
1
2
b
n
3
2
b
n 1
a
n
1
2
b
n+1
3
2
b
n
a
n
+ b
n
1
2
b
n+1
3
2
b
n
+ b
n
1
2
b
n+1
1
2
b
n
Recall that were really just interested in
a
n
+ b
n
, and since it can be defined just in terms of the
b
n
, we get:
a
n
+ b
n
1
2
b
n+1
3
2
b
n
+ b
n
1
2
b
n+1
1
2
b
n
1
2
5 + 5
10
2 + 5
( )
n+1
+
5 5
10
2 5
( )
n+1
_
,
1
2
5 + 5
10
2 + 5
( )
n
+
5 5
10
2 5
( )
n
_
,
1
2
5 + 5
10
2 + 5 ( ) 1
( )
2 + 5 ( )
n
+
1
2
5 5
10
2 5 ( ) 1
( )
2 5 ( )
n
1
2
5 + 5
10
1 + 5
( )
2 + 5
( )
n
+
1
2
5 5
10
1 5
( )
2 5
( )
n
1
2
10 + 6 5
10
2 + 5
( )
n
+
1
2
10 6 5
10
2 5
( )
n
5 + 3 5
10
2 + 5
( )
n
+
5 3 5
10
2 5
( )
n
Thats typically the closed-form youd leave it in, but we also want to show that
a
n
+ b
n
F
3n + 2
. Here
goes:
7
F
3n + 2
1
5
1+ 5
2
_
,
3n +2
1
5
1 5
2
_
,
3n + 2
1
5
1+ 5
2
_
,
2
1+ 5
2
_
,
3n
1
5
1 5
2
_
,
2
1 5
2
_
,
3n
1
5
6 + 2 5
4
1+ 5
2
_
,
3
_
,
n
1
5
6 2 5
4
1 5
2
_
,
3
_
,
n
6 + 2 5
4 5
2 + 5 ( )
n
6 2 5
4 5
2 + 5 ( )
n
5
5
6 + 2 5
4 5
2 + 5
( )
n
5
5
6 2 5
4 5
2 + 5
( )
n
10 +6 5
20
2 + 5
( )
n
+
10 6 5
20
2 + 5
( )
n
5 + 3 5
10
2 + 5
( )
n
+
5 3 5
20
2 + 5
( )
n
Therefore,
a
n
+ b
n
F
3n + 2
, and we rejoice.
Problem: What about the Circular Tower of Hanoi recurrence? Why cant we solve
that one as easily?
Well, you probably already noticed that both the pillar and the DNA recurrences
didnt have any inhomogeneous terms anywhere. Thats not the case with the
Circular Tower of Hanoi recurrence, so while we are certainly invited to eliminate one of the variables
from the set of recurrences, theres not much hope for solving it like we did for Martian DNA.
Q
n
0;
2R
n 1
+1
if n 0
if n > 0
'
R
n
0;
Q
n
+Q
n1
+ 1
if n 0
if n > 0
'
Clearly, its a piece of cake to define
Q
n
in terms of previous Qs, but these 1s just arent going to go away.
Q
n
2R
n 1
+1
2 Q
n1
+ Q
n 2
+ 1 ( ) + 1
2Q
n 1
+ 2Q
n 2
+ 3
so that
Q
n
0
1
2Q
n
+ 2Q
n1
+3
if n 0
if n 1
if n >1
'
Were sorta bumming because of that inhomogeneous 3.
There is a trick to solving this one, but its not something Im formally going to require you to know, since
theres little pedagogical value in memorizing tricks. For those of you with nothing to do on a Friday
8
night, try substituting
Q
n
A
n1
1 into the above system (making sure to adjust those base cases as
well.) Solve for
A
n
, then take whatever you got there and subtract 1 from it to get
Q
n
.
General Approach to Solving all Linear First-Order Recurrences
There is a general technique that can reduce virtually any recurrence of the form
a
n
T
n
b
n
T
n 1
+ c
n
to a sum. The idea is to multiply both sides by a summation factor,
s
n
, to arrive at
s
n
a
n
T
n
s
n
b
n
T
n 1
+s
n
c
n
. This factor
s
n
is chosen to make
s
n
b
n
s
n 1
a
n1
. Then if we write
S
n
s
n
a
n
T
n
we have a sum-recurrence for
S
n
as:
S
n
S
n1
+ s
n
c
n
Hence
S
n
s
0
a
0
T
0
+ s
k
c
k
1 kn
s
1
b
1
T
0
+ s
k
c
k
1 kn
1
s
n
a
n
s
1
b
1
T
0
+ s
k
c
k
1k n
_
,
.
So, you ask: How can we be clever enough to find the perfect
s
n
? Well, the relation
s
n
s
n 1
a
n1
b
n
can
be unfolded by repeated substitution for the
s
i
to tell us that the fraction:
s
n
a
n 1
a
n 2
a
n 3
Ka
1
b
n
b
n 1
b
n 2
Kb
2
(or any convenient constant multiple of this value) will be a suitable summation factor.
Problem: Solve
V
n
5 n =0
nV
n 1
+ 3 n! n 1
'
by finding the appropriate summation
factor.
Derivation of the solution just follows protocol. You're specifically told to use
summation factors here, so we should do that. Notice here that
a
n
1 and
b
n
n, so that the summation
factor should be chosen as:
s
n
1
n 1
n!
1
n!
V
0
5 for sure, but the recurrence formula
V
n
nV
n 1
+ 3 n! becomes
1
n!
V
n
1
n 1 ( )!
V
n1
+ 3. Let
T
n
1
n!
V
n
for the time being, just so we can solve an easier recurrence relation. We tackle
T
n
T
n 1
+ 3,
and wouldn't you know it:
T
n
3n + T
0
3n +
1
0!
V
0
3n + 5 .
T
n
1
n!
V
n
. We need
V
n
n!T
n
, so therefore
we have that
V
n
n! 3n + 5 ( ) . Woo!
9
Problem: Solve
C
n
0 n =0
n +1 +
2
n
C
k
0kn 1
n 1
'
by finding the appropriate
summation factor.
Lets write out the recurrences for
C
n
and
C
n1
a little more explicitly.
C
n
n +1 +
2
n
C
k
0kn 1
C
n1
n +
2
n 1
C
k
0kn 2
Subtracting the second one from the first one is a good idea, but only after the multiply through by a factor
thatll make each of the summations equal to each other:
C
n
n +1 +
2
n
C
k
0kn 1
n 1
n
C
n1
n 1
n
n +
n 1
n
2
n 1
C
k
0kn 2
n 1 +
2
n
C
k
0kn 2
n 1
n
C
n 1
n +1 +
2
n
C
k
0 k n1
_
,
n 1+
2
n
C
k
0 kn 2
_
,
2 +
2
n
C
n1
C
n
n + 1
n
C
n1
+ 2
nC
n
n +1 ( )C
n 1
+2n
If we choose a summation factor of
s
n
a
n 1
a
n 2
a
n 3
Ka
1
b
n
b
n 1
b
n 2
Kb
2
n 1 ( ) n 2 ( ) n 3 ( )K1
n +1 ( )n n 1 ( )K3
2
n n + 1 ( )
and multiply
through, we arrive at:
2
n n +1 ( )
nC
n
2
n n +1 ( )
n +1 ( )C
n1
+
2
n n +1 ( )
2n
2
n +1 ( )
C
n
2
n
C
n 1
+
4
n +1 ( )
C
n
n +1
C
n1
n
+
2
n +1
10
If we let
n +1 ( )D
n
C
n
, then we finally arrive at an equation that looks reasonable:
D
n
D
n1
+
2
n +1
.
Repeated substitution yields the following:
D
n
D
n1
+
2
n +1
D
n2
+
2
n
_
,
+
2
n +1
D
n3
+
2
n 1
_
,
+
2
n
+
2
n + 1
D
n4
+
2
n 2
_
,
+
2
n 1
+
2
n
+
2
n +1
M
D
0
+2
1
k +1
1kn
2 H
n+1
1 ( )
2 H
n
+
1
n +1
1
_
,
2 H
n
n
n +1
_
,
Recall that
C
n
n +1 ( )D
n
, so that
C
n
2 n + 1 ( ) H
n
n
n +1
_
,
2 n +1 ( )H
n
2n.
11