Você está na página 1de 13

Chapter 14

Recursive Methods
14.1

Using Recursion

Some problems in combinatorics and probability can be solved using recursive methods. Here
is the basic idea: Suppose we are interested in computing a sequence an , for n = 0, 1, 2, ... . The
value an could be the number of elements in a set or the probability of a certain event. We may
be able to find a recursive relation that relates an to other ai s where i < n. For example,
suppose that for a certain problem, we can show that
an = an1 + 2an3 , for n = 3, 4, 5, ...
Now, if we know a0 , a1 , and a2 , we can use the above recursive equation to find an for all n.
Lets look at an example.
Example 1. Find the total number of sequences of length n (using H and T ) such that no two
Hs are next to each other. For example, for n = 2, we have 3 possible sequences: HT, T H, T T .
Let an be the number of such sequences. Lets call these sequences NO-HH sequences. We
have a1 = 2, a2 = 3. But how do we find, say a1000 ? To do this, we will show that
an = an1 + an2 for n = 3, 4, 5, ...

(14.1)

To show this, consider a NO-HH sequence of length n. This sequence either starts with a T or
an H.
- If it starts with a T , then the rest of the sequence is simply a NO-HH sequence of length
n 1. Conversely, by adding a T in front of any NO-HH sequence of length n 1, we can
obtain a NO-HH sequence of length n.
- If it starts with an H, then the second element in the sequence must be a T . In this
case, the rest of the sequence is simply a NO-HH sequence of length n 2. Conversely,
by adding HT in front of any NO-HH sequence of length n 2, we can obtain a NO-HH
sequence of length n.
1

CHAPTER 14. RECURSIVE METHODS

Thus, we conclude that an = an1 + an2 . Since we already know that a1 = 2 and a2 = 3, we
can use this recursive equation to find
a3 = 5, a4 = 8, a5 = 13, ...
Using a computer program we can compute an for the larger values of n. However, there is
also a straight-forward method to solving Equation 14.1 in order to obtain a simple formula for
an that does not involve previous values in the sequence. We will discuss how to solve these
equations in general shortly. Here, we solve Equation 14.1 to find a formula for the number of
No-HH sequences of length n. The trick, as we will see, is to let ak = xk and find non-zero
values of x that satisfy the recursive equation. In particular, letting ak = xk in Equation 14.1,
we obtain
x2 = x + 1,
which gives

1+ 5
1 5
x1 =
, x2 =
.
2
2
Then the general solution can be written in the form of
an = 1 xn1 + 2 xn2 ,
where 1 and 2 are constants to be determined from the known values of an . For example,
here we know a1 = 2, a2 = 3. Using these two values we can find 1 and 2 . It is a little bit
easier to use a0 and a1 . That is, since a2 = a1 + a0 , we obtain a0 = 1. Thus we have
!0
!0
1+ 5
1 5
a0 = 1 = 1
+ 2
2
2
!
!1
1
1 5
1+ 5
+ 2
.
a1 = 2 = 1
2
2
Thus, we obtain
(

1 + 2 = 1

1 ( 1+2 5 ) + 2 ( 12 5 ) = 2

5
53 5
By solving these equations, we obtain 1 = 5+3
10 , and 2 =
10 . Finally,




5+3 5 1+ 5 n 53 5 1 5 n
+
an =
10
2
10
2

(14.2)

This might seem somewhat strange as it does not look like an integer. However, you can evaluate
the above expression for small values of n to see that, in fact, square roots always cancel out and
the resulting values of an are always integers. If the above calculation seems confusing, dont
worry. We will now discuss in general how to solve recursive equations such as the one given in
Equation 14.1.

14.1. USING RECURSION

14.1.1

Solving Linear Homogeneous Recurrence Equations with Constant


Coefficients

Suppose that we have the following recursive equation:


an + c1 an1 + c2 an2 + c3 an3 + ... + cd and = 0

(14.3)

where the ci s are known constants. Also suppose that we already know the values of ai for
d different values of i. For example, we might know the values of a1 , a2 , ..., ad . To solve this
recursive equation, we first solve the following characteristic equation
xd + c1 xd1 + c2 xd2 + c3 xn3 + ... + cd = 0

(14.4)

This equation is obtained by replacing ai by xi in the recursive Equation 14.3. Let x1 , x2 , ..., xd
be d distinct roots of the characteristic polynomial (we will discuss the case of repeated roots
shortly). Then the general format for solutions to the recursive Equation 14.3 is given by
an = 1 xn1 + 2 xn2 + 3 xn3 + ... + d xnd

(14.5)

The values of 1 , 2 , ..., d can be obtained from the known values of ai . If a root is repeated r
times, we need to include r terms for that root, each scaled by a power of n. For example, if x1
is a repeated root of multiplicity r, then we write
an = 11 xn1 + 12 nxn1 + 13 n2 xn1 + ... + 1r nr1 xn1 + 2 xn2 + 3 xn3 + ... + d xnd

(14.6)

To better understand all this, lets look at some examples.


Example 2. Solve the following recurrence equations:
(a) an = 3an1 2an2 , where a0 = 2, a1 = 3;
(b) an = 4an1 5an2 + 2an3 , where a0 = 0, a1 = 2, and a2 = 5.
Solution
(a) The characteristic polynomial for an = 3an1 2an2 is x2 3x + 2. It has roots x1 = 2
and x2 = 1. Thus, the general solution is of the form
an = 2n + .
Since a0 = 2, a1 = 3, we obtain = 1, = 1. Therefore, an is given by
an = 2n + 1, for n = 0, 1, 2, ...
(b) The characteristic polynomial for an = 4an1 5an2 + 2an3 is x3 4x2 + 5x 2. We can
factor this polynomial as
x3 4x2 + 5x 2 = (x 1)2 (x 2).
Thus we have two roots, x1 = 1 with multiplicity 2, and x2 = 2. The general formula for
xn can be written as
an = 1 + 2 n + 3 2n .
Using a0 = 0, a1 = 2, and a2 = 5, we obtain
an = 2n + n 1.

CHAPTER 14. RECURSIVE METHODS

Note that recurrences could be much more complicated than the form given in Equation 14.3,
and sometimes we may not be able to find simple closed form solutions for them. Nevertheless,
we can usually use computer programs to compute them for at least moderate values of n. In
general, if the recursion is not in the form of Equation 14.3, a good start would be to compute
an for small n and see if we can identify a pattern and guess a general formula for an . If we are
lucky, and we can guess a general formula, then we usually can prove it mathematically using
induction.

14.1.2

Using Recursion with Conditioning

As we have seen so far, conditioning is a powerful method for solving probability problems. In
some problems, after conditioning we get a recursive relation that can help us solve the problem.
As an easy example, lets start with a problem closely related to Example 1.
Example 3. I toss a fair coin n times and record the sequence of heads and tails. What is the
probability that I do not observe two consecutive heads in the sequence?
Solution: Let pn be the probability of not observing two consecutive heads in n coin tosses.
One way to solve this problem is to use our answer to Example 1. In that example, we found
the total number of sequences of length n with no HH to be




5+3 5 1+ 5 n 53 5 1 5 n
an =
+
.
10
2
10
2
Now, since the total number of sequences of length n is 2n , and all sequences are equally likely,
we obtain
an
pn = n =
2




5+3 5 1+ 5 n 53 5 1 5 n
=
+
.
(14.7)
10
4
10
4
Here we will solve this problem directly using conditioning. Let An be the event that we
observe no consecutive heads in n coin tosses, i.e., pn = P (An ). The idea is to condition on the
result of the first coin toss. There are two possibilities. Using the law of total probability and
by conditioning on the result of the first coin toss, we can write
pn = P (An ) = P (An |H)P (H) + P (An |T )P (T )
1
1
= P (An |H) + P (An |T )
(14.8)
2
2
Now, to find P (An |T ) note that if the first coin toss is a T , then in order to not observe an HH
in the entire sequence, we must not observe an HH in the remaining n 1 coin tosses. Thus,
we have
P (An |T ) = P (An1 ) = pn1 .
Similarly, if the first coin toss results in an H, the second one must be a T and we must not
observe an HH in the remaining n 2 coin tosses, thus we have
P (An |H) =

1
1
P (An2 ) = pn2 .
2
2

14.1. USING RECURSION

Plugging back into Equation 14.8 we obtain


1
1
pn = pn1 + pn2 .
2
4
Note that we also know that p1 = 1 and p2 = 34 . Using the recursion, we also obtain p0 = 1.
Thus we can solve this recursion. We obtain the following characteristic equation
1
1
x2 x = 0.
2
4
The characteristic equation has roots x1 =
by

1+ 5
4

and x2 =

1 5
4 ,

so the general solution is given




1+ 5 n
1 5 n
pn =
+
.
4
4


Using p0 = 1 and p1 = 1, we obtain






5+3 5 1+ 5 n 53 5 1 5 n
+
.
pn =
10
4
10
4
Which, as we expect, is the same as Equation 14.7.

Gamblers Ruin Problem:


Here we discuss a famous problem called the Gamblers Ruin. It refers to a simple gambling
game in which two gamblers play repeatedly until one of them runs out of money. This is also
an example of a random walk.
Example 4. Two gamblers, call them Gambler A and Gambler B, play repeatedly. In each
round, A wins 1 dollar with probability p or loses 1 dollar with probability q = 1 p (thus,
equivalently, in each round B wins 1 dollar with probability q = 1 p and loses 1 dollar with
probability p). We assume different rounds are independent. Suppose that initially A has i
dollars and B has N i dollars. The game ends when one of the gamblers runs out of money
(in which case the other gambler will have N dollars). Find pi , the probability that A wins the
game given that he has initially i dollars.
Solution: At first it might not be clear that this problem can be solved using recursive
methods. The main idea is very simple. Condition on the result of the first round. After the
first round, A will have either i 1 dollars (if he loses) or will have i + 1 dollars (if he wins).
This way we can relate pi to pi1 and pi+1 . In particular, applying the law of total probability,
we obtain
pi = P (A wins the game|A wins the first round)P (A wins the first round)+
P (A wins the game|A loses the first round)P (A loses the first round)
= pi+1 p + pi1 (1 p).

CHAPTER 14. RECURSIVE METHODS

Thus we obtain the recursive equation


pi = pi+1 p + pi1 (1 p).
We can rewrite the equation as
ppi+1 = pi (1 p)pi1 .
To solve this recursion we need to know the value of pi for two different values of i. We already
know that p0 = 0. If A starts with 0 dollars he is automatically the loser. Similarly, if B starts
with 0 dollars (i.e., A starts with N dollars), then A is automatically the winner. Thus, we have
pN = 1. The characteristic equation is
px2 x + (1 p) = 0.
q
Solving this equation, we obtain two roots, x1 = 1 and x2 = 1p
p = p . The roots are different if
p 6= q. Thus, we need to consider two cases: if p 6= q (i.e., when p 6= 21 ) we can write the general
solution as
 i
q
pi = +
.
p

Using p0 = 0 and pN = 1, we obtain


pi =

1 ( pq )i
1 ( pq )N

If p = q = 12 , the characteristic equation has a repeated root of x1 = 1, so the general solution


can be written as
pi = 0 + 0 i.
Using p0 = 0 and pN = 1, we obtain
pi =

i
.
N

To summarize, for i = 0, 1, 2, ..., N , we have

1( pq )i
if p 6=
1( pq )N
pi =
i if p = 1
N
2

1
2

Discussion: Using the above answer, we can draw some conclusions. The setup of this problem in some sense can be a model for someone who goes to the casino and gambles repeatedly.
We can look at this problem from two points of view. First, let us be somewhat optimistic
and assume that the casino games are fair. In that case, you can win or lose with probability
p = q = 12 each time. But the casino has usually much more money than an individual gambler,
that is i << N . This means that your chance of winning, Ni is very small. Thus, if you gamble

14.1. USING RECURSION

repeatedly you are most likely to lose all your money. What if you are very rich? Assume that
you have the same amount of money as the casino. Even in that case, you are in no luck. The
reason is that the games are usually unfair (casino has some advantage), so p < 21 . Now, if you
and the casino both have a large sum of money N2 then your chance of winning is
pi =

1 ( pq )i
1 ( pq )N
N

1 ( pq ) 2
1

since i =

( pq )N

N
2

( pq ) 2

( pq )N
1
0 as N becomes large.
=
q N
(p) 2

since N is large and q > p

Thus, even if you have the same amount of money as the casino, you will most likely lose all
your money if you gamble repeatedly.

14.1.3

Solved Problems

1. Solve the following recursive equations. That is, find a closed form formula for an .
(a) an = an1 + n, with a0 = 0.
(b) an = nan1 , with a0 = 1.
(c) an = 5an1 6an2 , with a0 = 3, a1 = 8.
(d) an = 3an1 4an3 , with a0 = 3, a1 = 5, a2 = 17.
(e) an = 2an1 2an2 , with a0 = a1 = 2.

Solution:
(a) Note that this equation is NOT of the form of Equation 14.3, so we cannot use
our general methodology to solve this problem. In these situations, it is always a
good idea to compute the first few terms in the sequence and try to guess a general
formula, and then prove it (possibly using mathematical induction). For this problem
we quickly observe that
an = 1 + 2 + 3 + + n.
This is the sum of the numbers from 1 to n, and it is given by an =

n(n+1)
.
2

To obtain

CHAPTER 14. RECURSIVE METHODS


this formula, you can write


1 + 2 + 3 + + n


+ n + (n 1) + + 2 + 1

an + an =

= (1 + n) + (2 + (n 1)) + + (n + 1)
= (n + 1)n.
(b) Again, this is not in the form of Equation 14.3. By writing the first few ai s we
observe that
an = 1 2 3 n.
Thus, we conclude that
an = n!
(c) This recurrence equation is in the form of Equation 14.3, so we can use our general
method. In particular, the characteristic polynomial is
x2 5x + 6.
This polynomial has two roots, x1 = 2 and x2 = 3, so the solution will be in the form
an = 2n + 3n .
Using a0 = 3 and a1 = 8, we obtain
an = 2n + 2 3n .
(d) This recurrence equation is in the form of Equation 14.3, so we can use our general
method. In particular, the characteristic polynomial is
x3 3x2 + 4 = (x 2)2 (x + 1).
Thus, the solution will be in the form
an = 1 2n + 2 n2n + 3 (1)n .
Using a0 = 3, a1 = 5, a2 = 17, we obtain
an = (2 + n)2n + (1)n .
(e) This recurrence equation is in the form of Equation 14.3, so we can use our general
method. In particular, the characteristic polynomial is
x2 2x + 2,

14.1. USING RECURSION

which has two complex roots, x1 = 1 + i, x2 = 1 i (where i =


solution will be in the form

1). Thus, the

an = (1 + i)n + (1 i)n .
Using a0 = a1 = 2, we obtain
an = (1 + i)n + (1 i)n .

2. Let an be the total number of sequences of length n (using H and T ) that do not include
three consecutive Hs. For example, we know that a1 = 2, a2 = 4 and a3 = 7. Find a
recurrence equation for an .
Solution: We can solve this problem with a similar approach to the method we used in
Example 1. In particular, we will show that
an = an1 + an2 + an3 .
Lets call these sequences NO-HHH sequences. Consider a NO-HHH sequence of length n.
This sequence either starts with an T or an H.
- If it starts with a T , then the rest of the sequence is simply a NO-HHH sequence of
length n 1. Conversely, by adding a T in front of any NO-HHH sequence of length
n 1, we can obtain a NO-HHH sequence of length n.
- If it starts with an H, then the second element in the sequence is either an H or a T :
If the second element is a T , then the rest of the sequence is simply a NO-HHH
sequence of length n 2. Conversely, by adding HT in front of any NO-HHH
sequence of length n 2, we can obtain a NO-HHH sequence of length n.
If the second element is also an H, then the third element must be a T , and thus
the rest of the sequence is a NO-HHH sequence of length n 3. Conversely, by
adding HHT in front of any NO-HHH sequence of length n 3, we will obtain
a NO-HHH sequence of length n.
Thus, we conclude that an = an1 + an2 + an3 .

3. Let k be a fixed given integer larger than 1. Let f (n, k) be the total number of sequences
of length n (using H and T ) that do not include k consecutive Hs. Find a recurrence
equation for f (n, k).

10

CHAPTER 14. RECURSIVE METHODS

Solution: Similar to Example 1 and Problem 2, we can argue that


f (n, k) = f (n 1, k) + f (n 2, k) + ... + f (n k, k), for n > k.
And f (n, k) = 2n for n = 1, 2, 3, ..., k 1, and f (k, k) = 2k 1. Using the above recursion
we can define f (0, k) = 1 so that the above recursion also holds for n = k.

4. Let f (n, k, l) be the number of binary sequences of length n with exactly k ones and at
least l consecutive zeros. Find a recursive equation along with initial values to compute
f (n, k, l). Assume that n k + l.
Solution: Let also g(n, k, l) be the number of binary sequences of length n with exactly k
ones and NO sequences of l consecutive zeros. We have
 
n
f (n, k, l) =
g(n, k, l).
k
We provide a recursive formula for f (n, k, l). First note that
f (n, k, l) = n l + 1, for k + l = n
f (n, 0, l) = 1, for l 1.
Now we prove for n 1, l 1, k 0, and k + l n we have
f (n, k, l) = f (n 1, k 1, l) + f (n 1, k, l) + g(n l 1, k 1, l).
Thus we have the following recursion for f (n, k, l):
f (n, k, l) = f (n 1, k 1, l) + f (n 1, k, l)+


nl1
f (n l 1, k 1, l).
k1
The above equation can be used to compute f (n, k, l) for moderate values of n, k, and l.
Proof: Consider a sequence of length n with exactly k ones and at least l consecutive
zeros. We consider two cases:
(a) The first n 1 bits (positions) include at least l consecutive zeros. In this case the
last bit is either 0 or a 1 which results in f (n 1, k 1, l) + f (n 1, k, l) distinct
sequences.
(b) The first n 1 bits do NOT include l consecutive zeros. In this case the last l bits
must be zeros and the (n l)th bit must be a one. Thus the first n l 1 bits must
have exactly k 1 ones and no consecutive l zeros. This results in g(n l 1, k 1, l)
distinct sequences.

14.1. USING RECURSION

11

5. I toss a biased coin n times and record the sequence of heads and tails. If P (H) = p
(where 0 < p < 1), what is the probability that I do not observe two consecutive heads in
the sequence?
Solution: Let An be the event that we observe no consecutive heads in n coin tosses, i.e.,
pn = P (An ). The idea is to condition on the result of the first coin toss. There are two
possibilities. Using the law of total probability and by conditioning on the result of the
first coin toss, we can write
pn = P (An ) = P (An |H)P (H) + P (An |T )P (T )
= p P (An |H) + (1 p) P (An |T )

(14.9)

Now, to find P (An |T ) note that if the first coin toss is a T , then in order to not observe
an HH in the entire sequence, we must not observe an HH in the remaining n 1 coin
tosses. Thus, we have
P (An |T ) = P (An1 ) = pn1 .
Similarly, if the first coin toss results in an H, the second one must be a T and we must
not observe an HH in the remaining n 2 coin tosses, thus we have
P (An |H) = (1 p) P (An2 ) = (1 p)pn2 .
Plugging back into Equation 14.9 we obtain
pn = (1 p)pn1 + p(1 p)pn2 .
Note that we also know that p1 = 1 and p2 = 1 p2 . Using the recursion, we also obtain
p0 = 1. Thus we can solve this recursion. We obtain the following characteristic equation:
x2 (1 p)x p(1 p) = 0.

1p+ (1p)(1+3p)
1p (1p)(1+3p)
The characteristic equation has roots x1 =
and
x
=
,
2
2
2
so the general solution is given by
p
p




1 p (1 p)(1 + 3p) n
1 p + (1 p)(1 + 3p) n
+
.
pn =
2
2
Using p0 = 1 and p1 = 1, we obtain
p
1 + p + (1 p)(1 + 3p)
p
=
2 (1 p)(1 + 3p)
p
(1 p)(1 + 3p) 1 p
p
=1=
.
2 (1 p)(1 + 3p)

12

CHAPTER 14. RECURSIVE METHODS


6. Solve the following recurrence equation, that is find a closed form formula for an .
an = an1 + ,

with a0 = 1,

where 6= 0 and are known constants.

Solution: This equation is NOT exactly of the form of Equation 14.3 (because of the
constant ), so we cannot directly use our general methodology to solve this problem.
However, by computing an for a few values of n we can identify the solution:
a1 = +
a2 = 2 + (1 + )
a3 = 3 + (1 + + 2 )
a3 = 4 + (1 + + 2 + 3 ).
In general, we obtain
an = n + (1 + + 2 + + n1 )


1 n
n
= +
.
1

7. This problem has applications in coding theory: I toss a biased coin n times and record
the sequence of heads and tails. If P (H) = p (where 0 < p < 1), what is the probability
that I observe an even number of Hs?

Solution: Let An be the event that I observe an even number of heads for n = 1, 2, .... Let
an = P (An ). Conditioning on the last coin toss, we can write for n 2:
an = P (An |H)P (H) + P (An |T )P (T )
= p P (An |H) + (1 p) P (An |T )
= p P (Acn1 ) + (1 p) P (An1 )
= p(1 an1 ) + (1 p)an1 .
Thus, we obtain the following recursive equation:
an = (1 2p)an1 + p,

with a1 = 1 p.

14.2. END OF CHAPTER PROBLEMS

13

From the recursion, we obtain a0 = 1, so we have the following equation:


an = (1 2p)an1 + p,

with a0 = 1.

This recursion is in the form given in problem 6 ( = 1 2p, = p), so we obtain




1 (1 2p)n
n
an = (1 2p) + p
2p
n
1 + (1 2p)
=
.
2

14.2

End of Chapter Problems

1. Solve the following recurrence equations, that is, find a closed form formula for an .
(a) an = 2an1 34 an2 , with a0 = 0, a1 = 1.
(b) an = 4an1 4an2 , with a0 = 2, a1 = 6.
2. I toss a biased coin n times. Let P (H) = p and let an,k be the probability that I observe
k heads.
(a) By conditioning, on the last coin toss, show that an+1,k+1 = p an,k + (1 p) an,k+1 .



n
n
(b) Using part (a), prove that for 0 k < n, we have n+1
k+1 = k+1 + k .
3. * You toss a biased coin repeatedly. If P (H) = p, what is the probability that two
consecutive Hs are observed before we observe two consecutive T s? For example, this
event happens if the observed sequence is T HT HHT HT T .
4. I toss a biased coin n times and record the sequence of heads and tails. Assume P (H) = p
(where 0 < p < 1). Let an be the probability that the number of heads is divisible by 3.
Write a set of recursive equations to compute an .

Você também pode gostar