Divide and Conquer Algorithm | Introduction

23 Jun, 2022
In this article, 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 the following topics. 
1. Introduction to DAC.
2. Algorithms under DAC techniques.
3. Recurrence Relation for DAC algorithm.
4. Problems using DAC technique.
Divide And Conquer This technique can be divided into the following three parts:
Divide: This involves dividing the problem into smaller sub-problems.Conquer: Solve sub-problems by calling recursively until solved.Combine: Combine the sub-problems to get the final solution of the whole problem. 
Divide: This involves dividing the problem into smaller sub-problems.
Conquer: Solve sub-problems by calling recursively until solved.
Combine: Combine the sub-problems to get the final solution of the whole problem. 
The following are some standard algorithms that follow Divide and Conquer algorithm.  
Quicksort is a sorting algorithm. The algorithm picks a pivot element and rearranges the array elements so that all elements smaller than the picked pivot element move to the left side of the pivot, and all greater elements move to the right side. Finally, the algorithm recursively sorts the subarrays on the left and right of the pivot element.Merge Sort is also a sorting algorithm. The algorithm divides the array into two halves, recursively sorts them, and finally merges the two sorted halves.Closest Pair of Points The problem is to find the closest pair of points in a set of points in the x-y plane. The problem can be solved in O(n^2) time by calculating the distances of every pair of points and comparing the distances to find the minimum. The Divide and Conquer algorithm solves the problem in O(N log N) time.Strassen’s Algorithm is an efficient algorithm to multiply two matrices. A simple method to multiply two matrices needs 3 nested loops and is O(n^3). Strassen’s algorithm multiplies two matrices in O(n^2.8974) time.Cooley–Tukey Fast Fourier Transform (FFT) algorithm is the most common algorithm for FFT. It is a divide and conquer algorithm which works in O(N log N) time.Karatsuba algorithm for fast multiplication does the multiplication of two n-digit numbers in at most
Quicksort is a sorting algorithm. The algorithm picks a pivot element and rearranges the array elements so that all elements smaller than the picked pivot element move to the left side of the pivot, and all greater elements move to the right side. Finally, the algorithm recursively sorts the subarrays on the left and right of the pivot element.
Merge Sort is also a sorting algorithm. The algorithm divides the array into two halves, recursively sorts them, and finally merges the two sorted halves.
Closest Pair of Points The problem is to find the closest pair of points in a set of points in the x-y plane. The problem can be solved in O(n^2) time by calculating the distances of every pair of points and comparing the distances to find the minimum. The Divide and Conquer algorithm solves the problem in O(N log N) time.
Strassen’s Algorithm is an efficient algorithm to multiply two matrices. A simple method to multiply two matrices needs 3 nested loops and is O(n^3). Strassen’s algorithm multiplies two matrices in O(n^2.8974) time.
Cooley–Tukey Fast Fourier Transform (FFT) algorithm is the most common algorithm for FFT. It is a divide and conquer algorithm which works in O(N log N) time.
Karatsuba algorithm for fast multiplication does the multiplication of two n-digit numbers in at most
single-digit multiplications in general (and exactly when n is a power of 2). It is, therefore, faster than the classical algorithm, which requires n2 single-digit products. If n = 210 = 1024, in particular, the exact counts are 310 = 59, 049 and (210)2 = 1, 048, 576, respectively.
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 the 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 the 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)
}
Recurrence Relation for DAC algorithm : This is a recurrence relation for the 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 the maximum where we will see that only two elements are left and then we can easily use 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
Javascript
// C++ code to demonstrate Divide and// Conquer Algorithm#include<iostream># include<iostream>using namespace std; // function to find the maximum no.// in a given array.int DAC_Max(int arr[], int index, int l){    int max;    if(index >= l - 2)    {        if(arr[index] > arr[index + 1])          return arr[index];        else          return arr[index + 1];    }     max = DAC_Max(arr, index + 1, l);       if(arr[index] > max)       return arr[index];    else       return max;} // Function to find the minimum no.// in a given arrayint DAC_Min(int arr[], int index, int l){    int min;    if(index >= l - 2)    {        if(arr[index] < arr[index + 1])          return arr[index];        else          return arr[index + 1];    }         min = DAC_Min(arr, index + 1, l);      if(arr[index] < min)       return arr[index];    else       return min;} // Driver codeint main(){    int arr[] = {120, 34, 54, 32, 58, 11, 90};    int n = sizeof(arr) / sizeof(arr[0]);    int max, min;    max = DAC_Max(arr, 0, n);    min = DAC_Min(arr, 0, n);    cout << "Maximum: " << max << endl;    cout << "Minimum: " << min << endl;    return 0;} // This code is contributed by probinsah.
// C code to demonstrate Divide and// Conquer Algorithm#include <stdio.h>int DAC_Max(int a[], int index, int l);int DAC_Min(int a[], int index, int l); // function to find the maximum no.// in a given array.int DAC_Max(int a[], int index, int l){    int max;    if (index >= l - 2) {        if (a[index] > a[index + 1])            return a[index];        else            return a[index + 1];    }     // logic to find the Maximum element    // in the given array.    max = DAC_Max(a, index + 1, l);     if (a[index] > max)        return a[index];    else        return max;} // Function to find the minimum no.// in a given array.int DAC_Min(int a[], int index, int l){    int min;    if (index >= l - 2) {        if (a[index] < a[index + 1])            return a[index];        else            return a[index + 1];    }     // Logic to find the Minimum element    // in the given array.    min = DAC_Min(a, index + 1, l);     if (a[index] < min)        return a[index];    else        return min;} // Driver Codeint main(){    // Defining the variables    int min, max, N;     // Initializing the array    int a[7] = { 70, 250, 50, 80, 140, 12, 14 };     // recursion - DAC_Max function called    max = DAC_Max(a, 0, 7);     // recursion - DAC_Max function called    min = DAC_Min(a, 0, 7);    printf("The minimum number in a given array is : %d\n", min);    printf("The maximum number in a given array is : %d", max);    return 0;} // This code is contributed by Ashish Rana (GFG Team)
// Java code to demonstrate Divide and// Conquer Algorithmclass GFG{ // Function to find the maximum no.// in a given array.static int DAC_Max(int a[], int index, int l){    int max;         if (index >= l - 2)    {        if (a[index] > a[index + 1])            return a[index];        else            return a[index + 1];    }     // Logic to find the Maximum element    // in the given array.    max = DAC_Max(a, index + 1, l);     if (a[index] > max)        return a[index];    else        return max;} // Function to find the minimum no.// in a given array.static int DAC_Min(int a[], int index, int l){    int min;    if (index >= l - 2)    {        if (a[index] < a[index + 1])            return a[index];        else            return a[index + 1];    }     // Logic to find the Minimum element    // in the given array.    min = DAC_Min(a, index + 1, l);     if (a[index] < min)        return a[index];    else        return min;} // Driver Codepublic static void main(String[] args){         // Defining the variables    int min, max;     // Initializing the array    int a[] = { 70, 250, 50, 80, 140, 12, 14 };     // Recursion - DAC_Max function called    max = DAC_Max(a, 0, 7);     // Recursion - DAC_Max function called    min = DAC_Min(a, 0, 7);         System.out.printf("The minimum number in " +                      "a given array is : %d\n", min);    System.out.printf("The maximum number in " +                      "a given array is : %d", max);}} // This code is contributed by Princi Singh
// C# code to demonstrate Divide and// Conquer Algorithmusing System;class GFG{ // Function to find the maximum no.// in a given array.static int DAC_Max(int []a, int index, int l){    int max;         if (index >= l - 2)    {        if (a[index] > a[index + 1])            return a[index];        else            return a[index + 1];    }     // Logic to find the Maximum element    // in the given array.    max = DAC_Max(a, index + 1, l);     if (a[index] > max)        return a[index];    else        return max;} // Function to find the minimum no.// in a given array.static int DAC_Min(int []a, int index, int l){    int min;    if (index >= l - 2)    {        if (a[index] < a[index + 1])            return a[index];        else            return a[index + 1];    }     // Logic to find the Minimum element    // in the given array.    min = DAC_Min(a, index + 1, l);     if (a[index] < min)        return a[index];    else        return min;} // Driver Codepublic static void Main(String[] args){         // Defining the variables    int min, max;     // Initializing the array    int []a = {70, 250, 50, 80, 140, 12, 14};     // Recursion - DAC_Max function called    max = DAC_Max(a, 0, 7);     // Recursion - DAC_Max function called    min = DAC_Min(a, 0, 7);         Console.Write("The minimum number in " +                      "a given array is : {0}\n", min);    Console.Write("The maximum number in " +                      "a given array is : {0}", max);}} // This code contributed by shikhasingrajput
# Python3 code to demonstrate Divide and# Conquer Algorithm # Function to find the maximum no.# in a given array.def DAC_Max(a, index, l):    max = -1;     if (index >= l - 2):        if (a[index] > a[index + 1]):            return a[index];        else:            return a[index + 1];     # Logic to find the Maximum element    # in the given array.    max = DAC_Max(a, index + 1, l);     if (a[index] > max):        return a[index];    else:        return max; # Function to find the minimum no.# in a given array.def DAC_Min(a, index, l):    min = 0;    if (index >= l - 2):        if (a[index] < a[index + 1]):            return a[index];        else:            return a[index + 1];     # Logic to find the Minimum element    # in the given array.    min = DAC_Min(a, index + 1, l);     if (a[index] < min):        return a[index];    else:        return min; # Driver Codeif __name__ == '__main__':       # Defining the variables    min, max = 0, -1;     # Initializing the array    a = [70, 250, 50, 80, 140, 12, 14];     # Recursion - DAC_Max function called    max = DAC_Max(a, 0, 7);     # Recursion - DAC_Max function called    min = DAC_Min(a, 0, 7);    print("The minimum number in a given array is : ", min);    print("The maximum number in a given array is : ", max); # This code is contributed by 29AjayKumar
<script>// Javascript code to demonstrate Divide and// Conquer Algorithm // Function to find the maximum no.// in a given array.function DAC_Max(a,index,l){    let max;          if (index >= l - 2)    {        if (a[index] > a[index + 1])            return a[index];        else            return a[index + 1];    }      // Logic to find the Maximum element    // in the given array.    max = DAC_Max(a, index + 1, l);      if (a[index] > max)        return a[index];    else        return max;} // Function to find the minimum no.// in a given array.function DAC_Min(a,index,l){    let min;    if (index >= l - 2)    {        if (a[index] < a[index + 1])            return a[index];        else            return a[index + 1];    }      // Logic to find the Minimum element    // in the given array.    min = DAC_Min(a, index + 1, l);      if (a[index] < min)        return a[index];    else        return min;} // Driver Codelet min, max;let a=[70, 250, 50, 80, 140, 12, 14]; // Recursion - DAC_Max function calledmax = DAC_Max(a, 0, 7); // Recursion - DAC_Max function calledmin = DAC_Min(a, 0, 7); document.write("The minimum number in " +                  "a given array is : ", min+"<br>");document.write("The maximum number in " +                  "a given array is : "+ max+"<br>");  // This code is contributed by rag2127</script>
Maximum: 120
Minimum: 11
Divide and Conquer (D & C) vs Dynamic Programming (DP) Both paradigms (D & C and DP) divide the given problem into subproblems and solve subproblems. How do 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 (See this for details).
    T(n) = aT(n/b) + f(n),
    
    where,
        n = size of input
        a = number of subproblems in the recursion
        n/b = size of each subproblem. All subproblems are assumed to have the same size.
        f(n) = cost of the work done outside the recursive call, which includes the cost of dividing the problem and cost of merging the solutions

Advantages of Divide and Conquer Algorithm:
The difficult problem can be solved easily.
It divides the entire problem into subproblems thus it can be solved parallelly ensuring multiprocessing
Efficiently uses cache memory without occupying much space
Reduces time complexity of the problem
Disadvantages of Divide and Conquer Algorithm:
It involves recursion which is sometimes slow
Efficiency depends on the implementation of logic
It may crash the system if the recursion is performed rigorously
References Algorithms by Sanjoy Dasgupta, Christos Papadimitriou, Umesh Vazirani Introduction to Algorithms by Clifford Stein, Thomas H. Cormen, Charles E. Leiserson, Ronald L. http://en.wikipedia.org/wiki/Karatsuba_algorithmPlease write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Ashish_rana
princi singh
shikhasingrajput
29AjayKumar
probinsah
random604naseeb
Aniket Pai
rag2127
chikki81166
guptavivek0503
aditiyadav20102001
Divide and Conquer
Divide and Conquer
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.