View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
  • Home
  • Blog
  • General
  • Explain Strassen's Matrix Multiplication: Implementation and Advantages

Explain Strassen's Matrix Multiplication: Implementation and Advantages

By upGrad

Updated on Mar 10, 2025 | 9 min read | 6.8k views

Share:

Traditional matrix multiplication, with its O(n³) complexity, struggles with inefficiencies when working with large datasets, particularly in machine learning and high-performance computing. 

Strassen’s algorithm addresses this by reducing the number of multiplications. It offers significant performance improvements, especially when handling large matrices in computationally intensive tasks. 

This blog will explore Strassen's matrix multiplication, its implementation, and how it outperforms traditional methods, helping you optimize computational tasks.

Explain Strassen's Matrix Multiplication Algorithm

Strassen's Matrix Multiplication algorithm, developed by Volker Strassen in 1969, provides a more efficient approach to matrix multiplication compared to the traditional method. 

It uses a divide-and-conquer strategy, reducing the number of recursive multiplications from 8 to 7, thereby improving performance. 

While the traditional matrix multiplication approach has a time complexity of O(n^3), Strassen’s algorithm achieves O(n^log7) ≈ O(n^2.81), making it significantly faster for large matrices.

Strassen’s Matrix Multiplication Formula:

Given two matrices A and B, and dividing them into submatrices:

A = [ A 11 A 12 A 21 A 22 ] , B = [ B 11 B 12 B 21 B 22 ]

Strassen’s algorithm computes 7 products as follows:

M 1 = A 11 + A 22 B 11 + B 22
M 2 = A 21 + A 22 B 11
M 3 = A 11 B 12 - B 22
M 4 = A 22 B 21 - B 11
M 5 = A 11 + A 12 B 22
M 6 = A 21 - A 11 B 11 + B 12
M 7 = A 12 - A 22 B 21 + B 22

The final matrix C=AB is computed as:

C 11 = M 1 + M 4 - M 5 + M 7
C 12 = M 3 + M 5
C 21 = M 2 + M 4
C 22 = M 1 + M 3 - M 2 + M 6

Example: 2x2 Matrix Multiplication using Strassen's Algorithm

Given Matrices:

A = 1 2 3 4 ;   B = 5 6 7 8

We divide the matrices into submatrices:

A 11 = 1 ,   A 12 = 2 ,   A 21 = 3 ,   A 22 = 4
B 11 = 5 ,   B 12 = 6 ,   B 21 = 7 ,   B 22 = 8

Placement Assistance

Executive PG Program11 Months
background

O.P.Jindal Global University

MBA from O.P.Jindal Global University

Live Case Studies and Projects

Master's Degree12 Months

Compute the 7 products:

M 1 = A 11 + A 22 B 11 + B 22 = 1 + 4 ( 5 + 8 ) = 5 × 13 = 65
M 2 = A 21 + A 22 B 11 = ( 3 + 4 ) × 5 = 7 × 5 = 35
M 3 = A 11 B 12 - B 22 = 1 × ( 6 - 8 ) = 1 × ( - 2 ) =   - 2
M 4 = A 22 B 21 - B 11 = 4 × ( 7 - 5 ) = 4 × 2 = 8
M 5 = A 11 + A 12 B 22 = ( 1 + 2 ) × 8 = 3 × 8 = 24
M 6 = A 21 - A 11 B 11 + B 12 = ( 3 - 1 ) × ( 5 + 6 ) = 2 × 11 = 22
M 7 = A 12 - A 22 B 21 + B 22 = ( 2 - 4 ) × ( 7 + 8 ) = ( - 2 ) × 15 =   - 30

Now, compute the four submatrices of the resulting matrix CC:

C 11 = M 1 + M 4 - M 5 + M 7 = 65 + 8 24 30 = 19
C 21 = M 2 + M 4 = 2 + 24 = 22
C 21 = M 2 + M 4 = 35 + 8 = 43
C 22 = M 1 + M 3 - M 2 + M 6 = 65 2 35 + 22 = 50

The resulting matrix C is:

C = 19 22 43 50

This method reduces the number of multiplications, optimizing performance.

Also Read: Time and Space Complexity in Machine Learning Explained

Strassen's Algorithm Implementation With Examples

Strassen's algorithm speeds up matrix multiplication, particularly for large matrices. Let’s explore how the algorithm divides the matrix into submatrices and recursively calculated.

Mechanism:

1. Dividing Matrices into Submatrices: Divide the matrices A and B into 4 submatrices each (quadrants).
2. Recursive Decomposition: Recursively apply the same procedure for smaller submatrices until the base case is reached (2x2 matrices).
3. Constructing the Resultant Matrix: Combine the computed products to obtain the final result.
4. Base Case Handling: For small matrices (2x2), use traditional matrix multiplication directly.
5. Optimizations: Strassen’s method reduces computational overhead by cutting down multiplication steps.

Code Example (Python Implementation):

import numpy as np

def strassen(A, B):
    # Get the size of the matrix (assumed to be square)
    n = A.shape[0]

    # Base case: If the matrix is 2x2, directly use standard matrix multiplication
    if n == 2:
        return np.dot(A, B)  # np.dot computes the dot product of A and B, i.e., regular matrix multiplication
    
    # Divide the matrices A and B into four quadrants (submatrices)
    mid = n // 2  # Find the midpoint of the matrix for division
    A11, A12, A21, A22 = A[:mid, :mid], A[:mid, mid:], A[mid:, :mid], A[mid:, mid:]
    B11, B12, B21, B22 = B[:mid, :mid], B[:mid, mid:], B[mid:, :mid], B[mid:, mid:]
    
    # Compute the 7 intermediate products (M1 to M7) using Strassen's formulas
    M1 = strassen(A11 + A22, B11 + B22)  # (A11 + A22) * (B11 + B22)
    M2 = strassen(A21 + A22, B11)        # (A21 + A22) * B11
    M3 = strassen(A11, B12 - B22)        # A11 * (B12 - B22)
    M4 = strassen(A22, B21 - B11)        # A22 * (B21 - B11)
    M5 = strassen(A11 + A12, B22)        # (A11 + A12) * B22
    M6 = strassen(A21 - A11, B11 + B12)  # (A21 - A11) * (B11 + B12)
    M7 = strassen(A12 - A22, B21 + B22)  # (A12 - A22) * (B21 + B22)
    
    # Compute the final quadrants (C11, C12, C21, C22) of the resultant matrix C
    C11 = M1 + M4 - M5 + M7  # Combine results to get top-left quadrant
    C12 = M3 + M5            # Combine results to get top-right quadrant
    C21 = M2 + M4            # Combine results to get bottom-left quadrant
    C22 = M1 + M3 - M2 + M6  # Combine results to get bottom-right quadrant

    # Combine the quadrants into the final matrix C
    C = np.zeros((n, n))  # Initialize an empty matrix C of the same size as A and B
    C[:mid, :mid], C[:mid, mid:], C[mid:, :mid], C[mid:, mid:] = C11, C12, C21, C22  # Place the quadrants in the correct locations

    return C  # Return the final matrix after Strassen's multiplication

# Example matrices A and B
A = np.array([[1, 2], [3, 4]])  # Example 2x2 matrix A
B = np.array([[5, 6], [7, 8]])  # Example 2x2 matrix B

C = strassen(A, B)  # Call the strassen function to multiply A and B
print("Resultant Matrix C:")
print(C)  # Print the final result of the matrix multiplication

Explanation: This Strassen’s algorithm implementation efficiently multiplies matrices by reducing the number of multiplications from 8 to 7. 

Here’s how it works:

1. Base Case (2x2 matrix multiplication): If the matrix is of size 2x2, it directly multiplies the matrices using np.dot(A, B), which is the standard matrix multiplication function in NumPy. This avoids recursion for small matrices.

2. Matrix Division: The input matrices A and B are divided into 4 submatrices each. This is done using array slicing. For example, A[:mid, :mid] gets the top-left submatrix of A.

3. Recursive Computation (M1 to M7): We recursively compute the 7 products M1 to M7​ by applying Strassen’s formulas. These steps involve recursively breaking down the submatrices and performing matrix additions or subtractions.

4. Constructing the Resultant Matrix: The final matrix C is constructed by combining the 4 submatrices (C11, C12, C21, C22) into the resulting matrix.

Output Example:

Resultant Matrix C:
[[19. 22.]
 [43. 50.]]

Key NumPy Functions Explained:

  • np.dot(A, B): Computes the dot product of two arrays, which in the case of matrices, performs standard matrix multiplication.
  • Array slicing (A[:mid, :mid]): Divides the matrix into submatrices by selecting appropriate rows and columns.

This implementation of Strassen’s algorithm efficiently multiplies matrices with fewer recursive multiplications, improving performance for larger matrices.

You can further improve your understanding of Strassen’s matrix multiplication and other algorithms with upGrad’s online software engineering courses. Gain hands-on experience and master advanced computational techniques for real-world applications.

Also Read: Basic Fundamentals of Statistics for Data Science

After understanding Strassen's approach, let's compare it with other matrix multiplication methods to assess how it performs in relation to traditional and advanced algorithms.

Comparing Strassen's Algorithm with Other Matrix Multiplication Methods

Strassen's algorithm significantly improves efficiency by reducing the time complexity from O(n³) to O(n².81), making it ideal for large matrices. However, it is less effective for extremely small matrices or when memory usage is a concern due to its recursive nature. 

For very large matrices, algorithms like Coppersmith-Winograd are more efficient. Strassen's algorithm is commonly used in fields like machine learningneural networks, and physics simulations, where large-scale matrix operations are critical.  

Here's a comparative table to highlight the differences:

Matrix Multiplication Method

Time Complexity

Space Complexity

Practicality

Performance

Traditional (Naive) O(n³) O(n²) Simple to implement, but inefficient for large matrices Slower for large matrices
Strassen’s Algorithm O(n².81) O(n²) More complex, requires recursive decomposition Faster for large matrices
Coppersmith-Winograd O(n².376) O(n²) Highly efficient for large matrices, but complex and difficult to implement Best performance for very large matrices
Karatsuba Algorithm O(n^log3) O(n²) Practical for medium-sized matrices Faster than traditional, but less optimal than Strassen

Also Read: Time Complexity of Kruskal Algorithm: Data Structure, Example

Strassen’s algorithm implementation strikes a balance between efficiency and complexity, making it a good choice for medium to large-scale matrix multiplication tasks where time complexity is a critical factor.

Practical Applications of Strassen’s Algorithm

Strassen's algorithm is used in fields like machine learning and scientific simulations to optimize large-scale matrix operations. For example, TensorFlow uses it to speed up matrix multiplications in deep learning, significantly reducing computation time during model training. It’s also applied in physics simulations to accelerate complex system modeling.  

Some of the key applications are:

  • Scientific Simulations and High-Performance Computing: Used in simulations that require large matrix computations, where Strassen’s faster performance significantly reduces execution time.
  • Advancements in Computer Graphics and Image Processing: Improves rendering and processing efficiency in graphics, especially for operations like transformations and filtering.
  • Role in Machine Learning and Data Analytics: Accelerates training of machine learning models, particularly those involving large datasets and complex transformations like convolutional operations.
  • Utilization in Parallel and Distributed Computing: Strassen’s recursive structure fits well with parallel processing, enabling more efficient computations across multiple processors.
  • Enhancing Security in Cryptographic Systems: Matrix multiplication is often a part of encryption algorithms, and Strassen’s algorithm can improve the performance of cryptographic operations.

By improving matrix multiplication, Strassen’s algorithm implementation has become a fundamental tool in applications requiring high-performance computing and optimization.

Also Read: Cryptography in Cybersecurity: Definition, Types & Examples

Having compared Strassen’s algorithm implementation with other methods, let’s now look at its advantages and limitations to understand where it excels and where it may fall short.

Advantages and Limitations of Strassen's Algorithm

Although Strassen's algorithm is effective for large matrices, it becomes less efficient for small matrices (e.g., n < 32) due to higher overhead. The algorithm’s recursive decomposition can cause numerical instability from floating-point precision errors. 

Additionally, Strassen increases memory usage, making it less suitable for memory-constrained environments or when high precision is needed. For small matrices, traditional methods are often more efficient due to lower overhead. 

Here’s a table highlighting the benefits and drawbacks:

Benefits

Drawbacks

Enhanced Computational Efficiency: Reduces multiplication operations, making it faster for large matrices. High Memory Consumption: Requires extra memory for recursive submatrix storage.
Reduced Computational Complexity: Improves time complexity to O(n².81), faster than traditional methods. Challenges in Real-World Implementation: Complex to implement, especially for non-square or very large matrices.
Optimized Execution Speed: Processes large matrices significantly faster, ideal for high-performance computing. Issues with Numerical Precision: Prone to instability, particularly with matrices containing extreme values.
Mathematical Elegance and Innovation: Introduces a recursive method to reduce the number of multiplications. Limited Practical Use Cases: Not suitable for smaller matrices or scenarios where memory is limited.

Also Read: Demystifying Confusion Matrix in Machine Learning

Now that you’ve explored the benefits and drawbacks of Strassen’s algorithm implementation, upGrad’s courses can further help you deepen your understanding of advanced algorithms and computer science concepts with hands-on experience.

How upGrad Can Accelerate Your Computer Science Learning Journey?

upGrad, South Asia’s leading EdTech platform, offers in-depth courses that equip over 10M+ learners with essential skills in Python, machine learning, and algorithmic optimization. The courses cover detailed explanations, hands-on coding, and real-world applications across various fields.

Here are some relevant courses to enhance your learning journey:

You can also get personalized career counseling with upGrad to guide your career path, or visit your nearest upGrad center and start hands-on training today! 

Similar Reads:

Escalation Matrix: How To Design, Types, Process, How Does It Work
Confusion Matrix in R: How to Make & Calculate [With Examples] 
Linear Algebra for Machine Learning: Critical Concepts, Why Learn Before ML
Evaluation Metrics in Machine Learning: Top 10 Metrics You Should Know
A Complete Guide To Matrix Addition in Python
What are Data Structures & Algorithm

Frequently Asked Questions

1. What makes Strassen's algorithm a divide-and-conquer approach?

2. Why does Strassen's algorithm reduce the number of multiplications?

3. How does Strassen’s algorithm handle large matrices?

4. Does Strassen’s algorithm require more or less computational resources than traditional matrix multiplication?

5. Can Strassen’s algorithm be used in parallel processing?

6. How does Strassen’s algorithm impact real-time applications?

7. What are the challenges of implementing Strassen’s algorithm in high-precision calculations?

8. How does Strassen’s algorithm compare to the Karatsuba algorithm for matrix multiplication?

9. What are some modifications to Strassen's algorithm for improving its accuracy?

10. Is Strassen's algorithm suitable for all matrix sizes?

11. How does Strassen’s algorithm affect the performance of machine learning models with large datasets?

upGrad

475 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

Top Resources

Recommended Programs

PMI® | upGrad KnowledgeHut

PMI® | upGrad KnowledgeHut

Project Management Professional (PMP)® Certification

Guaranteed Exam Pass Study Plan

Certification

36 Hrs Live Expert-Led Training

Scaled Agile Inc.® | upGrad KnowledgeHut

Scaled Agile Inc.® | upGrad KnowledgeHut

Implementing SAFe® 6.0 with SPC Certification

1-Year Access to SA Community

Certification

32 Hrs Live Expert-Led Training

PeopleCert® | upGrad KnowledgeHut

PeopleCert® | upGrad KnowledgeHut

PRINCE2® Foundation and Practitioner Certification

Simulations, Mock Tests and More

Certification

32 Hrs Live Expert-Led Training