Uploaded by wegepi9592

Week 4(Recurrence Relations)

advertisement
Week 4
(Recurrences Relation )
Factorial Numbers
5!=5*4*3*2*1
Factorial (n)
{
If(n==1)
then return 1 // base case
Else
Return n*Factorial(n-1) n*factorial(n-1)
}
Write the recurrence relation of the following recursive algorithm state the
complexity of the algorithm.
Go(n)
{
If(n==1) then return 1 // T(1)=1 n==1
Return Go(n-1)+Go(n-1) // T(n)=T(n-1)+T(n-1) == 2T(n-1)
}
n>1
T(1)=1
T(n)=2T(n-1)
By using Expansion method
T(n)=2 T(n-1)
T(n-1)= 2(2T(n-2))
=22 T(n-2)
=22 (2T(n-3))
=23T(n-3)
We can deduce pattern
2kT(n-k) ➔ Equation 1
T(1)=1 ➔Equation 2
n-k=1
k=n-1
By sub In Equation 1
2n-1 = 2n* 2-1 O(2n)
Question (2)
T(1)=1 ,T(n)=2T(n/4)+n1/2
Question (3)
T(1)=1 , T(n)=2T(n/2)+logn
Sheet (2)
Question (1)
T(1)=1,T(n)=4T(n/3)+n for n>1
Question (2)
Test(n)
{
If(n==1) then return 1
Temp=Test(n/2)+Test(n/2)
For i =1 to n do
Temp=temp+i*j
End for
Return temp
}
Solution :
Master Method
Question (1)
T(1)=1 ,T(n)=2T(n/4)+n1/2
Solution :
Question (2)
T(1)=1,T(n)=4T(n/3)+n for n>1
Solution:
Download