Upload
vikash-dhal
View
72
Download
0
Embed Size (px)
Citation preview
Implementation Of Strassens's Algorithm
#include<stdio.h>
void main()
{
int i,j, r1,c1, r2, c2, a[50][50], b[50][50], c[50][50], p1,p2,p3,p4,p5,p6,p7;
printf("nxn matrix using Strassen's multiplication");
printf("\nEnter row size of matrix A:\t");
scanf("%d", &r1);
printf("Enter column size of matrix A:\t");
scanf("%d", &c1);
printf("\nEnter row size of matrix B:\t");
scanf("%d", &r2);
printf("Enter column size of matrix B:\t");
scanf("%d", &c2);
if(c1!=r2)
{
printf("Invalid matrix size! Exiting...\n");
return ;
}
printf("Enter elements for matrix A:\n");
for(i=1;i<=r1;i++)
{
for(j=1;j<=c1;j++)
{
scanf("%d", &a[i][j]);
}
}
printf("\nEnter elements of matrix B:\n");
for(i=1;i<=r2;i++)
{
for(j=1;j<=c2;j++)
{
scanf("%d", &b[i][j]);
}
}
printf("MATRIX A:\n");
for(i=1;i<=r1;i++)
{
printf("\n");
for(j=1;j<=c1;j++)
{
printf("%d\t", a[i][j]);
}
}
printf("\nMATRIX B:\n");
for(i=1;i<=r2;i++)
{
printf("\n");
for(j=1;j<=c2;j++)
{
printf("%d\t", b[i][j]);
}
}
p1= (a[1][1]+a[2][2])*(b[1][1]+b[2][2]);
p2= (a[2][1]+a[2][2])*b[1][1];
p3= a[1][1]*(b[1][2]-b[2][2]);
p4= a[2][2]*(b[2][1]-b[1][1]);
p5= (a[1][1]+a[1][2])*b[2][2];
p6= (a[2][1]-a[1][1])*(b[1][1]+b[1][2]);
p7= (a[1][2]-a[2][2])*(b[2][1]+b[2][2]);
c[1][1]=p1+p4-p5+p7;
c[1][2]=p3+p5;
c[2][1]=p2+p4;
c[2][2]=p1+p3-p2+p6;
printf("\nThe matrix C is:\n");
for(i=1;i<=r1;i++)
{
printf("\n");
for(j=1;j<=c2;j++)
{ printf("%d\t", c[i][j]);
}
}
}
Output
Strassen's Multiplication Algorithm
P1 = (A11+ A22)(B11+B22)
P2 = (A21 + A22) * B11
P3 = A11 * (B12 - B22)
P4 = A22 * (B21 - B11)
P5 = (A11 + A12) * B22
P6 = (A21 - A11) * (B11 + B12)
P7 = (A12 - A22) * (B21 + B22)
C11 = P1 + P4 - P5 + P7
C12 = P3 + P5
C21 = P2 + P4
C22 = P1 + P3 - P2 + P6
Where C is :
Introduction:
Strassens's Multiplication
The Strassen’s method of matrix multiplication is a typical divide and conquer algorithm. We’ve
seen so far some divide and conquer algorithms like merge sort and the Karatsuba’s fast
multiplication of large numbers. However let’s get again on what’s behind the divide and
conquer approach.
Unlike the dynamic programming where we “expand” the solutions of sub-problems in order to
get the final solution, here we are talking more on joining sub-solutions together. These solutions
of some sub-problems of the general problem are equal and their merge is somehow well
defined.