Explain Strassen's Matrix Multiplication: Implementation and Advantages
By upGrad
Updated on Mar 10, 2025 | 9 min read | 6.8k views
Share:
For working professionals
For fresh graduates
More
By upGrad
Updated on Mar 10, 2025 | 9 min read | 6.8k views
Share:
Table of Contents
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.
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:
Strassen’s algorithm computes 7 products as follows:
The final matrix C=AB is computed as:
Example: 2x2 Matrix Multiplication using Strassen's Algorithm
Given Matrices:
We divide the matrices into submatrices:
Compute the 7 products:
Now, compute the four submatrices of the resulting matrix CC:
The resulting matrix C is:
This method reduces the number of multiplications, optimizing performance.
Also Read: Time and Space Complexity in Machine Learning Explained
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:
This implementation of Strassen’s algorithm efficiently multiplies matrices with fewer recursive multiplications, improving performance for larger matrices.
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.
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 learning, neural 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.
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:
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.
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.
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
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
Top Resources