Professional Documents
Culture Documents
Examples:
Binary Search , Quick Sort, Merge Sort
Divide-and-Conquer
• Divide the problem into a number of sub-problems
– Similar sub-problems of smaller size
• Conquer the sub-problems
– Solve the sub-problems recursively
– Sub-problem size small enough solve the problems in
straightforward manner
• Combine the solutions of the sub-problems
– Obtain the solution for the original problem
General method for Divide and Conquer Technique
Algorithm DAndC(p)
{
if Small(p) then return s(p);
else
{
divide p into smaller instances p1,p2,…….,pk, k≥1;
Apply DAndC to each of these subproblems;
return Combine(DAndC(p1), DAndC(p2),…,
DAndC(pk));
}
}
If the size of p is n and the sizes of the k sub problems are n 1,n2,….,nk,
then the computing time of DAndC is described by the recurrence
relation
T(n)= g( n) n small
T(n1)+T(n2)+……+T(nk)+f(n) Otherwise
Where T(n) is the time for DAndC on any input of size n and g(n) is
the time to compute the answer directly for small inputs.
The function f(n) is the time for dividing p and combining the
solutions from sub problems.
There are 2 methods for solving an recurrence relation
• Substitution method
• Master’s theorem
Substitution method
Example1 : Consider the case
Let T(1)=2 and f(n)=n. We have
T(n) = 2T(n/2)+n
= 2[2T(n/4)+n/2]+n
= 4T(n/4)+2n
=4[2T(n/8)+n/4]+2n
= 8T(n/8)+3n
:
=2iT(n/2i)+i.n for any log2n>=i>=1
=2log2nT(n/2log2n)+nlog2n=2n T(n/n) + nlog2n
Thus, T(n)=nT(1)+nlog2n=nlog2n+2n
T(n) = O(nlogn)
Example 2: Solve the following recurrence relation using
Substitution Method- T(n) = 4T(n/2) + n2
T(n) = 4T(n/2) + n2
= 4[4T(n/4)+ n2 /4]+ n2
= 16T(n/4)+ 2n2
= 16[4T(n/8)+ n2 /16]+2n2
= 64T(n/8)+ 3n2
= 43 T(n/23)+ 3.n2
:
= 4i T(n/2i)+ i.n2 for any log2n>=i>=1
=4log2nT(n/2log2n)+ n2 log2n
= 2log2n 2log2n T(n/2log2n)+ n2 log2n
= n.n T(n/n) + n2 log2n = n2 + n2 log2n
T(n) = O(n2logn)
Doubts ?
9
Master’s Theorem
https://www.programiz.com/dsa/master-theorem
•Master’s Theorem:
T(n) = aT(n/b) + f(n) where a >= 1 and b > 1 and
f(n)=θ (nklogpn)
•There are following three cases:
1. If f(n) = O(nk) where k < Logba (OR)
a >bk then T(n) = Θ(nLogba)
2. If f(n) = Θ(nk) where k = Logba (OR)
a =bk then T(n) = Θ(nLogba logp+1n)
3.If f(n) = ῼ(nk) where k> Logba (OR)
a < bk then T(n) = Θ(f(n))
Examples
Problem-01:
Solve the following recurrence relation using Master’s
theorem- T(n) = 3T(n/2) + n 2
Solution-
We compare the given recurrence relation with
T(n) = aT(n/b) + θ (nklogpn).
Then, we have a = 3,b = 2,k = 2,p = 0
Now, a = 3 and bk = 22 = 4.
3<4 i.e., a < bk.
So, we follow case-03.
Since p = 0, so we have- T(n) = θ (nklogpn) (or) f(n)
T(n) = θ (n2log0n)
Thus, T(n) = θ (n2)
Problem-02:
Solve the following recurrence relation using Master’s
theorem- T(n) = 2T(n/2) + nlogn
Solution-
We compare the given recurrence relation with
T(n) = aT(n/b) + θ (nklogpn).
Then, we have- a = 2, b = 2, k = 1, p = 1
Now, a = 2 and bk = 21 = 2.
Clearly, a = bk.
So, we follow case-02.
Since p = 1, so we have- T(n) = θ (nlogba.logp+1n)
T(n) = θ (nlog22.log1+1n)
Thus, T(n) = θ (nlog2n)
Problem-03:
Solve the following recurrence relation using Master’s
theorem- T(n) = 2T(n/4) + n0.51
Solution-
We compare the given recurrence relation with
T(n) = aT(n/b) + θ (nklogpn).
Then, we have- a = 2, b = 4, k = 0.51, p = 0
Solution-