Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Analysis of Algorithm Divide and Conquer Approach

Chapter-2. 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.

General Method (Control Abstraction)


• Divide and conquer approach works in three stages.
1. Recursively divide the problem into smaller subproblems.
2. Subproblems are solved independently.
3. Combine solutions of subproblems in-order to derive solution of the original big-problem.
• Each subproblem in divide and conquer are independent and hence subsystem may be solved
independent times.

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)

• n is the size of the problem


• a is number of subproblems in the recursion
• n/b is the size of each sub problem
• F(n) is cost of the work done other than recursive calls like
• Cost of dividing the problem
• Cost of merging solution

Explain Binary Search using divide and Conquer

• Searching is the problem of finding element from given set of data.


• Binary search uses divide and conquer approach.
• Algorithm divides the list into two halves and check if element to be searched is on upper or lower
half of the array.
• If element is found in middle then algorithm returns otherwise it continues.

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

0 1 2 3 4 L=5 6 M=7 8 H=9


23 > 56
Take 1st half
2 5 8 12 16 23 38 56 72 91

0 1 2 3 4 L=5, M=5 H=6 7 8 9


Found 23
Return 5
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

Algorithm BINARY-SEARCH(A, Key)


//Description: Perform Binary Search on Array A.
//Input: Sorted Array A of size n and Key to be Searched
//Output: Data found or not found.
Start1
End n
if start <= end
middle = floor((start+end)/2)
if A[middle]==x
return middle
if A[middle]>x
return BINARY-SEARCH(A, start, middle-1, x)
if A[middle]<x
return BINARY-SEARCH(A, middle+1, end, x)
return FALSE // in case, element is not in the array

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

void mergesort(int a[], in i, int j) {


int mid;
if (i < j) {
mid = (i + j)/2;
mergesort(a, i, mid); //left recurssion
mergesort(a, mid+1, j); //right recurssion
merge (a, i, mid, mid+1, j); //merging of two sorted sub-array
}

void merge(int a[ ],int i1,int j1,int i2,int j2)


{
int temp[50]; //array used for merging
int i,j,k;
i=i1; //beginning of the first list
j-i2; //beginning of the second list
while(i<=j1 && j<=j2) //while elements in both lists
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
while(i<=j1) //copy remaining elements of the first list
temp[k++] = a[i++];
while(j <= j2) //copy remaining elements of the sec
temp[k++] = a[j++];
//Transfer elements from temp[ ] back to a[ ]
for(i = i1, j = 0; i <= j2; i++, j++)
a[i]=temp[j];

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 for merge sort is described below.

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:

COMBINE (A, low, mid, high)


l1 ← mid – lwo + 1 // size of 1st array
l2 ← high – mid // size of 2nd array
for i ← 1 to l1 do
LEFT [i] ← A [low + i – 1] // copy 1st sub list in array
LEFT
end
for j ← 1 to l2 do
RIGHT [i] ← A [mid + j] // copy 2nd sub list in array
RIGHT
end
// insert sentinel symbol at the end of each array
LEFT [l1 + 1] ← ∞
RIGHT [l2 + 1] ← ∞

// Start two-way merge process


i ← 1, j ← 1 Two-way merge

for k ← low to high do // perform 2-way merge on LEFT & RIGHT


if LEFT[i] ≤ RIGHT[j] then
//smaller element is in LEFT sub list, so copy it in B
B[k] ← LEFT[i]
i←i+1
else
// smaller element is in RIGHT sub list, so copy it in B
B
B[k] ← RIGHT[i]
j←j+1
end
end

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.

- After finding such elements,

✓ If low < high, then exchange A[low] and A[high].

✓ If low ≥ high, then exchange A[pivot] and A[high], which will split the list into two sub-
lists. Solve them recursively.

Algorithm for quick sort is shown below:

Algorithm QUICKSORT(A, low, high)


// Description: Sort array A using Quick sort
// Input: Unsorted array A of size n, low = 0, high = n – 1
// Output: Sorted array A
if low ≤ high then Find pivot index to split the list

q ← PARTITION (a, low, high)


Sort left sub list
QUICKSORT(A, low, q – 1)
Sort right sub list
QUICKSORT(A, q + 1, high)
end

The pseudo code of PARTITION subroutine is given below:

PARTITION (A, low, High)

x ← A[high] // x is pivot element i.e. last element of list


i ← low – 1
for j ← low to high – 1 do
if A[j] ≤ x then
i←i+1
swap (A[i], A[j])
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 7
Analysis of Algorithm Divide and Conquer Approach
end
end
swap (A[i + 1], A[High])
return (i + 1) // Correct index of pivot after sorting

Min- Max Problem


• It is used to find min and max element from given array.
• In traditional approach the maximum and minimum element can be found by comparing each
element. This approach is simple but it require n – 1 comparison.
• Using divide and conquer approach, we can reduce the number of comparison.
• If a1 is the only element in array, a1 is maximum and minimum.
• If the array contains only two element a1 and a2 then the single comparison between two elements
can decide minimum and maximum two elements.
• If there are more than two elements, algorithm divides the array from middle and crate two
subproblems.
• Both subproblems are treated as an independent problem and same recursive process is applied.
• The division continues until subproblem size becomes one or two.
• After solving two subproblems, their minimum and maximum numbers are compared two build the
solution of large problem.
• This process continues in bottom up fashion to build solution of parent problem.

Algorithm DC_MAXMIN (A, low, High)


//Description: Find minimum and maximum element from array using
divide and conquer approach
//Input: Array A of length n, and indces low = 0 and high = n – 1
//Output: (min, max) variables holding minimum and maximum element of
array
if n == 1 then
return (A[1], A[1])
else if n == 2 then
if A[1] < A[2] then
return (A[1], A[2])
else
return (A[2], A[1])
else
mid ← (low + high)/2
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 8
Analysis of Algorithm Divide and Conquer Approach
[LMin, LMax] = DC_MAXMIN(A, low, mid)
[RMin, RMax] = DC_MAXMIN(A, mid + 1, high)

if LMax > RMax then //Combine solution


max ← LMax
else
max ← RMax
if LMin > RMin then //Combine solution
min ← LMin
else
min ← RMin
else
return (min, max)
end

Strassen’s matrix multiplication


• The strassens algorithm, named after Volker Strassen, is algorithm for matrix multiplication.
• It is faster than the standard matrix multiplication algorithm.

• 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

You might also like