Divide and Conquer Algorithm
Divide and Conquer Algorithm
Sushma Prajapati
Assistant Professor
CO Dept
CKPCET,surat
Email:sushma.prajapati@ckpcet.ac.in
Outline
● Introduction
● Recurrence and different methods to solve recurrence
● Problem Solving using divide and conquer algorithm
○ Multiplying large Integers Problem
○ Binary Search
○ Max-Min problem
○ Sorting (Merge Sort, Quick Sort)
○ Matrix Multiplication
○ Exponential
Introduction
● This technique can be divided into the
following three parts:
○ Divide: This involves dividing the problem
into some sub problem.
○ Conquer: Sub problem by calling recursively
until sub problem solved.
○ Combine: The Sub problem Solved so that we
will get find problem solution.
General Divide and Conquer Algorithm
Algorithm DAndC(P)
If Small(P) then
Return S(P);
Else
{
Devide P into smaller instances P1, P2, … Pk k≥ 1
Apply DAndC to each of these Sub-problems;
Return Combine(DAndC(P1), DAndC(P2),…, DAndC(Pk))
}
Generally time complexity of this problem is Time Complexity
given by recurrence relation T(n) = T(1) ; n=1
= aT(n/b) + f(n); n>1
Recurrence and different methods to solve
recurrence
Recurrence
In mathematics a recurrence is a function that is defined in terms of one or more base
cases, and itself , with smaller arguments
Examples:
Different methods to solve recurrence
● Back Substitution
● Recursion tree
● Master’s theorem
● Change variable
Back substitution method
● It is also known as iterative method or substitution method or iterative substitution
method
● It is a technique or procedure in computational mathematics used to solve a
recurrence relation that uses an initial guess to generate a sequence of improving
approximate solutions for a class of problems, in which the n-th approximation is
derived from the previous ones.
Back substitution method (Example)
Consider the algorithm:: Recurrence equation will be::
T (n) = 1 if n=0
Void fun(int n) = T (n-1) if n>1
{
if(n>0)
{ Wil solve it on board……….
printf (“%d”,n);
fun(n-1);
}
}
Master’s Theorem
Recursion tree method
● A recursion tree is a tree where each node represents the cost of a certain recursive
sub problem. Then you can sum up the numbers in each node to get the cost of the
entire algorithm.
Recursion tree method: Example
Recurrence: T (n) = 3T (n/4) + cn 2
Recursion tree method: Example (Contd…)
Recurrence: T (n) = 3T (n/4) + cn 2
Change variable method
● Sometimes, a little algebraic change can make an unknown recurrence similar to
one you have seen before.
T(2m) = 2T(2m/2) + m
5678
x 1234
7006652
17
Grade School Multiplicaton
45
x 63
135
2700
2835
18
Grade School Multiplication
45
Algorithm description (informal*): x 63
compute partial products (using multiplication
& “carries” for digit overflows), and add all 135
(properly shifted) partial products together
2700
This algorithm takes O(n^2) time.
2835
19
Multiplication of large integers
(divide-conquer recursive algorithm)
● a = a1a0 and b = b1b0
○ c=a*b
○ = (a110n/2 + a0) * (b110n/2 + b0)
● =(a1 * b1)10n + (a1 * b0 + a0 * b1)10n/2 + (a0 * b0)
where
where
c2 = a1 * b1
c0 = a0 * b0
T(n) = n1.585
Binary Search
Binary Search : Introduction
● 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.
● To apply binary search on an unsorted array,
○ First, sort the array using some sorting technique.
○ Then, use binary search algorithm.
Binary Search : Algorithm
● Iterative Version BinSearchI(A, n, key)
● Input Parameters
○ n – No of elements low=1;
○ Array – A[1:n] in high=n;
○ increasing order, n>0, While( low ≤ high)
○ key – element to be searched {
● Output mid = (low + high)/2 ;
If (key < A[mid]) then
○ If key is present in A it returns index of found
high= mid -1;
element
Else if If (key > A[mid]) then
○ If key is not present then return 0
low = mid + 1;
Else
Return mid;
}
Return 0;
Binary Search: Example
Binary Search : Time Analysis
The recurrence relation can be written as,
So, by applying the master’s theorem we can get the time complexity as,
ɵ(n)
Sorting(Merge Sort, Quick Sort)
Merge Sort : Introduction
● Sorting Problem: Sort a sequence of n elements into non-decreasing order.
● Divide: Divide the n-element sequence to be sorted into two subsequences of n/2
elements each
● Conquer: Sort the two subsequences recursively using merge sort.
● Combine: Merge the two sorted subsequences to produce the sorted answer.
Merge Sort : Algorithm
MERGE-SORT(A, p,r) Algorithm MERGE(A, p, q,r)
Sorts A[p . .r].
1 if p < r then Merges A[p . . q] and A[q + 1 . .r].
2 q ← b(p + 1)/2c
3 MERGE-SORT(A, p, q); MERGE-SORT(A, q + 1,r) 1 n1 ← q − p + 1; n2 ← r − q
4 MERGE(A, p, q,r) 2 create array L[1 . . n1 + 1] and R[1 . . n2 + 1]
3 for i ← 1 to n1 do L[i] ← A[p + i − 1]
4 for j ← 1 to n2 do R[ j] ← A[q + j]
5 L[n1 + 1] ← ∞; R[n2 + 1] ← ∞
6 i ← 1, j ← 1
7 for k ← p to r do
8 if L[i] <= R[ j] then A[k] ← L[ i]; i++
9 else A[k] ← R[ j]; j++
Merge Sort : Example
Merge Sort : Analysis
● Running time T(n) of Merge Sort:
● Divide: computing the middle takes θ(1)
● Conquer: solving 2 subproblems takes 2T(n/2)
● Combine: merging n elements takes θ(n)
● Total:
○ T(n) = (1) if n = 1
○ T(n) = 2T(n/2) + (n) if n > 1
● By applying master’s theorem we get complexity as
● T(n) =θ (n lg n)
Quick Sort : Introduction
● Divide: Partition (separate) the array A[p..r] into two (possibly empty) subarrays
A[p..q–1] and A[q+1..r].
○ Each element in A[p..q–1] < A[q].
○ A[q] < each element in A[q+1..r].
○ Index q is computed as part of the partitioning procedure.
● Conquer: Sort the two subarrays by recursive calls to quicksort.
● Combine: The subarrays are sorted in place – no work is needed to combine them.
Quick Sort : Algorithm
Algorithm QUICKSORT(A, p, r) PARTITION(A, p, r)
{ {
if p < r x = A[r]
{ i=p-1
q = PARTITION(A, p, r) for j = p to r - 1
QUICKSORT(A, p, q - 1) {
QUICKSORT(A, q + 1, r) if A[j] <= x
} {
} i=i+1
exchange A[i] with A[j]
}
}
exchange A[i + 1] with A[r]
return i + 1
Quick Sort :Partition Procedure
Select the last element A[r] in the subarray A[p..r] as the pivot – the element around
which to partition.
A[r] = pivot.
Quick Sort : Example(Partitioning)
Quick Sort : Example(Partitioning)
Quick Sort : Time Analysis
Worst Case : Unbalanced Partitioning
● The worst-case behavior for quicksort occurs
when the partitioning routine produces one
region with n - 1 elements and one with only l
element.
● Since partitioning costs (n) time and T(1) = θ
(1), the recurrence for the running time is
● T(n) = T(n - 1) + θ(n).
● Applying the substitution method Here, we
get time complexity as,
● θ(n2)
Best Case : Balanced Partitioning
If the partitioning procedure produces two regions
of size n/2, quicksort runs much faster. The
recurrence is then
θ(nlogn)
Better way??
Strassen’s Matrix Multiplication
● In the previous divide and conquer method,
the main component for high time
complexity is 8 recursive calls.
● Strassen’s method is similar to above simple
divide and conquer method in the sense that
this method also divide matrices to
sub-matrices of size N/2 x N/2 as shown in the
above diagram, but in Strassen’s method, the
four sub-matrices of result are calculated
using following formulae.
Strassen’s Matrix Multiplication: Time Analysis
Here 7 multiplications are required so, recurrence relation can be written as,