Module-2-Devide&Conquer
Module-2-Devide&Conquer
DESIGN
Strategy
• Given a function on n inputs
– input splitted into k disjoint subsets
– yielding k subproblems.
• Solve subproblems
• Combine the subsolutions into a
solution
solution
Problem Problem
May 6, 2015 2:15 AM Algorithm Analysis and Design
Solving Subproblems
• Large Subproblems
– Solved by reapplication of divide and conquer.
– Subproblems same type as the original problem
implemented using recursive algorithm
• Smaller subproblems solved independently.
May 6, 2015 2:15 AM Algorithm Analysis and Design
Control Abstraction
Time Complexity
Summary
• When element comparisons are costlier dandc yields
a better algorithm.
• Dandc always willnot give better algorithm.
– Only a design technique that will guide to better designs.
• Constants should be specified, during comparisons if
relevant( when both has same order complexity).
May 6, 2015 2:15 AM Algorithm Analysis and Design
Merge Sort
• Divides list into two sub lists
• Sort sub lists separately
– Recursive invocation
• Merges the sub lists
25 11 18 17 13 45 28 30
May 6, 2015 2:15 AM Algorithm Analysis and Design
Algorithm Mergesort
Algorithm mergesort(low,high)
{ if (low<high) then
{ mid:= L (low+high)/2
megesort(low,mid);
mergesort(mid+1,high);
mege(low,mid,high);
}}
May 6, 2015 2:15 AM Algorithm Analysis and Design
Algorithm Merge
Algorithm merge(low,mid,high) if (h>mid) then
{h:=low;i:=low; j:=mid+1; for k=j to high do
while ((h<=mid) and (j<=high)) {b[i]=a[k];i=i+1;}
do else
{if (a[h]<=a[j]) then for k=h to mid do
{b[i]=a[h];h=h+1;} {b[i]=a[k];i = i+1;}
else for k=low to high do
{b[i]=a[j];j=j+1;} a[k]=b[k];
i=i+1;} }
May 6, 2015 2:15 AM Algorithm Analysis and Design
Complexity
T(n)= 2*T(n/2)+cn for n>1
a for n=1
• Unfolding recurrence
T(n) = 2 T(n/2) + cn
= 22 T(n/4) + 2cn
= 2k T(n/2k) + kcn
= 2k T(1) +kcn = an + cn log n
= O(n log n)
May 6, 2015 2:15 AM Algorithm Analysis and Design
Complexity
• Insertion sort for worst case
– 2 ε n j=n(n+1)/2-1=θ(N2).
• In the best case
– θ(N).
Quick Sort
• To avoid the complexity for merging
– division to subarrays made specially
• 2 main algorithms
– Quick Sort
– Partition
Working Principle
• Decides the partition element
• Divides into 2 subarrays based on partition
element
• Performs Recursive invocation to sort the
subarrays.
Partition
• Arranges the elements around the pivot
element
• All the elements less than pivot are moved to
left side (smaller indices) of the pivot
• All elements greater than the pivot are moved
to the right side of the pivot.
Working
• if the size of the array is n, partition requires
n+1 comparisons.
25 11 18 17 13 45 28 30
13 11 18 17 25 45 28 30
Algorithm Quicksort
Algorithm quicksort(p,q)
{ if (p<q) then
{j:=partition(a,p,q+1);
quicksort(p,j-1);
quicksort(j+1,q);}}
Algorithm Partition
algorithm partition(a,m,p) if (i<j) then
{v:=a[m]; i:=m; j:=p; {temp:=a[i];
repeat a[i]:=a[j];
{ repeat
a[j]:=temp;}
i:= i+1;
until (i>=j);
until (a[i]>=v);
a[m]:=a[j];
repeat
j:= j-1; a[j]:=v;
until (a[j]<=v); return j;}
May 6, 2015 2:15 AM Algorithm Analysis and Design
Complexity
• if the size of the array is n, partition requires
n+1 comparisons.
25 11 18 17 13 45 28 30
May 6, 2015 2:15 AM Algorithm Analysis and Design
= O(n2)
May 6, 2015 2:15 AM Algorithm Analysis and Design
Average Case
Ca(N)= n+1 + 1 ε k (Ca(k-1)+Ca(n-k))
Ca(N)= n+1 +1/n* 1 ε k (Ca(k-1)+Ca(n-k))
n*Ca(n)= n*(n+1) + 1 ε k (Ca(k-1)+Ca(n-k))
= n*(n+1) +2[ca(0)+Ca(1)+- - - + Ca(n-1)]
(n-1)*Ca(n-1) = n*(n-1) +2[ca(0)+Ca(1)+- - - + Ca(n-2)]
(n)*Ca(n)- (n-1)*Ca(n-1) = 2*n +2*Ca(n-1)
Ca (n) 2 + Ca(n-1)
=
n+1 n+1 n
Ca (n) 2 + 2 + Ca(n-2)
=
n+1 n+1 n n-1
= 3 ε n+1 (1/k) + Ca(1)/2
May 6, 2015 2:15 AM Algorithm Analysis and Design
Comparison
• Maximum recursion n-1
• Improvements :
– Selection of pivot element
• Sub arrays are approximately of equal size
– Reduce level of recursion
• Sort small sub array first
May 6, 2015 2:15 AM Algorithm Analysis and Design
Matrix Multiplication
For i:= 1 to m
For j:=1 to p
c[i,j]:=0;
For k:= 1 to n
c[i,j]:=c[i,j] + a[i,k]*b[k,j]
Complexity is O(mnp)
Complexity is O(n3) if both matrices are n x n
May 6, 2015 2:15 AM Algorithm Analysis and Design
[ [ [
A11
A21
A12
[
A22 X
B11
B21
[
B12
B22 =
C11
C21
C12]
C22]
[
C11=A11B11+ A12B21 a11 a12 a13 a14 b11 b12 b13 b14 c11 c12 c13 c14
a21 a22 a23 a24 b21 b22 b23 b24 c21 c22 c23 c24
C12=A11B12+ A12B22
a31 a32 a33 a34 b31 b32 b33 b34 c31 c32 c33 c34
C21=A21B11+ A22B21 a41 a42 a43 a44 b41 b42 b43 b44 c41 c42 c43 c44
C22=A21B12+ A22B22
May 6, 2015 2:15 AM Algorithm Analysis and Design
T(n)= 8 T(n/2)+cn2
= 8[8 T(n/22)+c(n/2)2 ]+cn2
= 82 T(n/22)+2cn2 +cn2
= 83 T(n/23)+22cn2 + 2cn2 + cn2
------------ -
= 8k T(n/2k)+2k-1cn2 + - -+2cn2 + cn2
= 8k* a +[2k-1]cn2 = a*(23 )k +[n-1]cn2=O(n3)
May 6, 2015 2:15 AM Algorithm Analysis and Design
Refinements
• Reformulate the equations for Cij so as to
have fewer matrix multiplications.
• Faster algorithms exist
– Clever divide-and-conquer recurrences.
• Mr Volker Strassen
– Fewer than 8 matrix maultiplications.
Algorithm Analysis and Design
Strassen’s Multiplication
• 7 submatrix multiplications
• 18 additions /subtractions of matrices.
• Principle
– 7 temporary sub matrices P,Q,R,S,T,U,V computed.
May 6, 2015 2:15 AM Algorithm Analysis and Design
Formulation
P=( A11 +A22 ) (B11+ B22)
Q=( A21 +A22 ) B11 C11=P+S-T+V
R= A11 (B12 - B22) C12= R+ T
S = A22 (B21 – B11)
C21=Q+S
T=( A11 +A12 ) B22
U=( A21 – A11 ) (B11+ B12) C22= P+R-Q+U
V=( A12 - A22 ) (B21+ B22)
• 7 multiplications and 6 additions and 4
subtractions.
May 6, 2015 2:15 AM Algorithm Analysis and Design
and b=b1b0 =
Time Complexity
No. of Multiplications= 3