DR Nazir A. Zafar Advanced Algorithms Analysis and Design
DR Nazir A. Zafar Advanced Algorithms Analysis and Design
By
Lecture No. 8
Recurrence Relations
(Algorithms Design and Analysis Techniques)
Dr Nazir A. Zafar
Today Covered
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
Advanced Algorithms Analysis and Design
Dr Nazir A. Zafar
Dr Nazir A. Zafar
Since, p(r1) = 0, p(r2) = 0, p(r3) = 0, . . ., p(rk) = 0 Hence all x = ri , for i {1, 2, . . .,k} are solutions to above characteristic polynomial. Therefore, r1n, r2n, . . ., rkn are solution to our original recurrence relation. Since linear combination of solutions is also a solution to recurrence, therefore below is a solution.
T (n) ci ri
i 1
Dr Nazir A. Zafar
where, c1, c2, . . ., ck are all constants to be determined finding particular solution The remarkable fact is that this equation has only solutions of this form provided all ri are distinct. Constants can be determined from k initial conditions by solving system of k linear equations in k unknowns
Dr Nazir A. Zafar
Solving these equations, we obtain c1 = -1/4, c2 = 0 and c3 = 1/4 Therefore, tn = c1 (-1/4)(-1)n + (1/4).(3)n
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Dr Nazir A. Zafar
Solution: It can be found as in case of second order linear homogenous recurrence relation. Since r is a multiple root. By definition of multiple roots, there exists a polynomial q(x) of degree k-2 such that the following holds p(x) = (x r)2q(x), for every n k
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
where c1, c2, b1, b2,. . ., and bk-2 are all real constants
Advanced Algorithms Analysis and Design
Dr Nazir A. Zafar
t n c1r c2 nr ... ck n r
n n
k 1 n
tn c j n r
j 1
j 1 n
m1 1 n 1 m2 1
r r2
n
c21r2 c22 nr2 c23n r2 ... c2 m2 n ... cl1rl cl 2 nrl cl 3n rl ... clml n
n n 2 n
Dr Nazir A. Zafar
ml 1 n l
Contd..
t n c11r1 c12 nr1 c13n r1 ... c1m1 n
n n 2 n n n 2 n m1 1 n 1 m2 1
r r2
n
c21r2 c22 nr2 c23n r2 ... c2 m2 n ... cl1rl cl 2 nrl cl 3n rl ... clml n
n n 2 n
ml 1 n l
tn c1 j n r c2 j n r2 ... clj n r
j 1 j 1 n 1 j 1 n j 1 j 1
m1
m2
ml
j 1 n l
tn cij n r
i 1 j 1
Dr Nazir A. Zafar
mi
j 1 n i
Problem
Statement: Consider the recurrence tn = n if n = 0, 1, 2 tn = 5tn-1 - 8tn-2 + 4tn-3 otherwise Find general solution of the recurrence above.
Solution: First we rewrite the recurrence. tn - 5tn-1 + 8tn-2 - 4tn-3 = 0 The characteristic equation become. x3 5x2 + 8x -4 = (x-1) (x-2)2 The roots are: r1 = 1 of multiplicity m1 = 1 and r2 = 2 of multiplicity m2 = 2, and hence the general solution is tn = c 1 1 n + c 2 2 n + c 3 n 2 n
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Example
The initial conditions give c1 + c2 = 0 c1 + 2c2 + 2c3 = 1 c1 + 4c2 + 8c3 = 2
Dr Nazir A. Zafar
Non-homogenous Recurrence
Solution of a linear recurrence with constant coefficients becomes more difficult when the recurrence is not homogeneous That is when linear combination is not equal to zero In particular, it is no longer true that any linear combination of solutions is a solution. Consider the following recurrence
a0 tn + a1 tn-1 + . . .+ ak tn-k = bn p(n) The left-hand side is the same as before, but on the right-hand side we have bnp(n) where b is a constant and p(n) is a polynomial in n of degree d.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
(a0 x k a1 x k 1 . . . ak )( x b1d 1 )
Which contains one factor for the left hand side And other factor corresponding to the right hand side, where d is degree of polynomial Characteristics polynomial can be used to solve the above recurrence.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Dr Nazir A. Zafar
Adding these equation, we get homogenous equation, which can be solved easily tn - 8tn-1 + 21tn-2 - 18tn-3 = 0
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
The characteristics equation of the above homogenous equation is: x3 8x2 +21x -18 = 0 ( x-2) (x-3)2 = 0 and hence, x = 2, 3, 3 General solution is: tn = c1 2n + c2 3n + c3 n 3n For n = 0, 1, 2 We can find values of c1, c2, c3 and then tn = (t0 - 9) 2n + (n + 3)3n+1
Advanced Algorithms Analysis and Design
Dr Nazir A. Zafar
Dr Nazir A. Zafar
Which contains one factor for the left hand side And other factor corresponding to the each term on right hand side. Once the characteristics polynomial is obtained the recurrence can be solved as before.
Dr Nazir A. Zafar
Dr Nazir A. Zafar
Dr Nazir A. Zafar
Conclusion
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
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design