Professional Documents
Culture Documents
Sesion-4 Divide and Conquer Technique
Sesion-4 Divide and Conquer Technique
General Method:
➢ These sub problems will be solved and then combined by using a separate method to get a
solution to a whole problem.
➢ If the sub problems are large, then the Divide and Conquer Technique will be reapplied.
➢ Often sub problems resulting from a Divide and Conquer Technique are of the same type as
the original problem.
Algorithm DAndC(p)
else
}
If the size of p is n and the sizes of the k sub problems are n1,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 to sub problems.
• Substitution method
• Master’s theorem
Substitution method
Example: Consider the case in which a=2 and b=2. 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
=2log2nT(n/2log2n)+nlog2n
Thus, T(n)=nT(1)+nlog2n=nlog2n+2n
Master’s Theorem:
• Master Method is a direct way to get the solution. The master method works only for
following type of recurrences or for recurrences that can be transformed to following type.
Examples:
Problem-01:
Solve the following recurrence relation using Master’s theorem- T(n) = 3T(n/2) + n2
Solution-
Now, a = 3 and bk = 22 = 4.
T(n) = θ (n2log0n)
Problem-02:
Solve the following recurrence relation using Master’s theorem- T(n) = 2T(n/2) + nlogn
Solution-
Then, we have- a = 2, b = 2, k = 1, p = 1
Now, a = 2 and bk = 21 = 2.
Clearly, a = bk.
T(n) = θ (nlog22.log1+1n)
Problem-03:
Solve the following recurrence relation using Master’s theorem- T(n) = 2T(n/4) + n0.51
Solution-
T(n) = θ (n0.51log0n)
Problem-04:
Solve the following recurrence relation using Master’s theorem- T(n) = √2T(n/2) + logn
Solution-
Solution-
The given recurrence relation does not correspond to the general form of Master’s theorem.
Problem-06:
Solve the following recurrence relation using Master’s theorem- T(n) = 3T(n/3) + n/2
Solution-
Then, we have- a = 3, b = 3, k = 1, p = 0
Now, a = 3 and bk = 31 = 3.
Problem-07:
Form a recurrence relation for the following code and solve it using Master’s theorem-
A(n)
if(n<=1) return 1;
Solution-
Here 1 = Constant time taken for comparing and returning the value.
We can not directly apply Master’s Theorem on this recurrence relation.
This is because it does not correspond to the general form of Master’s theorem.
However, we can modify and bring it in the general form to apply Master’s theorem.
Let- n = 2m ……(1)
Then, we have- a = 1, b = 2, k = 0, p = 0
Now, a = 1 and bk = 20 = 1.
Quicksort:
• Divide
– Partition the array A into 2 subarrays A[p..q] and A[q+1..r], such that each element
of A[p..q] is smaller than or equal to each element in A[q+1..r]
• Conquer
• Combine
• Divide:
4.If i<j condition satisfied then swap a[i] and a[j] &
7. First sub array < pivot and second sub array >pivot
8. Again apply the quick sort procedure to both halfs till the elements are sorted
Quick Sort Algorithm :
Algorithm QuickSort(low,high)
//Sorts the elements a[low],…..,a[high] which resides in the global array a[1:n] into //ascending
order;
QuickSort(low,j-1);
QuickSort(j+1,high);
Algorithm Partition(l,h)
while( i < j ) do
i++;
i++;
j--;
j--;
} // jth elements.
temp=a[x];
a[x]=a[y];
a[y]=temp;
Merge Sort:
1. Base Case, solve the problem directly if it is small enough(only one element).
2. Divide the problem into two or more similar and smaller subproblems.
// Small(P) is true if there is only one element to sort. In this case the list is already sorted.
mid := [(low+high)/2];
MergeSort(mid+1, high);
// and in a[mid+1:high]. The goal is to merge these two sets into a single set
b[i]:=a[h]; h:=h+1;
else
b[i]:=a[j]; j:=j+1;
i:=i+1;
/}
}
else
a[k]:=b[k];
}
Assignments: