Divide and Conquer
Divide and Conquer
The basic idea is to recursively divide the problem into smaller subproblems
until they become simple enough to be solved directly.
Once the solutions to the subproblems are obtained, they are then
combined to produce the overall solution.
Working of Divide and Conquer Algorithm
Divide and Conquer Algorithm can be divided into three steps:
Divide
Conquer
Merge
1. Divide:
Break down the original problem into smaller subproblems.
Each subproblem should represent a part of the overall problem.
The goal is to divide the problem until no further division is possible.
2. Conquer:
Solve each of the smaller subproblems individually.
If a subproblem is small enough (often referred to as the “base case”), we solve it
directly without further recursion.
The goal is to find solutions for these subproblems independently.
3. Merge:
Combine the sub-problems to get the final solution of the whole problem.
Once the smaller subproblems are solved, we recursively combine their solutions to
get the solution of larger problem.
The goal is to formulate a solution for the original problem by merging the results
from the subproblems.
Applications of Divide and Conquer Algorithm
Binary Search is a search-based algorithm that follows the divide and conquers
approach and can be used for searching a target element within a sorted array.
Quicksort is a sorting algorithm that 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.
Karatsuba algorithm for fast multiplication does the multiplication of two binary
strings in O(n1.59) where n is the length of binary string.
Applications of Divide and Conquer Algorithm
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.
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.
Advantages of Divide and Conquer Algorithm
Solving difficult problems: Divide and conquer technique is a tool for solving difficult
problems conceptually. e.g. Tower of Hanoi puzzle. It requires a way of breaking the
problem into sub-problems, and solving all of them as an individual cases and then
combining sub- problems to the original problem.
Algorithm efficiency: The divide-and-conquer algorithm often helps in the discovery
of efficient algorithms. It is the key to algorithms like Quick Sort and Merge Sort, and
fast Fourier transforms.
Parallelism: Normally Divide and Conquer algorithms are used in multi-processor
machines having shared-memory systems where the communication of data
between processors does not need to be planned in advance, because distinct sub-
problems can be executed on different processors.
Memory access: These algorithms naturally make an efficient use of memory caches.
Since the subproblems are small enough to be solved in cache without using the
main memory that is slower one. Any algorithm that uses cache efficiently is called
cache oblivious.
Disadvantages of Divide and Conquer Algorithm
Overhead: The process of dividing the problem into subproblems and then combining
the solutions can require additional time and resources. This overhead can be
significant for problems that are already relatively small or that have a simple
solution.
Complexity: Dividing a problem into smaller subproblems can increase the complexity
of the overall solution. This is particularly true when the subproblems are
interdependent and must be solved in a specific order.
Memory limitations: When working with large data sets, the memory requirements
for storing the intermediate results of the subproblems can become a limiting factor.
CONTROL ABSTRACTION OR GENERAL METHOD FOR DIVIDE AND CONQUER
ALGORITHM
DAC(P)
{
if( small (P) )
{
S(P);
}
else
{
divide P into P1, P2----------Pn;
apply DAC (P1) , DAC (P2) -----------DAC(Pn);
combine(DAC (P1) , DAC (P2) ------------DAC(Pn));
}
}
Binary Search Algorithm
Searching:
Searching is a process of finding a particular element among several given elements.
The search is successful if the required element is found.
Otherwise, the search is unsuccessful.
Searching Algorithms:
Searching Algorithms are a family of algorithms used for the purpose of searching.
The searching of an element in the given array may be carried out in the following
two ways-
Binary Search:
Binary Search is one of the fastest searching algorithms.
It is used for finding the location of an element in a linear array.
It works on the principle of divide and conquer technique.
Binary Search Algorithm can be applied only on Sorted arrays.
Binary search algorithm is being used to search an element ‘item’ in this linear array.
If search ends in success, it sets loc to the index of the element otherwise it sets loc
to -1.
Variables beg and end keeps track of the index of the first and last element of the
array or sub array in which the element is being searched at that instant.
Variable mid keeps track of the index of the middle element of that array or sub array
in which the element is being searched at that instant.
Explanation
Binary Search Algorithm searches an element by comparing it with the middle
most element of the array.
Then, following three cases are possible-
Case-01
If the element being searched is found to be the middle most element, its index is
returned.
Case-02
If the element being searched is found to be greater than the middle most
element, then its search is further continued in the right sub array of the middle
most element.
Case-03
If the element being searched is found to be smaller than the middle most
element, then its search is further continued in the left sub array of the middle
most element.
This iteration keeps on repeating on the sub arrays until the desired element is
found or size of the sub array reduces to zero.
Time Complexity Analysis
In each iteration or in each recursive call, the search gets reduced to half of the
array.
So for n elements in the array, there are log2n iterations or recursive calls.
Thus,
Step-01:
To begin with, we take beg=0 and end=6.
We compute location of the middle element as-
Mid = (beg + end) / 2
= (0 + 6) / 2
=3
Here, a[mid] = a[3] = 20 ≠ 15 and beg < end.
So, we start next iteration.
Step-02:
Since a[mid] = 20 > 15, so we take end = mid – 1 = 3 – 1 = 2 whereas beg remains unchanged.
We compute location of the middle element as-
Step-03:
Since a[mid] = 10 < 15, so we take beg = mid + 1 = 1 + 1 = 2 whereas end remains unchanged.
We compute location of the middle element as-
It eliminates half of the list from further searching by using the result of each
comparison.
It indicates whether the element being searched is before or after the current
position in the list. This information is used to narrow the search.
For large lists of data, it works significantly better than linear search.