Algorithm:
Step 1 :
Function to Add Matrices (add):
Iterate through each element of matrices A and B.
Add corresponding elements and store the result in the corresponding element of matrix C.
Step 2:
Function to Subtract Matrices (subtract):
Iterate through each element of matrices A and B.
Subtract corresponding elements and store the result in the corresponding element of matrix C.
Step 3
Function to Multiply Matrices (strassen):
If the size of the matrices is 1x1, perform a simple multiplication and store the result in the
corresponding element of matrix C.
Otherwise, perform the following steps for each quadrant of the matrices:
Split matrices A and B into four quadrants.
Compute seven products (P1 to P7) using recursive calls to strassen and specific additions and
subtractions.
Use the products to compute four quadrants of the resulting matrix C.
The recursive calls continue until matrices of size 1x1 are reached.
Step 4:
Function to Print a Matrix (printMatrix):
Iterate through each element of the matrix and print its value.
Print a newline after each row.
Step 5:
main Function:
Define the size of the matrices (n).
Initialize example matrices A and B.
Create a matrix C to store the result.
Call the strassen function to perform matrix multiplication.
Print the original matrices (A and B) and the result matrix (C).
Code:
#include <stdio.h>
// Function to add two matrices
void add(int n, int A[n][n], int B[n][n], int C[n][n]) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
C[i][j] = A[i][j] + B[i][j];
// Function to subtract two matrices
void subtract(int n, int A[n][n], int B[n][n], int C[n][n]) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
C[i][j] = A[i][j] - B[i][j];
// Function to multiply two matrices using Strassen's algorithm
void strassen(int n, int A[n][n], int B[n][n], int C[n][n]) {
if (n == 1) {
C[0][0] = A[0][0] * B[0][0];
return;
// Split matrices into quadrants
int newSize = n / 2;
int A11[newSize][newSize], A12[newSize][newSize], A21[newSize][newSize], A22[newSize]
[newSize];
int B11[newSize][newSize], B12[newSize][newSize], B21[newSize][newSize], B22[newSize]
[newSize];
int C11[newSize][newSize], C12[newSize][newSize], C21[newSize][newSize], C22[newSize]
[newSize];
int P1[newSize][newSize], P2[newSize][newSize], P3[newSize][newSize], P4[newSize][newSize];
int P5[newSize][newSize], P6[newSize][newSize], P7[newSize][newSize];
int temp1[newSize][newSize], temp2[newSize][newSize];
// Partition input matrices
for (int i = 0; i < newSize; i++)
for (int j = 0; j < newSize; j++) {
A11[i][j] = A[i][j];
A12[i][j] = A[i][j + newSize];
A21[i][j] = A[i + newSize][j];
A22[i][j] = A[i + newSize][j + newSize];
B11[i][j] = B[i][j];
B12[i][j] = B[i][j + newSize];
B21[i][j] = B[i + newSize][j];
B22[i][j] = B[i + newSize][j + newSize];
// Compute products and subproducts
add(newSize, A11, A22, temp1);
add(newSize, B11, B22, temp2);
strassen(newSize, temp1, temp2, P1);
add(newSize, A21, A22, temp1);
strassen(newSize, temp1, B11, P2);
subtract(newSize, B12, B22, temp1);
strassen(newSize, A11, temp1, P3);
subtract(newSize, B21, B11, temp1);
strassen(newSize, A22, temp1, P4);
add(newSize, A11, A12, temp1);
strassen(newSize, temp1, B22, P5);
subtract(newSize, A21, A11, temp1);
add(newSize, B11, B12, temp2);
strassen(newSize, temp1, temp2, P6);
subtract(newSize, A12, A22, temp1);
add(newSize, B21, B22, temp2);
strassen(newSize, temp1, temp2, P7);
// Compute C11, C12, C21, C22
add(newSize, P1, P4, temp1);
subtract(newSize, temp1, P5, temp2);
add(newSize, temp2, P7, C11);
add(newSize, P3, P5, C12);
add(newSize, P2, P4, C21);
subtract(newSize, P1, P2, temp1);
add(newSize, temp1, P3, temp2);
add(newSize, temp2, P6, C22);
// Copy results back to C
for (int i = 0; i < newSize; i++)
for (int j = 0; j < newSize; j++) {
C[i][j] = C11[i][j];
C[i][j + newSize] = C12[i][j];
C[i + newSize][j] = C21[i][j];
C[i + newSize][j + newSize] = C22[i][j];
// Function to print a matrix
void printMatrix(int n, int mat[n][n]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
printf("%d\t", mat[i][j]);
printf("\n");
int main() {
int n = 4; // Matrix size (should be a power of 2)
// Example matrices A and B
int A[4][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
int B[4][4] = {
{17, 18, 19, 20},
{21, 22, 23, 24},
{25, 26, 27, 28},
{29, 30, 31, 32}
};
// Resultant matrix C
int C[4][4];
// Perform matrix multiplication using Strassen's algorithm
strassen(n, A, B, C);
// Print the matrices
printf("Matrix A:\n");
printMatrix(n, A);
printf("\nMatrix B:\n");
printMatrix(n, B);
printf("\nMatrix C (Result of A * B using Strassen's algorithm):\n");
printMatrix(n, C);
return 0;
Output:
Matrix A:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Matrix B:
17 18 19 20
21 22 23 24
25 26 27 28
29 30 31 32
Matrix C (Result of A * B using Strassen's algorithm):
250 260 270 280
618 644 670 696
986 1028 1070 1112
1354 1412 1470 1528
Process returned 0 (0x0) execution time : 0.011 s