Chapter 3
Chapter 3
][8 Marks]
3. Recurrence Relation
3.1.Recursive Definition Sequences
3.2.Solution of Linear recurrence relations
3.3.Solution of Nonlinear recurrence relations
3.4.Application to algorithm analysis
Recurrence Relations
A sequence can be defined by giving a general formula for its nth term by writing a few of its
terms. An alternative approach is to represent the sequence by finding a relationship among its
terms. Such relations are referred as recurrences.
Recursively defined sequences are often referred to as recurrence relations.
A recurrence relation for the sequence {an} is an equation that expresses a n in terms of one or
more of the previous terms of the sequence namely a0,a1,a2,……an-1 for all integers n with n≥n0
where n0 is a non-negative integer.
A sequence is called a solution of a recurrence relation if its term satisfy the recurrence relation.
Example
Consider the following instructions for generating a sequence
1) start with 3
2) Given any term, add 3 to get the next term.
The term of sequence obtained is
3,6,9,12,15,18,…..
If we denote the sequence as a1, a2,….
a1=3 ............. (1)
Instruction (2) can be rephrased as
an= an-1+3 n≥2 ........... (A)
By using a1 and an, we can compute any term in the sequence.
Equation (A) is an example of recurrence relation (1)
Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 80
Example
2) Let {an} be a sequence that satisfies the recurrence relation. an=an-1-an-2 for n=2,3,4 and
suppose that a0=3 and a1=5 what are a2 and a3?
Similarly a4, a5 and also each successive terms. The sequence forms in such a way is 3,5,2,-3,….
3) Determine whether the sequence {an} is a solution of the recurrence relation an=2an-1-an-2 for
n=2,3,4,… where an=3n for every non negative integer n. Answer the same question where
an=2n and where an=5.
4) A customer deposits 10,000 in a bank and he gets 11% interest per annual and the interest
amount is accumulated every year. Find the recurrence relation to compute total amount
customer receive after 10 year. And also calculate amount that customer receive after 30 years.
p0=10,000
Interest=11%
p1=p0+0.11 *p0
=1.11p0
=11100
p2=p1 *0.11+p1
Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 81
=1.11p0 * 0.11+p0 *1.11
=1.11p0(0.11+1)
=(1.11)p0 *1.11
=(1.11)2p0
p3=1.11p2=(1,11)3p0
pn=(1,11)pn-1=(1.11)np0
pn=(1.11)n 10,000.
Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 82
rn=c1rn-1+c2rn-2+……+ckan-k
Dividing both by rn-k
rk=c1rk-1+c2rk-2+…..+ck
rk-c1rk-1-c2rk-2-…..-ck=0
This is the characteristic equation of degree k of the recurrence relation.
Let us consider the liner homogeneous recurrence relations of degree 2.
i.e.
an=c1an-1+c2an-2
Its characteristic equation is of the form
r2-c1r-c2=0
Theorem:
Let c1 and c2 be real numbers suppose that r2-c1r-c2=0 has two distinct roots r1 and r2, then the
sequence {an} is a solution of the recurrence relation an=c1an-1+c2an-2 iff an= α1r1n+ α2r2n for
n=0,1,2,….. where α1 and α2 are constant. To find α1 and α2 initial condition is used.
Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 83
Dividing equation (i) by rn-2, we get
r2-r-2=0
r2-(2-1)r-2=0
r2-2r+r-2=0
r(r-2)+1(r-2)=0
(r-2)(r+1)=0
Its roots are r=2 and r=-1. Hence the sequence {an} is a solution to the recurrence relation iff
an= α1r1n+ α2r2 n
an= α12n+ α2(-1)n
For some constants α1 and α2. From initial conditions it follows that,
a0= α1 + α2 =2
a1= α12+ α2 (-1)
7= α12+ α2 (-1)
i.e. α1+ α2 =2 ........... (ii)
2 α1- α2 =7 ................(iii)
Solving these two equation
α1= 3 and α2 =1
Hence the solution to the recurrence relation and initial conditions is the sequence {a n} with
an=3.2n-(-1)n
Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 84
For some constants α1 and α2 , using a0=0 and a1=1
When n=0, a0=0
a0= α1 + α2
α1= -α2
When n=1,a1=1
a1= α1 ((1+√5)/2)1+ α2((1+√5)/2)1
1 = -α2 ((1+√5)/2)+ α2((1+√5)/2)
= α2 (-1/2-√5/2+1/2-√5/2)
= α2 (-2√5/2)
α2=-1//√5
α1=1//√5
Theorem :
Let c1 and c2 be real number with c2≠0. Suppose that r2-c1r-c2=0 has only one root r0. A sequence
{an} is a solution of recurrence relation a n=c1an-1+c2an-2 iff an= α1r0n+ α2nr0n, for n=0,1,2,….
Where α1 and α2 are constant.
Theorem :
Let c1, c2, …….. ck be real numbers. Suppose that the characteristic equation.
rk-c1 rk-1 …….-ck=0
has k distinct roots r1, r2,……rk then sequence {an} is a solution of recurrence relation.
an=c1(an-1)+c2an-2+……+ckan-k iff
an= α1r1n+ α2r2n+ ….. + αkrknfor n=0,1,2.... where α1, α2, α3,......... αk are constants.
Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 85
Theorem :
Let c1, c2,…....ck be real numbers, suppose that the characteristics equation
rk-c1rk-1 ….. –ck=0
has t distinct roots r1, r2...........rk with multiplication m1, m2, m3,…..mt respectively so that mi≥1
for 1,2,….t and m1+m2+… +mk=k. Then a sequence {an} is a solution of the recurrence relation
an=c1an-1+c2an-2+…...+ckan-k iff
an=( α1,0+ α1,1n+..........+ α1,m1-1nm1-1)r1n+
(α2,0+ α2,1n+..........+ α2,m2-1nm2-1)r2n+....
.....+ ( αt,0+ αt,1n+..........+ αt,mt-1nmt-1)rtn
Suppose that roots of the characteristics equation of a linear homogeneous recurrence relation are
2, 2, 2, 5, 5 and 9 (that is there are three roots, the root 2 with multiplicity two three, the root 5
with multiplicity two and the root 9 with multiplicity one.
What is the form of general solution?
(α1,0+ α1,1 n+ α1,2 n2)2n+( α2,0+ α2,1 n)5n+ α3,09n
1) an=6an-1-9an-2 with a0=1, a1=6
Find the solution of given recurrence relation.
Solution:
an=6an-1-9an-2
Let an=rn, we get
rn=6rn-1-9rn-2, we get,
r2=6r-9
r2-6r+9=0 ................(1)
Here a1=6
a1= α13+ α23
6=3+ α23
α2=1
Hence the solution is an = 3n+n3n
Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 86
Solution:
The characteristic equation is given by
r3-6r2+11r-6=0
Or, r3-r2-5r2+5r+6r-6=0
Or, r2(r-1)-5r(r-1)+6(r-1)=0
Or, (r-1)(r2-5r+6)=0
Or, (r-1)(r2-3r-2r+6)=0
Or, (r-1)[r(r-3)-2(r-3)]=0
Or, (r-1)(r-2)(r-3)=0
3) Find the solution to the recurrence relation an=-3an-1=3an-2-an-3 with initial conditions a0=1,
a1=-2 and a2=-1
The characteristic of this recurrence relation is
r3+3r2+3r+1=0
(r+1)3=0
r1=r2=r3=-1 i.e. there is a single root r=-1 of multiplicity three of characteristic equation.
The solution is given by,
an=( α1,0+ α1,1n+ α1,2n2)(-1)n
=( α1,0(-1)n + α1,1n(-1)n + α1,2n2 (-1)n
To find the constants α1,0, α1,1 and α1,2 use the initial condition. This gives
a0=1= α1,0....................................(1)
a1=-2= α1,0(-1)+ α1,1(-1)+ α1,2(-1)
a1=-2= -α1,0- α1,1- α1,2........................(2)
a2=-1=( α1,0(-1)2 + α1,12(-1)2 + α1,222 (-1)2
a2=-1= α1,0 + 2α1,1 + 4α1,2.........................(3)
Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 87
Solving (1),(2) and (3), we get,
α1,0=1, α1,1=3, α1,2=-2
Hence, the solution is given by,
an=1(-1)n+3n(-1)n+(-2)n2(-1)n
an=(1+3n-2n2)(-1)n
Solution:
Let hn be the number of moves required to transfer n disk. We verify that h0=0, h1=1 and h2=3.
We have to find a recurrence relation that is satisfied by h n.
Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 88
Since h0=0, it holds for n=0
Assume that it holds for n. we then show it holds with n replaced by n+1
i.e. hn+1=2hn+1
=2(2n-1)+1
=2n+1-1
Since it also holds for n+1, hence the formula is verified.
To solve the non-homogeneous recurrence relation with constant coefficient, we need to solve its
associated liner homogeneous equation and to find a particular solution for the given non-
homogeneous equation.
A recurrence relation of the form
an=c1an-1 +c2an-2+….+ckan-k+ F(n)
Where c1,c2, ….. ck are real numbers and F(n) is a function not identically zero depending only
on n is linear non-homogeneous recurrence relation. The recurrence relation
an=c1an-1+c2an-2+….. ckan-k
is called the associated homogeneous recurrence relation.
For example,
1) an=an-1+2n
2) an=an-1+an-2+n2+n+1
3) an=3an-1+n3n
4) an=an-1+an-2+an-3+n
These are examples of linear non-homogeneous recurrence relation with constant coefficients
corresponding associated linear homogeneous recurrence relation are
1) an=an-1
2) an=an-1+an-2
3) an=3an-1
4) an=an-1+an-2+an-3
Theorem :
If {an(p)} is a particular solution of the non-homogeneous liner recurrence relation with constant
coefficients.
an=c1an-1+c2an-2+…..+ckan-k+f(n)
Then every solution is of the form {a n(p)+an(h)} where an(h) is a solution of the associated
homogeneous recurrence relation.
an=c1an-1+c2an-2+…..+ckan-k
Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 89
Theorem:
Suppose {an} satisfies the linear recurrence relation.
an=c1an-1+c2an-2+….+ckan-k+f(n)
Where c1,c2,…..ck are real numbers and
f(n)=(btnt+bt-1nt-1+……+b1 n+b0)sn
Where b0, b1............bt and s are real numbers. When s is not a root of the characteristics equation
of the associated linear homogeneous recurrence relation there is a particular solution of the
form.
(ptnt+pt-1nt-1+…..+p1n+p0)sn
When s is a root of this characteristic equation and its multiplicity is m, there is a particular
solution is the form
nm(ptnt+pt-1nt-1+…..+p1n+p0)sn
1) what form does a particular solution of the linear non-homogeneous recurrence relation
an=6an-1-9an-2+f(n).
have when f(n)=3n
f(n)=n3n
f(n)=n22n
f(n)=(n2+1)3n
Solution,
The associated linear homogeneous recurrence relation is an=6an-1-9an-2. Its characteristics
equation, r2-6r+9=(r-3)2=0 has a single root 3, of multiplicity two.
For f(n)=3n
Because s=3 is a root with multiplicity 2, particular solution has the form p 0n23n if f(n)=3n
For f(n)=n3n
The form is n2(p1n+p0)3n if f(n)=n3n
2) Find all solutions of the recurrence relation an=3an-1+2n. what is the solution with a1=3 ?
Solution,
The associated linear homogeneous equation is an=3an-1
Its solution are an(h)= α3n
Where α is constant
Now, finding particular solution
f(n)=2n is a polynomial of degree one
Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 90
Comparing with f(n)= p(n) sn
we get, s=1
The root of the characteristic equation is 3
Here, s is not the root of characteristic equation of the associated linear homogeneous recurrence
relation, there is a particular solution of the form
(p0+p1n+…..+pt-1 nt-1+ ptnt)sn
=(p0+p1n)sn
=(p0+p1n)
New, equation an=3an-1 + 2n becomes
(p0+p1n)= 3(p0+p1(n-1))+2n
or, (p0+p1n)= 3p0+3p1n-3p1+2n
or, n(p1-3p1-2)= 2p0-3p1
or, n(-2p1-2)= 2p0-3p1
or, n(2p1+2)+ 2p0-3p1=0
Solution,
an=5an-1- 6an-2
Here,
f(n)=7n
s=7
Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 91
Here s is not the root of characteristic equation, there is particular solution in the form
p0=7n
p07n=5.p07n-1-6.p07n-2+7n
p0=49/20
an=an(p)+an(h)
=(49/20)* 7n+α13n+α22n
Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 92
Solving recurrence relation using substitution
Solve recurrence relation
an2-2a2n-1=1 for n≥1 where a0=1
This non-linear equation can be converted into linear equation by using substitution b n=an2
Now equation become bn-2bn-1=1 with b0=a02=12=1
Solving bn-2bn-1=1 with b0=1, the homogeneous solution is α.2n and particular solution is c.
So, bn=a2n+c where a and c are constants.
Using fact b0=1 and b1=3 we get a+c =1 and 2a+c=3
∴a=2 and b=-1.
Hence the solution is bn=2.2n-1
=2.2n-1
=2n+1-1
Substituting back an2=2n+1-1
an=√2𝑛+1 − 1
Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 93
Analysis of algorithm
Binary search:
Binary search looks for a value in a sorted sequence and returns the index of the value if it is
found or 0 if it is not found.
Input : A sequence si, si+1,…..,sj, i≥1 sorted in increasing order, a value key, i and j.
Output : The output is an index k for which sk=key or if key is not in the sequence the output is
the value 0.
Example
Input
s1=2, s2=4, s3=5, s4=6 and key 6
- i=1, j=4, k=index
- Since (i>j) (1>4) is false
- k=2
- Since key(6) is not equal to s4(4)
- Key (6)<sk (6<4) is false
- We set, i=k+1 =3
i=3, j=4
s3=5, s4=6
Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 94
i>j (3>4) is false
k=(3+4)/2=3
Thus, binary search reduces the search of an element in a search sequence of size n to the binary
search sequence of the element in a search sequence of size n/2 when n is even. Hence the
problem of size n has reduce to one problem of size n has reduced to one problem of size n/2.
Two comparison are needed to implement this reduction ( one to determine which half of the list
to use and other to determine whether any term of the list remain). Hence if f(n) is number of
comparison required to search for an element in a search sequence of size n, then
f(n)=f(n/2)+2
Selection sort
This algorithm sorts the sequence
s1,s2,….,sn
in increasing order by first selecting the largest item and placing it last and then recursively
sorting the remaining elements.
Input : s1, s2, ……, sn and length n of the sequence
Output ; s1, s2, ….., sn arranged in increasing order
Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 95
5) // more largest to end
Swap (sn, smax_index)
6) Call selection_sort (s, n-1)
7) End selection_sort
bn=n-1+bn-1
Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 96