0% found this document useful (0 votes)
10 views5 pages

Experiment 4

sdbfb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Experiment 4

sdbfb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment 4

Aim-: Write a program for multiplication of two matrices using strassen's matrix multiplication
algorithm
Code-:
#include <stdio.h>
#include <stdlib.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 implement 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;
}

int mid = n / 2;
int A11[mid][mid], A12[mid][mid], A21[mid][mid], A22[mid][mid];
int B11[mid][mid], B12[mid][mid], B21[mid][mid], B22[mid][mid];
int C11[mid][mid], C12[mid][mid], C21[mid][mid], C22[mid][mid];
int M1[mid][mid], M2[mid][mid], M3[mid][mid], M4[mid][mid], M5[mid][mid], M6[mid][mid],
M7[mid][mid];
int temp1[mid][mid], temp2[mid][mid];

// Dividing matrices A and B into 4 sub-


matrices for (int i = 0; i < mid; i++) {
for (int j = 0; j < mid; j++)
{ A11[i][j] = A[i][j];
A12[i][j] = A[i][j + mid];
A21[i][j] = A[i + mid][j];
A22[i][j] = A[i + mid][j + mid];

B11[i][j] = B[i][j];
B12[i][j] = B[i][j + mid];
B21[i][j] = B[i + mid][j];
B22[i][j] = B[i + mid][j + mid];
}
}

// Calculating M1 to M7
add(mid, A11, A22, temp1);
add(mid, B11, B22, temp2);
strassen(mid, temp1, temp2, M1);

add(mid, A21, A22, temp1);


strassen(mid, temp1, B11, M2);

subtract(mid, B12, B22,


temp2); strassen(mid, A11,
temp2, M3);

subtract(mid, B21, B11,


temp2); strassen(mid, A22,
temp2, M4);
add(mid, A11, A12, temp1);
strassen(mid, temp1, B22, M5);

subtract(mid, A21, A11,


temp1); add(mid, B11, B12,
temp2);
strassen(mid, temp1, temp2, M6);

subtract(mid, A12, A22,


temp1); add(mid, B21, B22,
temp2);
strassen(mid, temp1, temp2, M7);

// Calculating C11, C12, C21, C22


add(mid, M1, M4, temp1);
subtract(mid, temp1, M5,
temp2); add(mid, temp2, M7,
C11);

add(mid, M3, M5, C12);

add(mid, M2, M4, C21);

add(mid, M1, M3, temp1);


subtract(mid, temp1, M2,
temp2); add(mid, temp2, M6,
C22);

// Combining 4 sub-matrices into one result matrix C


for (int i = 0; i < mid; i++) {
for (int j = 0; j < mid; j++)
{ C[i][j] = C11[i][j];
C[i][j + mid] = C12[i][j];
C[i + mid][j] = C21[i][j];
C[i + mid][j + mid] = C22[i][j];
}
}
}

int main() {
int n = 4; // Size of the matrix (must be a power of 2)
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}};
int C[4][4];

strassen(n, A, B, C);

printf("Result matrix C:\n");


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
{ printf("%d ", C[i]
[j]);
}
printf("\n");
}

return 0;
}
Output-:

You might also like