UNIT 3 - 3150703 - Divide and Conqour
UNIT 3 - 3150703 - Divide and Conqour
Take n=2
T(2)=c*2*log2
T(2)=2*2*log2=4 Assume c>=2
T(n)=O(n2)
Exercise
• T(n)=T(n−2)+n2
• T (n) = T (n-1) +1
• T(n)=4T(n/2)+n2
• T(n)=4T(2*n/5)+n O(nlog5/2n)
• T(n)=c1*T(n/a)+f(n) O(f(n)*logan)
Iteration Method
• We can also called it as back substitution
methos.
• In this method, we first convert the recurrence
into a summation.
• We do so by iterating the recurrence until the
initial condition is reached.
• For converting the recurrence of the previous
example into summation, we would first
break T(n) into T(n/2) and then into T(n/4) and
so on.
Example 1:
T(n)=T(n-1)+n -----------1
T(n-1)=T(n-2)+(n-1) -----------2
T(n-2)=T(n-3)+(n-2) -----------3
T(n)=T(n-3)+(n-2)+(n-1)+n -----------4
T(n)=T(n-3)+(n-2)+(n-1)+n -----------4
T(n)=n+(n-1)+(n-2)+……….+T(n-k)
T(n)=T(n-k)+(n-k+1)+(n-k+2)+……..+ n
➢Suppose if we take k=n thus,
T(n)=T(n-n)+(n-n+1)+(n-n+2)+…………+n
T(n)=0+1+2+3+…………+n
T(n)=n(n+1)/2 =O(n2)
Example 2:
Example 3:
T(n)=2T(n/3)+n -----------1
➢Replacing n by n/3 , we can write following equation
T(n/3)=2T(n/9)+(n/3) -----------2
➢Put eq. 2 in 1 , we can write following equation
T(n)=2(2T(n/9)+(n/3))+n
T(n)=n+(2/3)*n+4T(n/9)) -----------3
Now
T(n/9)=2T(n/27)+(n/9) -----------4
T(n)=n+(2/3)*n+(4/9)*n +(8/27)*n+…..+2kT(n/3k)
n/3k=1
3k=n
➢So Taking log on both side we have
log(3k)=logn
klog(3)=logn
k=logn/log3=log3n
T(n)=n+(2/3)*n+(4/9)*n +8T(n/27)+…..
➢So for nth iteration we can write,
Thus,
T(n)=O(n)
Example 4:
T(n)=2T(n/2)+n -----------1
➢Replacing n by n/2 , we can write following equation
T(n/2)=2T(n/4)+(n/2) -----------2
➢Put eq. 2 in 1 , we can write following equation
T(n)=2(2T(n/4)+(n/2))+n
T(n)=n+n+4T(n/4)) -----------3
Now
T(n/4)=2T(n/8)+(n/4) -----------4
T(n)=n+n+n +n+…..+2kT(n/2k)
n/2k=1
2k=n
➢So Taking log on both side we have
Log(2k)=logn
Klog(2)=logn
k=logn/log2=log2n
➢So for nth iteration we can write,
T(n)=n+n+n +n+…..n+2kT(n/2k)
k
T(n)=kn+2kT(n/2k)
➢Put k=log n here , we get
T(n)=(logn)n+2lognT(1)
T(n)=(logn)n + nT(1)
T(n)=n(logn) + n if T(1)=1
Thus,
T(n)=O(n log n)
Example 5:
T(n)=T(n/2)+c -----------1
➢Replacing n by n/2 , we can write following equation
T(n/2)=T(n/4)+c -----------2
➢Put eq. 2 in 1 , we can write following equation
T(n)=(T(n/4)+c)+c
T(n)=c+c+4T(n/4)) -----------3
Now
-----------4
T(n/4)=T(n/8)+c
➢Put eq. 4 in 3 , we can write following equation
T(n)=c+c+c+T(n/8)))
T(n)=c+c+c+T(n/8)
➢From above , we can write general form for k as
T(n)= c+c+c+…….+T(n/2k)
n/2k=1
2k=n
➢So Taking log on both side we have
Log(2k)=logn
Klog(2)=logn
k=logn/log2=log2n
➢So for nth iteration we can write,
T(n)=c+c+c +c+…..c+T(n/2k)
k
T(n)=kc+T(n/2k)
➢Put k=log n here , we get
T(n)=(logn)+(1)
T(n)=(logn) + 1
Thus,
T(n)=O( log n)
Exercise
• T(n)=2T(n/2)+c
Recurrence Tree Method
▪ In recurrence tree, each node represents the cost of a single sub-
problem in the set of recursive function invocations.
▪ We sum the costs within each level of the tree to obtain a set of
per level costs.
▪ Then we sum the all the per level costs to determine the total cost
of all levels of the recursion.
29
Recurrence Tree Method
30
Recurrence Tree Method
Cost of level
n/2+n/2
n/4+n/4+n/4+n/4
31
Recurrence Tree Method
Determine total number of levels in the recursion tree-
Klog(2)=logn
k=logn/log2=log2n
➢Total number of levels in the recursion tree
level=log2n+1
Recurrence Tree Method
34
Recurrence Tree Method
37 37
Recurrence Tree Method
38
Recurrence Tree Method
39
Master Method
40
Master Method
41
Master Method
Merge sort
42
Master Method
Binary
Search
43
Master Method
44
Master Method
45