Title: 9-AdvancedCounting -1
1Recurrence Relations (RRs)
- A Recurrence Relation for a sequence an is an
equation that expresses an in terms of one or
more of the previous terms in the sequence (i.e.,
a0,a1,a2,,an-1) for all nn0. -
- Examples
- a01, a13, a24for n 3, an an-1an-2-an-3
- 1,3,4,6,7,9,10,12,13,15,16,18,19,21,22,
- a02, a14, a23for n 3, an an-1an-2-an-3
- 2,4,3,5,4,6,5,7,6,8,7,9,8,10,9,11,
- a04, a13, a23for n 3, an an-1an-2-an-3
- 4,3,3,2,2,1,1,0,0,-1,-1,-2,-2,-3,-3,-4,-4,
2Solutions to Recurrence Relations
- A sequence an is called a solution of the
recurrence relation if its terms satisfy the
recurrence relation. - Examples
- For n 3, an an-1an-2-an-3 Initial conditions
a01, a13, a24.Solution 1,3,4,6,7,9,10,12,13,1
5,16,18,19,21,22, - For n 3, an an-1an-2-an-3 Initial conditions
a02, a14, a23. Solution 2,4,3,5,4,6,5,7,6,8,
7,9,8,10,9,11, - For n 3, an an-1an-2-an-3 Initial conditions
a04, a13, a23. Solution 4,3,3,2,2,1,1,0,0,-1,
-1,-2,-2,-3,-3,-4,-4, - Every recurrence relationship has many solutions
each determined uniquely by its own initial
conditions. - We say a function fN?R is a solution to a
recurrence relation if the sequence f(n) is a
solution to it.Example f(n)5 2n is a solution
to the recurrence relation an2an-1.
3Solving Recurrence Relations
- If an4an-1 and a03, find a function f such that
f(n)an. - an4an-142an-243an-3...4na03?4n.
- If anan-1n and a04, find a function f such
that f(n)an. - anan-1nan-2n(n-1) an-2n(n-1)(n-2)...a0
n(n-1)(n-2)...14n(n1)/2(n2n8)/2. - If an2nan-1 and a05, find a function f such
that f(n)an. - an2nan-122n(n-1)an-223n(n-1)an-3...2nn!a05?
2nn!. - If an2an-11 and a00, find a function f such
that f(n)an. - an2an-1122an-22123an-342124an-48421
...2n-12n-22n-3...84212n-1.
4Modeling with Recurrence Relations
- An initial deposit of P0 dollars deposited at 7
annual interest. - Pn(10.07)Pn-1 is the value after n years.
Pn(10.07)nP0 - Rabbits on an island. Each pair producing a new
pair every month. - Fibonacci Numbers f00, f11 for n 2, fn
fn-1fn-2. - Tower of Hanoi
- Disks of decreasing diameter on 1 of 3 pegs.
- Move disks to another peg, always maintaining
decreasing disk diameters on each peg. - Hn number of moves to transfer n disks from 1
peg to another. - H11 for n 2, Hn Hn-11Hn-1 2Hn-11.
1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,163
83,32767,65535, - Hn 2n-1.
5RRs for Counting Bit Strings
- How many bit strings of length n do not contain 2
consecutive 0s? b12 (0,1), b23
(01,10,11) - For n 2 01counted or 1counted bn
bn-2 bn-1 . - Recognize this?
-
- How many bit strings of length n contain 2
consecutive 0s? b0b10 - For n 2 00any or 01counted or
1counted bn 2n-2 bn-2
bn-1 . - How many bit strings of length n contain 3
consecutive 0s? b0b1b20 - For n 3 000any or 001counted or
01counted or 1counted bn 2n-3
bn-3 bn-2
bn-1 .
6Counting Code Words
- How many strings of n digits have an even number
of 0s? - a19
- For n 2
- Either the first digit isnt 0 and the rest has
an even number of 0s (there are 9an-1 of these) - or the first digit is 0 and the rest does not
have an even number of digits (there are
10n-1-an-1 of these) - an 9an-1(10n-1-an-1)8an-110n-1
7Catalan Numbers
- Cn number of ways to parenthesize the product
of n1 numbers. C11 (1x2)
C22 (1x(2x3)), ((1x2)x3) - C35 (1x(2x(3x4))), (1x((2x3)x4)),
((1x2)x(3x4)), ((1x(2x3))x4), (((1x2)x3)x4) - C0C11
- CnC0Cn-1C1Cn-2C2Cn-3Cn-3C2Cn-2C1Cn-1C
0
(Covered in Sec.7.4, Ex.41)
Cn Number of extended binary trees with n
internal nodes.
2
5
14
8Solving Recurrence Relations
- Weve seen several examples of Recurrence
Relations - an aan-1 bn nbn-1
fn fn-2 fn-1 - bn2n-2bn-2bn-1 bn2n-3bn-3bn-2bn-1
. - bn bn-1bn-2-bn-3 Hn2Hn-11
- In each case, many sequences satisfy the
relationship and one also needs to set/have
initial conditions get a unique solution. - In some cases, we also have nice (closed form)
function, f, for the sequence, giving the nth
term in a formula that doesnt depend on earlier
ones, making it so that the sequence is just
f(n). - (For example, anan and Hn2n-1.)
- We look at some classes of recurrence relations
where we can do this.
9Recurrence Relations
- When c1, c2, , ck are constants and ck?0, an
c1an-1c2an-2ckan-k F(n) - is said to be a linear recurrence relation of
degree k with constant coefficients . - Linear each aj appears only to the 1st
power, no aj2 , aj3, . . . - degree k k previous terms, ck?0.
- constant coefficients cj are constants,
not functions cj(n). - Additionally, if F(n)0, the relation is called
homogeneous. - Homogeneous linear recurrence relation of degree
k with constant coefficientsan
c1an-1c2an-2ckan-k where ck?0 - We like these because we can solve them
explicitly. ?
10Examples of Recurrence Relations
- HLRRwCC Homogeneous linear recurrence relation
with constant coefficients - an aan-1 HLRRwCC of
degree 1
- an (an-1)2 Not Linear
- an nan-1 Coefficients
are not constant - fn fn-2 fn-1 HLRRwCC
of degree 2 - bn2n-2bn-2bn-1 . Not Homogeneous
- bn2n-3bn-3bn-2bn-1 Not Homogeneous
- an an-1an-2-an-3 HLRRwCC of
degree 3 - Hn2Hn-11 Not Homogeneous
- Remember
- A recurrence relation has many solutions.
- Only when the initial conditions are specified is
the solution unique. - For degree k, you need k contiguous initial
conditions.
11Solving Linear (degree 1) HLRRwCC
- The relation is an can-1
- All solutions are of the form an dcn
The function is fd(n) dcn - With the initial condition a0 specified, the
unique solution is an a0cn - We saw this in computing compound interest
- An initial deposit of P0 dollars deposited at 7
annual interest.Pn(10.07)Pn-1 is the value
after n years. Pn(10.07)nP0
12Fibonacci Solved
- When we had the HLRRwCC of degree 1,
f(n)cf(n-1), we had such good luck with fd(n)
dcn , lets try F(n)rn on the Fibonacci numbers
where we have the HLRRwCC of degree 2
F(n)F(n-1)F(n-2). - If we have F(n)rn and F(n)F(n-1)F(n-2), then
r2r1.There are actually 2 solutions to this
r1(1v5)/2 and r2(1-v5)/2 - Letting Fi(n) ri n (for i1,2), we have
Fi(n-1)Fi(n-2) ri n-1ri n-2 ri n-2(ri 1)
ri n-2ri 2 ri n Fi(n)which is just what we
want except we also want F(0)0 and F(1)1. - Combining our 2 solutions (see lemma below) as
F(n)d1F1(n)d2F2(n), we get 0d1d2 and
1d1r1d2r2 ,and so 1d1(r1-r2 )d1(v5) - Giving the Fibonacci numbers as
13Solutions to HLRRwCC
- We like HLRRwCC because we can solve them
explicity.That is, we can find functions fN?R
so that thesequence f(n) solves/satisfies the
recurrence relation.Heres part of the reason
why. - Lemma If functions f and g are solutions to the
HLRRwCC an c1an-1c2an-2c3an-3ckan-k
then fg is also a solution as is df for any
constant d. - Definitions The characteristic equation of this
HLRRwCC is rk c1rk-1c2rk-2c3rk-3ck-1r
ck The solutions (roots) of this equation are
called the characteristic roots of the
recurrence relation
14(Fibonacci) Degree 2 HLRRwCC
- 1) Write out the characteristic equation. (For
Fibonacci r2r1) - 2) Find the characteristic roots (r1 and r2).
- 3) Any/Every function of the form f(n)d1r1 n
d2r2 n(d1 and d2 constants) satisfies/solves
the recurrence relation. - 4a) If the characteristic roots are distinct, we
can pick d1 and d2 to produce the required
initial values. - 4b) If there is only 1 characteristic root
(r1r2r1), then any/every function of the form
f(n)(d1d2n)r n (d1 and d2 constants)
satisfies/solves the recurrence relation. - Example
- a01, a14 for n 2, an 4an-1-4an-2
1,4,12,32,80,192,448, - Characteristic Equation r24r-4 ? r2-4r40
? r2 (twice) - f(n)(d1d2n)2 n 1d1 42d12d2 ?
d1d21 - f(n)(1n)2n and checking (just for fun)
f(6)(16)26764448
15All Solutions For Any HLRRwCC
- Thrm If rk c1rk-1c2rk-2ck-1rck (ck?0) has
k distinct roots, r1,r2,,rk, - then every solution of the recursion
relation - anc1an-1c2an-2ckan-k has the
form - an d1r1nd2r2ndkrkn for some
d1, d2, , dk - and every such sequence solves/satisfies
the recursion relation. - If there are t distinct roots, each with
multiplicity mi, - the sequences an solving the recursion
relation are given by
16Degree 3 HLRRwCC Examples
- a02, a15, a215 for n 3, an 6an-1-11an-2-
6an-3 - r3 6r2-11r-6 ? r3-6r211r60 ?
(r-1)(r-2)(r-3)0 - General solution an x1ny2nz3n .
- Initial values 2xyz 5x2y3z 15x4y9z
? x1 y-1 z2 - Specific solution an 1-2n23n .
- a01, a1-2, a2-1 for n 3, an
-3an-1-3an-2-an-3 - r3 -3r2-3r-1 ? r33r23r10 ? (r1)30
? r-1 with multiplicity 3 - General solution an(xynzn2)(-1)n .
- Initial values 1x 2xyz -1x2y4z ? x1
y3 z-2 - Specific solution an (13n-2n2) (-1)n.
17Inhomogeneous Recurrence Relations
- Thrm If fN?R is any solution to the recurrence
relation an c1an-1c2an-2ckan-kF(n)
(1)and gN?R is a solution to the
corresponding homogeneous RR an
c1an-1c2an-2ckan-k (2) - then f-g his also a solution to (1).
- Moreover, if hN?R is a solution to (1)
then, f-h is a solution to (2). - Thrm With (1) and (2) as above, if F(n) has the
form F(n)(btntbt-1nt-1 . . .b1nb0)
snthen there is a particular solution to (1) of
the form nm(ptntpt-1nt-1 . . .p1np0)
snwhere m is the multiplicity of s as a
characteristic root of (2)and m0 if s is not
such a root.
18Inhomogeneous RR Example
- To Solve an 3an-12n a13
- an 3an-1 has as its general solution an c3n
- F(n)2n.
- We take s1 and look for a solution of the form
f(n)bnd - an 3an-12n ? bnd3(b(n-1)d)2n
- ? 2n(b1)(2d-3b)0 ? Pick b-1, d3b/2-3/2
- f(n)-n-3/2 is one solution and/but its initial
value is -5/2 - (we get the sequence -5/2, -7/2, -9/2, -11/2, . .
.) - General solution an c3n-n-3/2
- Initial condition 3 a1 c3-1-3/2 3c-5/2 ?
c11/6 - Specific solution an -(2n3-113n-1)/2