Algorithm Design and Analysis Using Recurrence Relations
1. Department of Information Technology
Subject: . Design and Analysis of Algorithm
Topic:- Recurrence Relations
TAE:2
Submitted by:
Soumya Srinivas-03
Pratiksha Mohod-13
Samiksha Khanorkar-18
Guided by:- Prof. Kapil K. Wankhade
2. Recurrence Relations
A recurrence relation for a sequence { 𝑎 𝑛 } is an
equation that expresses 𝑎 𝑛 in terms of one or more of
the previous terms in the sequence,
𝑎0, 𝑎1, . . . , 𝑎 𝑛+1
For all integers n ≥ 𝑎0 where 𝑎0 is a nonnegative
integer.
A sequence is called a solution of a recurrence relation
if its terms satisfy the recurrence relation.
3. • Consider the example of Fibonacci numbers
F(n) = F(n − 1) + F(n − 2)
F(1) = 1
F(0) = 1
Recurrence Relations
In the Fibonacci numbers we needed two initial conditions,
F(0) =1 and F(1) = 1 since F(n) was defined by the two previous
terms in the sequence.
• Generally, recurrences can have the form
T(n) = aT ( n/b) + f(n)
4. Recurrence relations have two parts: recursive terms and non-
recursive terms.
Recurrence Relations
T(n) = 2T(n − 2) + 𝒏 𝟐 − 10
Recursive Non-Recursive
Recursive terms come from when an algorithm calls itself.
Non-recursive terms correspond to the “non-recursive” cost of
the algorithm work the algorithm performs within a function.
5.
6. Solving Recurrence Relations
• Homogeneous Mode
Consider the recurrence relation
T(n) + aT(n − 1) + bT(n − 2) = 0
The idea for solving this relation is to “guess” a solution of the
form T(n) = x n
Since T(n − 1) = 𝑥 𝑛−1 and T(n − 2) = 𝑥 𝑛−2 we get the equation
x n + axn−1 + bxn−2 = 0
Since x is clearly not zero, we can divide by 𝑥 𝑛−2
to get
𝑥2 + ax + b = 0
7. If this equation factors as (x−𝑟1) (x−𝑟2) = 0
with 𝑟1≠ 𝑟2 (so that the characteristic equation has two
distinct roots), then the general solution is
T(n) = 𝒄 𝟏. 𝒓 𝟏
𝒏+ 𝒄 𝟐. 𝒓 𝟐
𝒏
Since x is clearly not zero, we can divide by 𝑥 𝑛−2 to get
𝑥2 + ax + b = 0
Solving Recurrence Relations
8. Solving Recurrence Relations
Example . Solve the recurrence relation
T(n) − 4T(n − 1) + 3T(n − 2) = 0
T(0) = 0
T(1) = 2
Solution: The characteristic equation is
𝑥2 − 4x + 3 = 0,
or
(x − 3)(x − 1) = 0,
so the general solution is,
T(n) = 𝒄 𝟏 3 𝑛 + 𝒄 𝟐 1 𝑛
T(n) = 𝒄 𝟏 3 𝑛 + 𝒄 𝟐.
9. To find 𝒄 𝟏 and 𝒄 𝟐 we plug in the initial conditions
to get two equations in those two variables:
0 = T(0) = 𝒄 𝟏 30
+ 𝒄 𝟐
T(0) =𝒄 𝟏+ 𝒄 𝟐
2 = T(1) = 𝒄 𝟏 31 +𝒄 𝟐
T(1) = 3𝒄 𝟏 + 𝒄 𝟐
It’s easy to solve these equations for the solution 𝒄 𝟏 = 1, 𝒄 𝟐 = −1,
so the final answer is
Solving Recurrence Relations
T(n) = 𝟑 𝒏 − 1
10. Solving Recurrence Relations
• Non-Homogeneous Mode
Now consider what happens when the right side of equation
is not zero.
We get an equation of the form
T(n) + aT(n − 1) + bT(n − 2) = f(n)
We’ll learn how to solve in the special case that
f(n) = 𝑟 𝑛
Actually, this requires only a slight modification of the
method for the homogeneous case. This method is best
illustrated by the given example.
11. Solving Recurrence Relations
Example . Solve the recurrence relation
T(n) = 1 if n = 0
T(n) =3T(n − 1) + 2 𝑛 when n > 0
Solution: If the nonhomogeneous term 2 𝑛
were not
present, the characteristic equation would
be simply
x − 3 = 0.
But here,
We compare f(n) with 𝑏 𝑛.P(n)
i.e. :- f(n)= 𝑏 𝑛.P(n)
12. Solving Recurrence Relations
In the presence of the nonhomogeneous term, however,
we must multiply this by (𝑥 − 2)0+1
So, the characteristic equation is actually
(x − 3)(x − 2) = 0,
so the general solution is
T(n) = 𝒄 𝟏. 3 𝑛 + 𝒄 𝟐. 2 𝑛
we’ll need two equations to find the constants 𝒄 𝟏 and
𝒄 𝟐.
13. We can get a second value to use by simply applying the
recurrence formula
for n = 1
T(1) = 3T(0) +21
T(1) = 3(1) + 2 = 5
We now proceed as usual
1 = T(0) = c13 0 + c22 0 = c1 + c2
5 = T(1) = c13 1 + c22 1 = 3c1 + 2c2
These equations have solution c1 = 3 and c2 = −2, so the
exact solution is
Solving Recurrence Relations
T(n) = (3) 𝟑 𝒏 − (2) 𝟐 𝒏 = 𝟑 𝒏+𝟏 − 𝟐 𝒏+𝟏