Sunteți pe pagina 1din 4

CS5613- Advanced Analysis of Algorithms MS(CS) - MAJU

Lecture 4

Divide and Conquer Method

DIVIDE-AND-CONQUER METHOD

Introduction:

Both merge sort and quicksort employ a common algorithmic paradigm based on recursion. This
paradigm, divide-and-conquer, breaks a problem into subproblems that are similar to the original
problem, recursively solves the subproblems, and finally combines the solutions to the
subproblems to solve the original problem. Because divide-and-conquer solves subproblems
recursively, each subproblem must be smaller than the original problem, and there must be a
base case for subproblems.

You should think of a divide-and-conquer algorithm as having three parts:

1. Divide the problem into a number of subproblems that are smaller instances of the same
problem.
2. Conquer the subproblems by solving them recursively. If they are small enough, solve
the subproblems as base cases.
3. Combine the solutions to the subproblems into the solution for the original problem.

You can easily remember the steps of a divide-and-conquer algorithm as divide, conquer,
combine. Here's how to view one step, assuming that each divide step creates two subproblems
(though some divide-and-conquer algorithms create more than two):

Prepared by: Engr. M. Nadeem Page 1


CS5613- Advanced Analysis of Algorithms MS(CS) - MAJU

Because divide-and-conquer creates at least two subproblems, a divide-and-conquer algorithm


makes multiple recursive calls.

Simple Iterative method: (Brute Force Method)


int power(int base, int num)
1
{
2
int result = 1;
3
4
// this function will return base ^ num
5
int i= 1;
6 for(i = 1; i<=num; i++)
7 {
8 result = result * base;}
9
}
10
return result;
}

Recursive Method:
int power(int base, int num)
{
if(num == 0)
{
return 1;
}
return base * power(base, num - 1);
}

But in both these methods, the time complexity is of O(n), and this can take a long time in
operation.

Divide and Conquer:


int power(int base, int pwr)
{
int temp;
if (pwr == 0)
return 1;
temp = power(base, pwr / 2);
if (pwr % 2 == 0)
return temp * temp;
else
return base * temp * temp;
}

Prepared by: Engr. M. Nadeem Page 2


CS5613- Advanced Analysis of Algorithms MS(CS) - MAJU

The technique we are using here is Divide and Conquer. We can further reduce its complexity
to O(log n) by calculating the result power(base,num/2) only once and then using it further.

Application of divide and conquer algorithm:

 Sorting: merge sort and quicksort


 Binary tree traversals
 Binary search
 Multiplication of large integers

Conclusion:

 The Divide-and-Conquer paradigm can be described in this way:


o Given an instance of the problem to be solved, split this into several, smaller, sub-
instances (of the same problem) independently solve each of the sub-instances
and then combine the sub-instance solutions so as to yield a solution for the
original instance.
 It gives very efficient solutions. It is the choice when the following is true:
o the problem can be described recursively - in terms of smaller instances of the
same problem.
o the problem-solving process can be described recursively, i.e. we can combine the
solutions of the smaller instances to obtain the solution of the original problem
 Note that we assume that the problem is split in at least two parts of equal size, not
simply decreased by a constant factor.

Prepared by: Engr. M. Nadeem Page 3


CS5613- Advanced Analysis of Algorithms MS(CS) - MAJU

Prepared by: Engr. M. Nadeem Page 4

S-ar putea să vă placă și