Skip to content

Commit 8674dcf

Browse files
Strassen's Algo Matrix Multiplication in C
1 parent f1e5a6b commit 8674dcf

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

Arrays/Strassen's_Algo.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include<stdio.h>
2+
int main(){
3+
int mat1[2][2], mat2[2][2], mat3[2][2], i, j;
4+
int a, b, c, d , e, f, g;
5+
6+
printf("Enter the elements of first matrix:\n"); //matrix 1
7+
for(i = 0;i < 2; i++)
8+
{
9+
for(j = 0;j < 2; j++)
10+
{
11+
scanf("%d", &mat1[i][j]);
12+
}
13+
}
14+
15+
printf("Enter the elements of second matrix:\n"); //matrix 2
16+
for(i = 0; i < 2; i++)
17+
{
18+
for(j = 0;j < 2; j++)
19+
{
20+
scanf("%d", &mat2[i][j]);
21+
}
22+
}
23+
//Displaying
24+
printf("The Matrix 1:\n"); //Displaying matrix 1 elements
25+
for(i = 0; i < 2; i++)
26+
{
27+
for(j = 0; j < 2; j++)
28+
{
29+
printf("%d\t", mat1[i][j]);
30+
}
31+
printf("\n");
32+
}
33+
34+
printf("The Matrix 2:\n"); //Displayimg matrix 2 elements
35+
for(i = 0;i < 2; i++)
36+
{
37+
for(j = 0;j < 2; j++)
38+
{
39+
printf("%d \t", mat2[i][j]);
40+
}
41+
printf("\n");
42+
}
43+
//reduced eight times Time Complexity to Seven Times
44+
a= (mat1[0][0] + mat1[1][1]) * (mat2[0][0] + mat2[1][1]);
45+
b= (mat1[1][0] + mat1[1][1]) * mat2[0][0];
46+
c= mat1[0][0] * (mat2[0][1] - mat2[1][1]);
47+
d= mat1[1][1] * (mat2[1][0] - mat2[0][0]);
48+
e= (mat1[0][0] + mat1[0][1]) * mat2[1][1];
49+
f= (mat1[1][0] - mat1[0][0]) * (mat2[0][0]+mat2[0][1]);
50+
g= (mat1[0][1] - mat1[1][1]) * (mat2[1][0]+mat2[1][1]);
51+
52+
mat3[0][0] = a + d- e + g;
53+
mat3[0][1] = c + e;
54+
mat3[1][0] = b + d;
55+
mat3[1][1] = a - b + c + f;
56+
57+
printf("Strassen's algorithm : Matrix Multiplication\n"); //Resultant matrix after applying Strassen's Algo
58+
for(i = 0; i < 2 ; i++)
59+
{
60+
for(j = 0;j < 2; j++)
61+
{
62+
printf("%d\t", mat3[i][j]); //displaying resultant matrix
63+
}
64+
printf("\n");
65+
}
66+
return 0;
67+
}

0 commit comments

Comments
 (0)