Solve the following recurrence relation, subject to the basis. S(1) = 2 S(n) =2S(n/2) + 2n
Solutions:-
given
S(1) = 2 S(n) =2S(n/2) + 2n
now we are consider that
S(n)= 2 , n=1
S(n)=2S(n/2)+2n , n>1
S(n)=2S(n/2)+2n --------------------(1)
S(n/2)=2S(n/22)+2*(n/2)
=2S(n/22)+n
put the value of S(n/2) on equation (1).
S(n)=2[2S(n/22)+n]+2n
=22S(n/22)+2n+2n
S(n) =22S(n/22)+4n-------------------(2)
S(n/22)=2S(n/23)+2*(n/22)
=2S(n/23)+(n/2)
S(n)=22[2S(n/23)+(n/2)]+4n
S(n) =23S(n/23)+6n-------------------(3)
|
|
|
|
S(n)=2kS(n/2k)+2kn (using K term)
2k is constant so we can write it as only k
S(n)=2kS(n/2k)+kn
let we are assume that
S(n/2k)=S(1)
n/2k=1
or,n=2k
or,k =log n
now,
S(n)=n*1+n(log n)
time complexity=O(n log n)
Get Answers For Free
Most questions answered within 1 hours.