Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

STRASSEN MATRIX MULTIPLICATION

Submitted by

AMEEN MUHAMMAD RA2211003020286


RAGUL RA2211003020275
LAKSHMAN RA2211003020263

Under the guidance of

Mrs. K. Subha
(Assistant Professor, Department of Computer Science andEngineering)

21CSC204J/DESIGN AND ANALYSIS OF ALGORITHM


PROJECT REPORT
IV SEMESTER/II YEAR
COMPUTER SCIENCE AND ENGINEERING
COLLEGE OF ENGINEERING AND TECHNOLOGY

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY


RAMAPURAM, CHENNAI - 600089
MAY 2024
ABSTRACT

Matrix multiplication is a fundamental operation in numerous computational tasks


across various domains, ranging from scientific computing to machine learning. The
Strassen algorithm offers an alternative approach to traditional methods, promising
improved efficiency through its divide-and-conquer strategy. In this project report, we
delve into the design and analysis of the Strassen matrix multiplication algorithm
within the context of the course "Design and Analysis of Algorithms." We explore the
theoretical underpinnings of the algorithm, its implementation, and its practical
implications. Through performance evaluation and complexity analysis, we assess the
algorithm's efficacy and discuss its potential applications. This report aims to provide
a comprehensive understanding of the Strassen algorithm's significance in the realm
of algorithm design and analysis.

PROBLEM STATEMENT

Matrix multiplication stands as a cornerstone in various computational endeavors,


spanning disciplines from computer graphics to numerical simulations. The classical
approach to matrix multiplication, while conceptually straightforward, exhibits a
cubic time complexity concerning the size of the matrices involved. This cubic
growth poses significant scalability challenges, especially in scenarios where matrices
are large, such as in high-dimensional data processing or scientific simulations.

The Strassen algorithm presents itself as an intriguing alternative to conventional


matrix multiplication methods. By employing a divide-and-conquer strategy, the
Strassen algorithm promises potentially improved efficiency by reducing the number
of scalar multiplications required. However, while the theoretical basis of the Strassen
algorithm suggests improved performance, its practical viability and applicability in
real-world scenarios remain subjects of exploration and analysis.

Hence, the primary objective of this project is to delve deeply into the Strassen
algorithm within the framework of the "Design and Analysis of Algorithms" course.
This endeavor involves understanding the theoretical underpinnings of the Strassen
algorithm, implementing it in code, and rigorously evaluating its performance
characteristics. Through comprehensive performance analysis and complexity
assessment, we aim to ascertain the practical advantages and limitations of the
Strassen algorithm compared to traditional matrix multiplication methods.

Furthermore, this project seeks to explore the potential applications of the Strassen
algorithm in various domains, including but not limited to scientific computing, image
processing, and machine learning. By investigating its efficacy in real-world contexts
and addressing scalability challenges inherent in conventional matrix multiplication,
we aim to contribute to the ongoing discourse in algorithmic optimization and
computational efficiency.

ALGORITHM

1. Input Matrices: Given two input matrices AA and BB, each of size n×nn×n.

2. Base Case Check: If nn is a small value (e.g., n≤2n≤2), perform standard matrix
multiplication using the conventional method.

3. Matrix Partitioning: Divide each input matrix AA and BB into four submatrices
of equal size, each of size n/2×n/2n/2×n/2. For example:

where A11A11​ , A12A12​ , A21A21​ , A22A22​ , B11B11​ , B12B12​ ,


B21B21​ , and B22B22​ are n/2×n/2n/2×n/2 submatrices.

4. Matrix Operations:
Compute seven intermediate matrices:
M1​ =(A11​ +A22​ )×(B11​ +B22​ )
M2​ =(A21​ +A22​ )×B11​
M3​ =A11​ ×(B12​ −B22​ )
M4​ =A22​ ×(B21​ −B11​ )
M5​ =(A11​ +A12​ )×B22​
M6​ =(A21​ −A11​ )×(B11​ +B12​ )
M7​ =(A12​ −A22​ )×(B21​ +B22​ )

5. Recursive Multiplication:
Using the intermediate matrices M1,M2,…,M7M1​ ,M2​ ,…,M7​ , compute
the resulting submatrices of the product matrix CC:
C11​ =M1​ +M4​ −M5​ +M7​
C12​ =M3​ +M5​
C21​ =M2​ +M4​
C22​ =M1​ −M2​ +M3​ +M6​

6. Output: The resulting matrix CC is the product of matrices AA and BB.

7. Merge Results: Combine the resulting submatrices C11C11​ , C12C12​ ,


C21C21​ , and C22C22​ to obtain the final product matrix CC.

8. Return: Return the resulting matrix CC as the product of matrices AA and BB.

PESUDO CODE

function strassen_matrix_multiply(A, B)
if size(A) == 1 // Base case: A is a 1x1 matrix
return A * B

// Divide matrices A and B into submatrices


n = size(A)
A11, A12, A21, A22 = divide_matrix(A)
B11, B12, B21, B22 = divide_matrix(B)

// Compute intermediate matrices


M1 = strassen_matrix_multiply(A11 + A22, B11 + B22)
M2 = strassen_matrix_multiply(A21 + A22, B11)
M3 = strassen_matrix_multiply(A11, B12 - B22)
M4 = strassen_matrix_multiply(A22, B21 - B11)
M5 = strassen_matrix_multiply(A11 + A12, B22)
M6 = strassen_matrix_multiply(A21 - A11, B11 + B12)
M7 = strassen_matrix_multiply(A12 - A22, B21 + B22)

// Compute resulting submatrices


C11 = M1 + M4 - M5 + M7
C12 = M3 + M5
C21 = M2 + M4
C22 = M1 - M2 + M3 + M6

// Merge resulting submatrices into the final product matrix


C = merge_matrices(C11, C12, C21, C22)

return C

function divide_matrix(M)
n = size(M)
mid = n / 2
return M[1:mid, 1:mid], M[1:mid, mid+1:n], M[mid+1:n, 1:mid], M[mid+1:n,
mid+1:n]

function merge_matrices(C11, C12, C21, C22)


n = size(C11)
C = new matrix of size n*2 x n*2
C[1:n, 1:n] = C11
C[1:n, n+1:2n] = C12
C[n+1:2n, 1:n] = C21
C[n+1:2n, n+1:2n] = C22
return C
PROGRAM

#include<stdio.h>
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}
};
printf("The first matrix is: ");
for(i = 0; i < 2; i++) {
printf("\n");
for(j = 0; j < 2; j++)
printf("%d\t", x[i][j]);
}
printf("\nThe second matrix is: ");
for(i = 0; i < 2; i++) {
printf("\n");
for(j = 0; j < 2; j++)
printf("%d\t", 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][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;
printf("\nProduct achieved using Strassen's algorithm: ");
for(i = 0; i < 2 ; i++) {
printf("\n");
for(j = 0; j < 2; j++)
printf("%d\t", z[i][j]);
}
return 0;
}

OUTPUT
COMPLEXITY ANALYSIS

Time complexity :

The time complexity of the Strassen algorithm can be analyzed using the master
theorem. Let T(n) be the time complexity of multiplying two n×n matrices using the
Strassen algorithm.

In the algorithm, we perform seven recursive matrix multiplications of size n/2×n/2,


along with additional matrix additions and subtractions, each taking O(n2)O(n2) time.

Therefore, the recurrence relation for the time complexity is:


T(n) = 7T(n/2) + O(n2)

Using the master theorem, the time complexity of the Strassen algorithm is
O(n^log27), which is approximately O(n2.81). This complexity is slightly better than
the conventional matrix multiplication algorithm, which has a time complexity of
O(n3).
Space complexity :

The space complexity of the Strassen algorithm depends on the recursion depth and
the space required for intermediate matrices.

At each recursive call, the algorithm divides the matrices into submatrices, resulting
in a recursion depth of log2n.

Additionally, the algorithm requires space to store intermediate matrices


M1,M2,…,M7, each of size n/2 × n/2.

Therefore, the space complexity of the Strassen algorithm is O(n2logn) due to the
recursion stack and the space required for intermediate matrices.
SUMMARY

The Strassen matrix multiplication algorithm employs a divide-and-conquer strategy


to enhance computational efficiency in multiplying matrices. By recursively dividing
input matrices into smaller submatrices and utilizing a set of seven intermediate
matrix multiplications, along with additions and subtractions, it achieves a time
complexity of O(nlog27), slightly outperforming the O(n3)complexity of conventional
methods. However, this efficiency comes at the cost of increased space complexity,
with a requirement of O(n2logn) space due to recursion and storage of intermediate
results. While theoretically advantageous, practical applicability depends on factors
such as matrix size and implementation considerations. Nonetheless, the Strassen
algorithm finds utility across various domains, including scientific simulations, image
processing, and machine learning, showcasing its relevance in modern computational
tasks.

You might also like