You are on page 1of 32

Sesion-4

Divide and Conquer Technique

General Method:

➢ The Divide and Conquer Technique splits n inputs into k subsets ,

1< k ≤ n, yielding k sub problems.

➢ 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.

➢ 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

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 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.

There are 2 methods for solving an recurrence relation

• 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

=2iT(n/2i)+in, for any log2n>=i>=1

=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.

• T(n) = aT(n/b) + f(n) where a >= 1 and b > 1


• There are following three cases:

1. If f(n) = Θ(nc) where c < Logba then T(n) = Θ(nLogba)

2. If f(n) = Θ(nc) where c = Logba then T(n) = Θ(ncLog n)

3.If f(n) = Θ(nc) where c > Logba then T(n) = Θ(f(n))

Examples:

Problem-01:

Solve the following recurrence relation using Master’s theorem- T(n) = 3T(n/2) + n2

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.

Clearly, a < bk.

So, we follow case-03.

Since p = 0, so we have- T(n) = θ (nklogpn)

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

Now, a = 2 and bk = 40.51 = 2.0279.

Clearly, a < bk.

So, we follow case-03.

Since p = 0, so we have- T(n) = θ (nklogpn)

T(n) = θ (n0.51log0n)

Thus, T(n) = θ (n0.51)

Problem-04:

Solve the following recurrence relation using Master’s theorem- T(n) = √2T(n/2) + logn

Solution-

We compare the given recurrence relation with

T(n) = aT(n/b) + θ (nklogpn).

Then, we have- a = √2, b = 2, k = 0, p = 1

Now, a = √2 = 1.414 and bk = 20 = 1.

Clearly, a > bk.

So, we follow case-01.

So, we have- T(n) = θ (nlogba) T(n) = θ (nlog2√2)


Problem-05:

Solve the following recurrence relation using Master’s theorem-

T(n) = 8T(n/4) – n2logn

Solution-

The given recurrence relation does not correspond to the general form of Master’s theorem.

So, it can not be solved using Master’s theorem.

Problem-06:

Solve the following recurrence relation using Master’s theorem- T(n) = 3T(n/3) + n/2

Solution-

We write the given recurrence relation as T(n) = 3T(n/3) + n.

Now, we can easily apply Master’s theorem.

We compare the given recurrence relation with T(n) = aT(n/b) + θ (nklogpn).

Then, we have- a = 3, b = 3, k = 1, p = 0

Now, a = 3 and bk = 31 = 3.

Clearly, a = bk.So, we follow case-02.

Since p = 0, so we have- T(n) = θ (nlogba.logp+1n)

T(n) = θ (nlog33.log0+1n) T(n) = θ (n1.log1n)

Problem-07:

Form a recurrence relation for the following code and solve it using Master’s theorem-

A(n)

if(n<=1) return 1;

else return A(√n);

Solution-

We write a recurrence relation for the given code as T(n) = T(√n) + 1.

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- T(2m) = T(2m/2) + 1

Now, let T(2m) = S(m), then T(2m/2) = S(m/2)

So, we have- S(m) = S(m/2) +1

Now, we can easily apply Master’s Theorem.

We compare the given recurrence relation with S(m) = aS(m/b) + θ (mklogpm).

Then, we have- a = 1, b = 2, k = 0, p = 0

Now, a = 1 and bk = 20 = 1.

Clearly, a = bk. So, we follow case-02.

Since p = 0, so we have- S(m) = θ (mlogba.logp+1m)

S(m) = θ (mlog21.log0+1m) = θ (m0.log1m)

Thus, S(m) = θ(logm) ……(2) Now, From (1), we have n = 2m.

So, logn = mlog2 which implies m = log2n.

Substituting in (2), we get- S(m) = θ(loglog2n) OrT(n) = θ (loglog2n)

Quicksort:

• Sort an array A[p…r]

• 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]

– Need to find index q to partition the array

• Conquer

– Recursively sort A[p..q] and A[q+1..r] using Quicksort

• Combine

– Trivial: the arrays are sorted in place

– No additional work is required to combine them

– The entire array is now sorted

• Divide:

• Pick any element as the pivot, e.g, the first element

• Partition the remaining elements into

FirstPart, which contains all elements < pivot

SecondPart, which contains all elements > pivot

• Recursively sort FirstPart and SecondPart.

• Combine: no work is necessary since sorting is done in place.


Quick sort procedure

1.First element will be taken as a pivot element

2.Move i towards right until it satisfies a[i]≥pivot

3.Move j towards left until it satisfies a[j]≤pivot

4.If i<j condition satisfied then swap a[i] and a[j] &

continue from step 2

5.If i≥j then swap a[j] and pivot

6.If pivot is moved then array will be divided into 2 halfs.

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;

// a[n+1] is considered to be defined and must ≥ all the elements in a[1:n].

if( low< high ) // if there are more than one element

{ // divide p into two subproblems.


j :=Partition(low,high);

// j is the position of the partitioning element.

QuickSort(low,j-1);

QuickSort(j+1,high);

// There is no need for combining solutions.

Algorithm Partition(l,h)

pivot:= a[l] ; i:=l; j:= h+1;

while( i < j ) do

i++;

while( a[ i ] < pivot ) do

i++;

j--;

while( a[ j ] > pivot ) do

j--;

if ( i < j ) then Interchange(i,j ); // interchange ith and

} // jth elements.

Interchange(l, j ); return j; // interchange pivot and jth element.

Algorithm interchange (x,y )

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.

3. Recursively solve the subproblems.

4. Combine solutions to the subproblems.

Merge Sort: Algorithm :

Algorithm MergeSort ( low, high)

// a[low: high] is a global array to be sorted.

// Small(P) is true if there is only one element to sort. In this case the list is already sorted.

if ( low<high ) then // if there are more than one element

// Divide P into sub problems.

// Find where to split the set.

mid := [(low+high)/2];

//solve the sub problems.


MergeSort(low,mid);

MergeSort(mid+1, high);

// Combine the solutions.

Merge(low, mid, high);

Algorithm Merge(low, mid, high)

// a[low:high] is a global array containing two sorted subsets in a[low:mid]

// and in a[mid+1:high]. The goal is to merge these two sets into a single set

// residing in a [low: high]. b[ ] is a temporary global array.

h := low; i := low; j := mid+1;

while( h ≤ mid ) and ( j ≤ high ) do

if( a[h] ≤ a[j] ) then

b[i]:=a[h]; h:=h+1;

else

b[i]:=a[j]; j:=j+1;

i:=i+1;

/}

if( h > mid ) then

for k:=j to high do

b[i] := a[k]; i:= i+1;

}
else

for k:=h to mid do

b[i] := a[k]; i:= i+1;

for k:= low to high do

a[k]:=b[k];

}
Assignments:

1. Sort the following elements using Quick sort technique:

54, 26, 93, 17, 77, 31, 44, 55, 20

2. Sort the following elements using Merge sort technique:

54, 26, 93, 17, 77, 31, 44, 55, 20

You might also like