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

ADA Madhav

The document outlines Experiment 4, which focuses on Strassen's matrix multiplication algorithm implemented in C++. It explains the theory behind the algorithm, provides pseudo code, and includes a program for multiplying 2x2 matrices, as well as a graph comparing the time complexities of Strassen's method and standard multiplication. The analysis concludes that Strassen's algorithm reduces time complexity from O(n^3) to O(n^2.81).

Uploaded by

Madhav Khanna
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)
4 views5 pages

ADA Madhav

The document outlines Experiment 4, which focuses on Strassen's matrix multiplication algorithm implemented in C++. It explains the theory behind the algorithm, provides pseudo code, and includes a program for multiplying 2x2 matrices, as well as a graph comparing the time complexities of Strassen's method and standard multiplication. The analysis concludes that Strassen's algorithm reduces time complexity from O(n^3) to O(n^2.81).

Uploaded by

Madhav Khanna
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: To perform Strassen’s matrix multiplication


Language used: C++, IDE for plotting graphs à Octave online.
Theory:
Strassen’s Matrix à it is a divide-and-conquer algorithm that reduces the multiplications required to
multiply two matrices. It divides the matrices into smaller sub-matrices and recursively calculates
seven products instead of the traditional eight.
Pseudo code:
Start: for(int i=0;
i<n;i++){
for(int j=0;j<n;j++){
C[i][j]=0;
for(int k=0;k<n;k++){ c[i]
[j] = A[i][k]*B[k][j]; }
}
}
Formulas required for Strassen’s matrix (A, B being the matrices to be multiplied and
C is the matrix that holds the result):
P = A11 * (B12 - B22)
Q = (A11 + A12) * B22
R = (A21 + A22) * B11
S = A22 * (B21 - B11)
T = (A11 + A22) * (B11 + B22)
U = (A12 - A22) * (B21 + B22)
V = (A11 - A21) * (B11 + B12)
C11 = P + S – T + V
C12 = R + T
C21 = Q + S
C22 = P + R – Q + V

Madhav Khanna
A2305222268
Program code (for 2X2 matrix multiplication):

#include<iostream>
using namespace std;
int main() { int
z[2][2]; int i, j;
int m1, m2, m3, m4 , m5, m6, m7;
int x[2][2] = { {12, 34},
{22, 10}
};
int y[2][2] = {
{3, 4},
{2, 1}
};
cout<<"The first matrix is: ";
for(i = 0; i < 2; i++)
{ cout<<endl; for(j = 0;
j < 2; j++) cout<<x[i]
[j]<<" ";
}
cout<<"\nThe second matrix is: ";
for(i = 0;i < 2; i++)
{ cout<<endl; for(j = 0;j <
2; j++) cout<<y[i][j]<<" ";
}

m1 = (x[0][0] + x[1][1]) * (y[0][0] + y[1][1]);


m2 = (x[1][0] + x[1][1]) * y[0][0]; m3 = x[0]
[0] * (y[0][1] - y[1][1]); m4 = x[1][1] * (y[1]
[0] - y[0][0]); m5 = (x[0][0] + x[0][1]) * y[1]

Madhav Khanna
A2305222268
[1]; m6 = (x[1][0] - x[0][0]) * (y[0][0]+y[0]
[1]); m7 = (x[0][1] - x[1][1]) * (y[1][0]+y[1]
[1]);

z[0][0] = m1 + m4- m5 + m7;


z[0][1] = m3 + m5; z[1][0] =
m2 + m4; z[1][1] = m1 - m2 +
m3 + m6;

cout<<"\nProduct achieved using Strassen's algorithm: ";


for(i = 0; i < 2 ; i++) { cout<<endl; for(j = 0; j < 2;
j++) cout<<z[i][j]<<" ";
}
return 0; }
OUTPUT:

Plotting the graph:


n = 2.^(1:10);

Madhav Khanna
A2305222268
strassen_complexity = n.^log2(7); standard_complexity
= n.^3;

figure; plot(n, strassen_complexity, 'b-',


'LineWidth', 2); hold on; plot(n,
standard_complexity, 'r--', 'LineWidth', 2); hold
off;

xlabel('Matrix Size (n x n)'); ylabel('Time Complexity'); title('Time Complexity


of Strassen''s vs Standard Matrix Multiplication'); legend('Strassen''s:
O(n^{log_2(7)})', 'Standard: O(n^3)', 'Location', 'NorthWest'); grid on;

Analysis:

Madhav Khanna
A2305222268
The recurrence relation for Strassen's algorithm à
As there are 7 products used in multiplying of matrices and we use divide and conquer
strategy for breaking down the matrices into multiples of 2 each time hence the time
complexity comes out to be: 7T(n/2) + Θ(n²).
Using the Master method, the time complexity comes becomes à O(n^log₂(7)), i.e O(n^2.81).
Using Strassen’s multiplication of matrix the time complexity is reduced to n^ 2.81 instead of
n^3 which is the time complexity for the traditional matrix multiplication.

Madhav Khanna
A2305222268

You might also like