Skip to main content

What is Divide And Conquer Technique

Divide and Conquer is a programming technique which makes the program more efficient to write. And this technique work on the concept of recursion to solve a problem step by step. Generally this technique work in three parts:- Divide:-  Divide the problem into some subproblem. Conquer:-  Conquer the subproblem by calling recursively until subproblem solved. Combine:-  (Optional Step) Combine the subproblem solution. So, that we will get the final problem Solution.  When the subproblems are large enough to solve recursively, we call the recursive case. Once the subproblem becomes small enough that we no longer recursive, we say that the recursion "bottom out" and that we have gotten down to the base case.                          Application of Divide and Conquer Quick Sort Strassen's algorithm for matrix multiplication Merge Sort Counting inversions Binary Search Finding Min and ...

What is Divide And Conquer Technique

Divide and Conquer is a programming technique which makes the program more efficient to write.
And this technique work on the concept of recursion to solve a problem step by step.
Generally this technique work in three parts:-
  1. Divide:- Divide the problem into some subproblem.
  2. Conquer:- Conquer the subproblem by calling recursively until subproblem solved.
  3. Combine:- (Optional Step) Combine the subproblem solution. So, that we will get the final problem Solution. 
When the subproblems are large enough to solve recursively, we call the recursive case. Once the subproblem becomes small enough that we no longer recursive, we say that the recursion "bottom out" and that we have gotten down to the base case.
                        

Application of Divide and Conquer

  1. Quick Sort
  2. Strassen's algorithm for matrix multiplication
  3. Merge Sort
  4. Counting inversions
  5. Binary Search
  6. Finding Min and max

Divide and Conquer Abstract Algorithm

DAC(a,i,j)
{        
          //small(a,i,j) represent the smallest subproblem which can't be further divided
          if(small(a,i,j))  
                //solution(a,i,j) return some constant value
               return(solution(a,i,j));
           else
            {
                 //Divide(a,i,j) represent the dividing of large problem into small
                 mid=Divide(a,i,j);
                 b=DAC(a,i,mid);
                 c=DAC(a,mid+1,j);
                 //combine(b,c) represnt merging up of small subproblem
                 d=combine(b,c);
                  return d;
              }
}
                            

About Complexity of Abstract Divide & Conquer Algorithm

small(a,i,j)            having O(1) time complexity
solution(a,i,j)        having O(1) time complexity
Divide(a,i,j)          having some constant C1 time complexity
b=DAC(a,i,mid);  having T(n/2) time complexity
c=DAC(a,i,mid);  also having T(n/2) time complexity
combine(b,c)         having some constant C2 time complexity

The total time complexity of Divide and Conquer
      T(n)=O(1)   if n is small
      T(n)=C1+T(n/2)+T(n/2)+C2   if n is large

assuming C=C1+C2
Then,
         T(n)=2T(n/2)+C     //for two equal subproblem cost

And for 'a' equal subproblem of size 'b'
           T(n)=aT(n/b)+C     //It also depict that Divide and Conquer can be solved by Master's Theorem

Important Points about Divide & Conquer

  1. Function calling occur in Preorder
  2. Execution of function happen in Postorder














Comments

Post a Comment

Popular posts from this blog

Sorting numbers using LinkList in C Language

#include<stdio.h> #include<conio.h> //creating  a structure for linked list typedef struct node {     int data;     struct node* next; }node;     //rename whole structure // function for creating nodes of linked list node * createlinkedlist(int n); //return address void display(node * head); void sort1(node * head,int n); int main(){ int n=0; node* HEAD=NULL; node* HEAD1=NULL; printf("how many nodes want to add in a linklist "); scanf("%d",&n); HEAD=createlinkedlist(n); sort1(HEAD,n); display(HEAD); return 0; } node * createlinkedlist(int n) {     int i=0;     node * head=NULL;     node * temp=NULL;     node * p=NULL;     for(i=0;i<n;i++)     {         //creating unattached node         temp=(node*)malloc(sizeof(node));       ...

Reverse Words

Reverse Words Given a list of space separated words, reverse the order of the words. Each line of text contains L letters and W words. A line will only consist of letters and space characters. There will be exactly one space character between each pair of consecutive words. Input The first line of input gives the number of cases, N. N test cases follow. For each test case there will a line of letters and space characters indicating a list of space separated words. Spaces will not appear at the start or end of a line. import java.io.BufferedReader; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; import java.util.logging.Level; import java.util.logging.Logger; /**  *  * @author Rishabh  */ public class Reversestring  {     public static void main(String[] args) ...