Sei sulla pagina 1di 21

Advanced Algorithms Design and

Analysis
By

Nazir Ahmad Zafar
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Lecture No 4


Recurrence Relations

(Algorithms Design and Analysis Techniques)
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Solution: General Homogenous Recurrences when
Roots are distinct
Roots are repeated
multiplicity of a root is k
many roots with different multiplicities
Non-homogenous Recurrence Relations
Characteristics of various type of non-homogenous
recurrence relations
Solution to various non-homogenous recurrence
relations
Conclusion
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Today Covered
Solution to Generalized Recurrence
with Distinct Roots
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Statement:
Find the general solution of the k
th
order recurrence
given below assuming that all roots are distinct
a
0
t
n
+ a
1
t
n-1
+ ..+a
k
t
n-k
= 0
Solution: Let T
n
= x
n
, x is a constant as yet unknown.
If we assume that T
n
is a solution of equation
a
0
t
n
+ a
1
t
n-1
+ ..+a
k
t
n-k
= 0
Then, a
0
x
n
+ a
1
x
n-1
+.+ a
k
x
n-k
= 0
Equation satisfied if x = 0 ,trivial solution, no interest.
Otherwise, the equation is satisfied if and only if
a
0

+ a
1
x
1
+.+ a
k
x
k
= 0
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Solution to Gen. Recurrence with Distinct Roots
a
0

+ a
1
x
1
+.+ a
k
x
k
= 0

This equation of degree k in x is called the characteristic
equation of above recurrence and
P(x) = a
0

+ a
1
x
1
+.+ a
k
x
k
is called its characteristic polynomial
Fundamental theorem of algebra states that polynomial
P(x) of degree k has k roots, not necessarily distinct
It means that it can be factorized as a product of k terms
P(x) = (x-r
1
) (x-r
2
) (x-r
3
). . .(x-r
k
)
where r
i
may be complex numbers.
Moreover, these r
i
are only solutions of equation P(x) = 0
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Solution to Recurrence with Distinct Roots
Consider any root r
i
of the characteristic polynomial

P(x) = a
0

+ a
1
x
1
+.+ a
k
x
k


Since, p(r
1
) = 0, p(r
2
) = 0, p(r
3
) = 0, . . ., p(r
k
) = 0
Hence all x = r
i
, for i {1, 2, . . .,k}are solutions to
above characteristic polynomial.
Therefore, r
1
n
, r
2
n
, . . ., r
k
n
are solution to our original
recurrence relation.
Since linear combination of solutions is also a
solution to recurrence, therefore below is a solution.

=
=
k
i
i
r x
1
) (
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Solution to Recurrence with Distinct Roots

=
=
k
i
n
i i
r c n T
1
) (



where, c
1
, c
2
, . . ., c
k
are all constants to be
determined finding particular solution
The remarkable fact is that this equation has only
solutions of this form provided all r
i
are distinct.
Constants can be determined from k initial
conditions by solving system of k linear equations
in k unknowns
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Solution to Recurrence with Distinct Roots

=
=
k
i
n
i i
r c n T
1
) (
Problem:
Consider the recurrence
t
n
= n if n = 0, 1, 2
t
n
= 7.t
n-2
+ 6.t
n-3
otherwise
Find the general solution of the recurrence above.

Solution: First we rewrite the recurrence.
t
n
- 7.t
n-2
- 6t
n-3
= 0
The characteristic equation become.
x
3
7x - 6 = (x + 1) (x + 2)(x - 3)
The roots are: r
1
= -1, r
2
= - 2 and r
3
= + 3
t
n
= c
1
(-1)
n
+ c
2
(-2)
n
+ c
3
(3
n
)
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Solving Recurrence Problem with Distinct Roots
t
n
= c
1
(-1)
n
+ c
2
(-2)
n
+ c
3
(3
n
)

The initial conditions give
c
1
+ c
2
+ c
3
= 0 for n = 0
- c
1
- 2c
2
+ 3c
3
= 1 for n = 1
c
1
+ 4c
2
+ 9c
3
= 2 for n = 2

Solving these equations, we obtain

c
1
= -1/4, c
2
= 0 and c
3
= 1/4

Therefore, t
n
= c
1
(-1/4)(-1)
n
+ (1/4).(3)
n

Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Solving Recurrence Problem with Distinct Roots
Solution to Generalized Recurrence
with One Roots
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Statement: Consider the recurrence
t
n
= n if n = 0, 1, 2
t
n
= 5t
n-1
- 8t
n-2
+ 4t
n-3
otherwise
Find general solution of the recurrence above.
Solution: First we rewrite the recurrence.
t
n
- 5t
n-1
+ 8t
n-2
- 4t
n-3
= 0
The characteristic equation become.
x
3
5x
2
+ 8x -4 = (x-1) (x-2)
2

The roots are: r
1
= 1 of multiplicity m
1
= 1 and r
2
= 2
of multiplicity m
2
= 2, and hence the general
solution is
t
n
= c
1
1
n
+ c
2
2
n
+ c
3
n 2
n

Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Problem
The initial conditions give
c
1
+ c
2
= 0 for n = 0
c
1
+ 2c
2
+ 2c
3
= 1 for n = 1
c
1
+ 4c
2
+ 8c
3
= 2 for n = 2

Solving these equations, we obtain

c
1
= -2, c
2
= 2 and c
3
= -1/2

Therefore,
t
n
= c
1
1
n
+ c
2
2
n
+ c
3
n 2
n

= -2 + 2.2
n
.n.2
n
=2
n+1
n.2
n-1
- 2
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Example
Non-homogeneous Recurrence
of Higher Order
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Problem:
Consider the recurrence below. Find its solution
t
n
2t
n-1
= 3
n

Solution:
Compare the above recurrence with
a
0
t
n
+ a
1
t
n-1
+ . . .+ a
k
t
n-k
= b
n
p(n)
Here: b = 3, p(n) = 1, a polynomial of degree 0.
Reducing to homogeneous case, we are given
t
n
2t
n-1
= 3
n
(1)
Replace n by n - 1 and multiply by 3, we obtain
t
n-1
2t
n-2
= 3
n-1

3t
n-1
6t
n-2
= 3
n
(2)
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Problem 1: Non-homogeneous Recurrences
From (1) and (2)
t
n
2t
n-1
= 3
n
(1)

+ 3t
n-1
6t
n-2
= 3
n
(2)
Subtracting 2 from equation 1, we get
t
n
- 5t
n-1
+ 6t
n-2
= 0
The characteristic equation is
x
2
- 5x + 6 = 0
Roots of this equation are:
x = 2 and x = 3
And therefore general solution is
t
n
=c
1
2
n
+ c
2
3
n

Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Problem 1: Non-homogeneous Recurrences
Statement:
The Tower of Hanoi is a mathematical game or puzzle. It
consists of three pegs, and a number of disks of different
sizes which can slide onto any peg. The puzzle starts with
the disks neatly stacked in order of size on one peg, the
smallest at the top, thus making a conical shape.
The objective of the game is to move the entire stack to
another peg, obeying the following rules:
Only one disk may be moved at a time.
Each move consists of taking the upper disk from one of
the pegs and sliding it onto another peg, on top of the
other disks that may already be present on that peg.
No disk may be placed on top of a smaller disk.
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Tower of Hanoi: Problem 3
T(k) = Moves required to move k disks from tower 1 to 2 =
Moves required to move k-1 disks from tower 1 to 3 + time to
move k
th
disk from tower 1 to tower 2 + Moves required to
move k-1 disks from tower 3 to back to tower 2 =

T(k) = T(k-1) + 1 + T(k-1) = 2T(k-1) + 1
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Modeling Tower of Hanoi Problem 3
Tower 1 Tower 2 Tower 3
The number of movements of a ring required in the
towers of Hanoi problem is given by equation
t(m) = 0, 1 if m = 0, 1
t(m) = 2t(m-1) + 1 otherwise
This can be written as: t(m) 2t(m-1) = 1
which is of the form of non-homogenous equation:
a
0
t
n
+ a
1
t
n-1
+ . . .+ a
k
t
n-k
= b
n
p(n)

Here, b = 1 and p(n) = 1, a polynomial of degree 0.
Characteristic equation becomes (x - 2) (x - 1)
It has roots: (x - 2) (x - 1) = 0; x = 2, 1
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Solving Tower of Hanoi Problem 3
Where factor (x-2) comes from the left-hand side of
eq and the factor (x-1) from its right-hand side.
The roots of this polynomial are 1 and 2, both of
multiplicity 1, so all solutions of this recurrence are :

t(m) = c
1
1
m
+ c
2
2
m


For m = 0 and m = 1, c1 and c2 are calculated as
1 and -1 therefore
t(m) = 2
m
-1
For m = 64
t(64) = 2
64
-1
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis
Solving Tower of Hanoi Problem 3
Recursive relations are important because used in
divide and conquer, dynamic programming and in
many other e.g. optimization problems
Analysis of such algorithms is required to
compute complexity
Recursive algorithms may not follow homogenous
behavior, it means there must be some cost in
addition to recursive cost
Solution to homogenous recursive relations is
comparatively easy, but not so easy in case of
non-homogenous recursive relations
Conclusion
Dr Nazir A. Zafar Advanced Algorithms Design and Analysis

Potrebbero piacerti anche