8.recurrence Relations
8.recurrence Relations
Learning Objectives
Learn about recurrence relations
Learn the relationship between sequences and
recurrence relations
Explore how to solve recurrence relations by
iteration
Learn about linear homogeneous recurrence
relations and how to solve them
Become familiar with linear nonhomogeneous
recurrence relations
2
Useful Facts
Arithmetic series
n(n 1)
1 2 3 n
2
More generally, if an= an-1 +c, where c is a
constant, then
n(an a1 )
a1 a2 a3 an
2
3
Useful Facts
Geometric series
1 2 4 2 n 2 n 1 1
More generally, if an= can-1 ,where c≠1 is a
constant, then
cn 1
a1 a2 a3 an a1
c 1
If 0< c< 1, then the sum of the infinite geometric
series is
a1
i 1
ai
1 c
4
Sequences and Recurrence Relations
5
Sequences and Recurrence Relations
6
Sequences and Recurrence Relations
7
Sequences and Recurrence Relations
8
Sequences and Recurrence Relations
9
10
Linear Homogenous Recurrence Relations
11
Linear Homogenous Recurrence Relations
12
Linear Homogenous Recurrence Relations
13
Linear Homogenous Recurrence Relations
14
Linear Homogenous Recurrence Relations
find the solution for T(n) = 2T(n/2) + 5n2 Assume T(1) = 7, n = 2k
15
Linear Homogenous Recurrence Relations
k1
Let 1
i 0
/ 2 i
2 1 / 2 k1
= 7n + 5n2(2 – 2/n)
= 7n + 10n2 – 10n
= 10n2 – 3n
16
Solving Recurrence Relations
Basically, when solving such recurrence relations, we try to
find solutions of the form an = rn, where r is a constant.
an = rn is a solution of the recurrence relation
an = c1an-1 + c2an-2 + … + ckan-k if and only if
rn = c1rn-1 + c2rn-2 + … + ckrn-k.
Divide this equation by rn-k and subtract the right-hand side
from the left:
rk - c1rk-1 - c2rk-2 - … - ck-1r - ck = 0
This is called the characteristic equation of the
recurrence relation.
17
Solving Recurrence Relations
The solutions of this equation are called the characteristic roots of the
recurrence relation.
Let us consider linear homogeneous recurrence relations of
Theorem: Let c1 and c2 be real numbers. Suppose that
. 18
Solving Recurrence Relations
a0 = 1 = 1
a1 = 6 = 13 + 23
Solving these equations yields 1 = 1 and 2 = 1.
Consequently, the overall solution is given by
an = 3n + n3n. 20
Example solution
an = an-1 + 2an-2, a0=2 and a1=7
Characteristic equation
r2 – r – 2 = 0
The quadratic formula for ax2+bx+c = 0,
b b2 4ac
x
2a
gives roots r1=(1+(-3))/2=-1 and r2=(1-(-3))/2 = 2
21
Example solution
Therefore,
a1 a0 r1 7 2 ( 1) 9
b2 3
r2 r1 2 ( 1) 3
a0 r2 a1 2 2 7 3
b1 1
r2 r1 2 ( 1) 3
22
Example 5
Find a closed form solution for the following recurrence relations. For brevity, we
omit the inductive proofs.
a) Recurrence relation: a0 = 2
an = 3an-1
Solution:
an = 3an-1
an = 3 (3an-2) = 32 an-2
an = 32 (3an-3) = 33 an-3
Set i = n, yielding:
an = 3 n a 0
an = 2 * 3 n
23
b) Recurrence relation: a0 = 1
an = 2an-1 – 1
Solution:
an = 2(2an-2 – 1) – 1 = 22 an-2 – 2 – 1
an = 22 (2an-3 – 1) – 2 – 1 = 23 an-3 – 22 – 2 – 1
Set i = n, yielding:
an = 2n (a0) – 2n-1 – 2n-2 – … – 20 = 2n – 2n-1 – 2n-2 – … – 20
an = 2n – (2n – 1) (Note: 2n-1 – 2n-2 – … – 1 = 2n – 1)
an = 1
c) Recurrence relation: a0 = 5
an = nan-1
Solution:
an = n(n-1)an-2
an = n(n-1)(n-2)an-3
an = n(n-1)(n-2)…(n-i+1)an-I
Set i = n, yielding:
an = n(n-1)(n-2)…(1) a0 = a0 n!
an = 5n! 24
A recurrence relation expresses the running time of a recursive
algorithm. This includes how many recursive calls are generated at
each level of the recursion, how much of the problem is solved by each
recursive call, and how much work is done at each level.
25
Example 6
Consider the following function to compute factorials:
int factorial(int n)
{
1) if (n <= 1)
2) return(1);
else
3) return(n * factorial(n-));
}
Recurrence relation:
T(1) = a
T(2) = b + T(1) = b + a
T(3) = b + T(2) = b + (b + a) = a + 2b
T(4) = b + T(3) = b + (a + 2b) = a + 3b
T(n) = a + (n-1)b for all n >= 1
26