Week 3
Week 3
CSO102
WEEK-3 (DRAFT)
Solutions to Some Recurrence
Relations
■ Substitution Method:
– Problem 1:
• tn=tn-1+n n>2, t1=1
• tn=tn-2+n-1+n
• =1+2+3+.................+n=O(n2)
– Problem 2:
• tn=tn/2+n
• =tn/4+n/2+n , k=log(n)
• =1+2+..........+2k=2k+1-1=O(n)
Homogenous Recurrence
■ a0tn+a1tn-1+------+aktn-k=0
■ tn =
■ ci’s are determined by intial conditions
– Let tn=xn ,where x is a constant
– a0xn+a1xn-1+-----+akxn-k=0
– X=0 (trivial solution )
– If r1,r2,r3,..rk, are k roots of above characteristic equation.
■ Problem 1:
– tn-3tn-1-4tn-2=0,t0=0 and t1=1
– x2-3x-4=0 => tn=c1(-1)n+c24n
T(n) = O(4n)
InHomogenous Recurrence
■ a0tn+a1tn-1+---+aktn-k=F(n)
■ Problem :
– tn=2tn-1+n
– tn-2tn-1=n ----eq(1)
– Replacing n by n+1 ,tn+1-2tn=n+1
– 2tn+1-4tn=2n+2 ---eq(2)
– tn+2-2tn+1=n+2 ---eq(3)
– Solving eq(3)+eq(1)-eq(2) by homogenous recurrence
– tn=O(2n)
– eg : 1) tn=2tn-1+n+2
– eg: 2) tn=tn-1+tn-2+n2+n+1
Change of Variable
■ T(n)=4T(n/2)+n
– T(2k)=4T(2k-1)+2k
– tk=4tk-1+2k
– O(n2)
=
■ Ex : 1
– T(n) = 4T(n/2) + n
– a = 4, b = 2 nlogba = n2; f (n) = n.
– CASE 1: T(n) = Ο(n2).
■ Ex : 2
– T(n) = 4T(n/2) + n2
– a = 4, b = 2 , nlogba = n2; f (n) = n2.
– CASE 2: T(n) = O(n2lg n).
PARTITION
QUICK SORT
QUICK SORT
■ TIME COMPLEXITY:
■ WORST CASE: T(N)= T(N-1) + N
■ BEST & AVERAGE CASES: T(N) = 2T(N/2) + N