In this, we are going to discuss how Divide and Conquer technique is helpful and how we can solve the problem with the DAC technique approach. In this section, we will discuss as the following topics.
This technique can be divided into the following three parts:
The following are some standard algorithms that follows Divide and Conquer algorithm:
What does not qualifies as Divide and Conquer:
Binary Search is a searching algorithm. In each step, the algorithm compares the input element x with the value of the middle element in array. If the values match, return the index of the middle. Otherwise, if x is less than the middle element, then the algorithm recurs for left side of the middle element, else recurs for the right side of the middle element. Contrary to popular belief, this is not an example of Divide and Conquer because there is only one sub-problem in each step (Divide and conquer requires that there must be two or more sub-problems) and hence this is a case of Decrease and Conquer.
Divide And Conquer algorithm:
DAC(a, i, j)
{
if(small(a, i, j))
return(Solution(a, i, j))
else
m = divide(a, i, j) // f1(n)
b = DAC(a, i, mid) // T(n/2)
c = DAC(a, mid+1, j) // T(n/2)
d = combine(b, c) // f2(n)
return(d)
}
This is recurrence relation for above program.
O(1) if n is small
T(n) = f1(n) + 2T(n/2) + f2(n)
Example:
To find the maximum and minimum element in a given array.
Input: { 70, 250, 50, 80, 140, 12, 14 }
Output: The minimum number in a given array is: 12
The maximum number in a given array is: 250
Approach: To find the maximum and minimum element from a given array is an application for divide and conquer. In this problem, we will find the maximum and minimum elements in a given array. In this problem, we are using a divide and conquer approach(DAC) which has three steps divide, conquer and combine.
For Maximum: In this problem, we are using the recursive approach to find maximum where we will see that only two elements are left and then we can easily using condition i.e.
if(a[index] > a[index + 1].)
In a program line a[index] and a[index + 1])condition will ensure only two elements in left.
if(index >= l - 2)
{
if(a[index] > a[index + 1])
{
// (a[index]
// Now, we can say that the last element will be maximum in a given array.
}
else
{
//(a[index+1]
// Now, we can say that last element will be maximum in a given array.
}
}
In the above condition, we have checked the left side condition to find out the maximum.
Now, we will see the right side condition to find the maximum.
Recursive function to check the right side at the current index of an array.
max = DAC_Max(a, index + 1, l);
// Recursive call
Now, we will compare the condition and check the right side at the current index of a given array.
In the given program, we are going to implement this logic to check the condition on the right side at the current index.
// Right element will be maximum.
if(a[index]>max)
return a[index];
// max will be maximum element in a given array.
else
return max;
}
For Minimum: In this problem, we are going to implement the recursive approach to find the minimum no. in a given array.
int DAC_Min(int a[], int index, int l)
// Recursive call function to find the minimum no. in a given array.
if(index >= l - 2)
// to check the condition that there will be two-element in the left
then we can easily find the minimum element in a given array.
{
// here we will check the condition
if(a[index] < a[index + 1])
return a[index];
else
return a[index + 1];
}
Now, we will check the condition on the right side in a given array.
// Recursive call for the right side in the given array.
min = DAC_Min(a, index + 1, l);
Now, we will check the condition to find the minimum on the right side.
// Right element will be minimum
if(a[index]<min)
return a[index];
// Here min will be minimum in a given array.
else
return min;
Implementation:
C++
C
Java
C#
Python3
Output
Maximum: 120
Minimum: 11
Both paradigms (D & C and DP) divide the given problem into subproblems and solve subproblems. How to choose one of them for a given problem? Divide and Conquer should be used when the same subproblems are not evaluated many times. Otherwise Dynamic Programming or Memoization should be used. For example, Quicksort is a Divide and Conquer algorithm, we never evaluate the same subproblems again. On the other hand, for calculating the nth Fibonacci number, Dynamic Programming should be preferred.
81 videos|80 docs|33 tests
|
1. What is Divide & Conquer in Computer Science Engineering (CSE)? |
2. How does Divide & Conquer approach work in CSE? |
3. What are the key steps involved in the Divide & Conquer algorithm? |
4. What are the advantages of using the Divide & Conquer technique in CSE? |
5. What are some examples of algorithms that use the Divide & Conquer technique in CSE? |
|
Explore Courses for Computer Science Engineering (CSE) exam
|