0% found this document useful (0 votes)
40 views43 pages

UNIT 3 - 3150703 - Divide and Conqour

Uploaded by

VEDANT PATEL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views43 pages

UNIT 3 - 3150703 - Divide and Conqour

Uploaded by

VEDANT PATEL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Unit-3

Divide and Conquer


Algorithm
Topics to be covered
• Introduction to Recurrence Equation
• Different methods to solve recurrence
• Divide and Conquer Technique
• Multiplying large Integers Problem
• Problem Solving using divide and conquer
algorithm –
1. Binary Search
2. Sorting (Merge Sort, Quick Sort)
3. Matrix Multiplication
4. Exponential
Recurrence Equation
Introduction
Methods to Solve Recurrence
• Substitution
• Iteration
• Master method
• Recurrence tree
Substitution Method
• A lot of things in this class reduce to induction
• In the substitution method for solving
recurrences we
➢Guess the form of the solution
➢Use mathematical induction to find the constants
and show that the solution works.
Example 1
T(1) = 1 and T(n) = 2T(n/2) + n for n > 1.
➢We guess that the solution is T(n) = O(n log n)
➢So we must prove that T(n) ≤ cn log n for
some constant c.
(We will get to n0 later, but for now let’s try to
prove the statement for all n ≥ 1.)
➢As our inductive hypothesis,
➢we assume T(n) ≤ cn log n for all positive numbers
less than n.
➢Therefore,
T(n/2) ≤ c*(n/2) log(n/2)
So
T(n) ≤ 2[c*(n/2) log(n/2)]+n T(n) = 2T(n/2) + n
≤ c*(n) log(n/2)+n
= cn log n − cn log 2 + n
= cn log n − cn + n
≤ cn log n (for c ≥ 1)
Thus T(n)=O(nlogn)
• Using Mathematical Induction, We need to prove that our
assumption holds true for boundary condition also.
So For n=1 T(n)<=0 Because log1=0

Take n=2
T(2)=c*2*log2
T(2)=2*2*log2=4 Assume c>=2

Our equation is T(n)= 2T(n/2) + n


when n=2
T(2)=2T(1)+2 =4 <=4 when c=2
Therefore,
we have shown that T(n) ≤ 2n log n for all n ≥ 2
T(n)=O(nlogn)
Example 2
T(1) = 1 and T(n) = T(n-1) + n for n > 0.
➢We guess that the solution is T(n) = O(n2)
➢So we must prove that T(n) ≤ cn2for some
constant c.
➢As our inductive hypothesis,
➢we assume T(n) ≤ cn2for all positive numbers less
than n.
➢Therefore,
T(n-1) <= c*(n-1)2
So
T(n) ≤ [c*(n-1)2]+n
= cn2 -2cn+c+n
= cn2 +n(1-2c)+c
≤ cn2 (for c ≥ 1/2)
Thus T(n)=O(n2)
• Using Mathematical Induction, We need to prove that our
assumption holds true for boundary condition also.

So, Take n=1


T(1)=c*12
T(1)=1 Assume c>=1

Our equation is T(n)= T(n-1) + n


when n=1
T(1)=T(0)+1=1 <=1
Therefore,
we have shown that T(n) ≤ n2 for all n ≥ 1

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

➢Replacing n by n-1 and n-2, we can write following equation

T(n-1)=T(n-2)+(n-1) -----------2

T(n-2)=T(n-3)+(n-2) -----------3

➢Substituting equation 3 in 2 and equation 2 in 1 we have now

T(n)=T(n-3)+(n-2)+(n-1)+n -----------4
T(n)=T(n-3)+(n-2)+(n-1)+n -----------4

➢From above , we can write general form for k as

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

➢Put eq. 4 in 3 , we can write following equation


T(n)=n+(2/3)*n+4(2T(n/27)+(n/9))))
T(n)=n+(2/3)*n+(4/9)*n+8T(n/27)))
T(n)=n+(2/3)*n+(4/9)*n +8T(n/27)+…..
➢From above , we can write general form for k as

T(n)=n+(2/3)*n+(4/9)*n +(8/27)*n+…..+2kT(n/3k)

➢So for initial condition we have T(1) therefore we can write

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,

T(n)=n+(2/3)*n+(2/3)2 (n) +(2/3)3 (n) +…..+2log3nT(1)

T(n)<=n ∑ (2/3)i +2log3n O(1)


∑r=1/(1-r)
T(n)<=n ∑ (2/3)i +2log3n O(1)
T(n)<= n (1/(1-2/3))+nlog32 O(1)
T(n)<= 3n+O(n)

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

➢Put eq. 4 in 3 , we can write following equation


T(n)=n+n+4(2T(n/8)+(n/4))))
T(n)=n+n+n+8T(n/8)))
T(n)=n+n+n +8T(n/8)+…..
➢From above , we can write general form for k as

T(n)=n+n+n +n+…..+2kT(n/2k)

➢So for initial condition we have T(1) therefore we can write

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)

➢So for initial condition we have T(1) therefore we can write

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-

➢ Size of sub-problem at level-0 = n/20


➢ Size of sub-problem at level-1 = n/21
➢ Size of sub-problem at level-2 = n/22 and so on.
➢ So Size of sub-problem at level-k = n/2k

Suppose at level-k (last level), size of sub-problem becomes 1


Then
n/2k=1
32
Recurrence Tree Method
Suppose at level-k (last level), size of sub-problem becomes 1
n/2k=1
2k=n
➢So Taking log on both side we have
Log(2k)=logn

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

Time to divide &


recombine

Number of Time required to


sub-problems solve a sub-
problem

40
Master Method

41
Master Method

Merge sort

42
Master Method

Binary
Search

43
Master Method

44
Master Method

45

You might also like