Divide and Conquer Approach
Divide and Conquer Approach
Introduction
• The technique of diving large problems into smaller subproblems, solving subproblems and
combining them to get solution of original large problem.
• Divide and Conquer is a recursive problem-solving
approach which break a problem into smaller
subproblems.
1. Divide: This involves dividing the problem into some sub
problem.
2. Conquer: Sub problems are solved independently.
3. Combine: The Sub problem combine so that we will get
find problem solution.
Applications
Many computer science problems are effectively solved using divide and conquer.
• Finding exponential of the number
• Multiplying large numbers.
• Multiplying matrices.
• Sorting elements (Quick sort and Merge Sort )
• Searching elements from the list (Binary Search)
• Discreate Fourier Transform.
• Max-Min Problem.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 1
Analysis of Algorithm Divide and Conquer Approach
Master Method
➢ Time Complexity :-
n
T(n) = aT (b) + f(n)
Binary Search
0 1 2 3 4 5 6 7 8 9
Search 23 2 5 8 12 16 23 38 56 72 91
L=0 1 2 3 M=4 5 6 7 8 H=9
23 > 16
Take 2nd half
2 5 8 12 16 23 38 56 72 91
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 2
Analysis of Algorithm Divide and Conquer Approach
Algorithm
Merge Sort
• Merge Sort Uses divide and Conquer approach.
• In Merge Sort, we divide array into two halves, sort the two halves recursively, and then merge the
sorted halves.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 3
Analysis of Algorithm Divide and Conquer Approach
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 4
Analysis of Algorithm Divide and Conquer Approach
Algorithm MERGE_SORT(A,B)
// Description: Sort elements of array A using Merge sort
// Input: Array of size n, low ← 1 and high ← n
// Output: Sorted array B
if low < high then Recursively sort first sub list
mid ← floor (low + high) / 2
MERGE_SORT(A, low, mid) Recursively sort second sub list
MERGE_SORT(A, mid+1, high)
COMBINE(A, low, mid, high) // merge two sorted sublists
end
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 5
Analysis of Algorithm Divide and Conquer Approach
Procedure COMBINE performs a two way merge to produce a sorted array of size n from two
sorted arrays of size n/2. The subroutine works as follow:
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 6
Analysis of Algorithm Divide and Conquer Approach
Quick Sort
• It is divided and conquer based approach.
• It select one element as a pivot and moves pivot to correct location.
• Fixing the pivot to correct location divides the list into two sub list.
• Each sub lists is solved recursively.
Procedure:
- Scan array from left to right until an element greater than pivot is found.
- Scan array from right to left until an element equal or smaller than pivot is found.
✓ If low ≥ high, then exchange A[pivot] and A[high], which will split the list into two sub-
lists. Solve them recursively.
• It takes less number of multiplication compare to this traditional way of matrix multiplication.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 9