Divide and Conquer
Divide and Conquer
Divide and Conquer algorithm consists of a dispute using the following three steps.
Divide: Dividing the problem into two or more than two sub-problems that are
similar to the original problem but smaller in size.
Conquer: Solve the sub-problems recursively.
Combine: Combine these solutions to subproblems to create a solution to the
original problem.
Divide And Conquer
This technique can be divided into the following three parts:
1. Divide: This involves dividing the problem into smaller sub-problems.
2. Conquer: Solve sub-problems by calling recursively until solved.
3. Combine: Combine the sub-problems to get the final solution of the whole
problem.
If we expand out two more recursive steps, it looks like this:
Because divide-and-conquer creates at least two subproblems, a divide-and-conquer
algorithm makes multiple recursive calls.
Examples: The specific computer algorithms are based on the Divide & Conquer
approach:
Solution Idea: The naive solution for the problem do a linear search to check
whether element K is present or not. This will take O(n) time complexity. But if we use
the sorted property of the array, we can apply the divide and conquer approach to
solve it efficiently in O(log n) time complexity.
The basic idea of binary search is to divide the array equally and compare the value K
with the middle element. If A[mid] is greater than K then definitely K will not be
present in the right part, so we search value K in the left part. Similarly, if A[mid] is
less than K then we search value K in the right part. (Think!)
Solution Steps
Recursive Call : binarySearch(A[], l, r, k)
1. Divide: Calculate the middle index of the array. if(A[mid] == k),then return 1.
This is a case of a successful search.
2. Conquer: Recursively solve the smaller sub-problem
Time complexity is O(log n), which is much faster than O(n) algorithm of linear
search. Note: We can solve the above recurrence relation by recursion tree method or
master theorem. (How? Think!)
Can we use some hypothesis to analyze the time complexity of binary search?
Think about the recursive and iterative implementation of the binary search
algorithms. Also, compare the space complexity of both?
Prepare a list of the problems where we can apply the ideas similar to the binary
search for the solution.
Explore the idea of Ternary Search where we apply the divide and conquer
approach but we divide the given array into three parts. Why is Binary
Search preferred over Ternary Search?
Divide: Divide the n elements sequence into two equal size subsequences of
n/2 element each
Conquer: Sort the two sub-sequences recursively using merge sort.
Combine: Merge the two sorted subsequences to produce a single sorted
sequence.
Solution Steps
Recursive Call: mergeSort(A[], l, r)
So, the time complexity of the merge sort is O(nlog n). Note: We can solve the above
recurrence relation by recursion tree method or master theorem.
Usually, we solve a divide and conquer problems using only 2 subproblems. But
there are few cases where we use more than two subproblems for the
solution. (Think and explore!)
Several problems can be solved using the idea similar to the merge sort and
binary search.
This is a very good algorithm design strategy to learn about recursive problem
solving. Before understanding dynamic programming and backtracking, We
always suggest to understand this approach.
The correct base case is very important for correctness!
Subproblems are always independent in divide conquer algorithms because
every subproblem is working on the different parts of the given input (Think!)
Iterative Implementation: Divide-and-conquer algorithms can also be
implemented by a non-recursive algorithm. In this case, we need to store the
solution of smaller subproblems in a explicit stack data structure.
In recursive algorithms, the call stack is used which also takes the memory
which leads to an increase in space complexity of the algorithm.