0% found this document useful (0 votes)
16 views21 pages

Divide and Conquer

The Divide and Conquer algorithm is a fundamental technique in computer science that breaks a problem into smaller subproblems, solves them individually, and combines their solutions for the final result. It consists of three main steps: Divide, Conquer, and Merge, and is applied in various algorithms such as Binary Search, Quicksort, and Merge Sort. While it offers advantages like improved efficiency and parallelism, it also has drawbacks including overhead, complexity, and challenges in implementation.

Uploaded by

heyprettyaera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views21 pages

Divide and Conquer

The Divide and Conquer algorithm is a fundamental technique in computer science that breaks a problem into smaller subproblems, solves them individually, and combines their solutions for the final result. It consists of three main steps: Divide, Conquer, and Merge, and is applied in various algorithms such as Binary Search, Quicksort, and Merge Sort. While it offers advantages like improved efficiency and parallelism, it also has drawbacks including overhead, complexity, and challenges in implementation.

Uploaded by

heyprettyaera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Divide and Conquer Algorithm

 Divide and Conquer is a fundamental algorithmic technique in computer


science, where a problem is divided into smaller, more manageable sub-
problems, solved individually, and then combined to form the final solution.

 Divide and conquer algorithm approach not only simplifies complex


problems but also enhances computational efficiency.

 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.

 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.
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.

Difficulty of implementation: Some problems are difficult to divide into smaller


subproblems or require a complex algorithm to do so. In these cases, it can be
challenging to implement a divide and conquer solution.

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.

So, the elements must be arranged in


 Either ascending order if the elements are numbers.
 Or dictionary order if the elements are strings.

 To apply binary search on an unsorted array.


 First, sort the array using some sorting technique.
 Then, use binary search algorithm.
Binary Search Algorithm

Consider there is a linear array ‘a’ of size ‘n’.

 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

Binary Search time complexity analysis is done below-

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,

Time Complexity of Binary Search Algorithm is O(log2n).

Here, n is the number of elements in the sorted linear array.

This time complexity of binary search remains unchanged irrespective of the


element position even if it is not present in the array.
Binary Search Example-
Consider-
We are given the following sorted linear array.
Element 15 has to be searched in it using Binary Search Algorithm.

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-

Mid = (beg + end) / 2


= (0 + 2) / 2
=1
Here, a[mid] = a[1] = 10 ≠ 15 and beg < end.
So, we start next iteration.

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-

Mid = (beg + end) / 2


= (2 + 2) / 2
=2

Here, a[mid] = a[2] = 15 which matches to the element being searched.


So, our search terminates in success and index 2 is returned.
Binary Search Algorithm Advantages

 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.

Binary Search Algorithm Disadvantages

 It employs recursive approach which requires more stack space.


 Programming binary search algorithm is error prone and difficult.
 The interaction of binary search with memory hierarchy i.e. caching is poor.
(because of its random access nature)

You might also like