codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🧮

Data Structures & Algorithms

44 / 65 topics
40Divide and Conquer Basics41Merge Sort42Quick Sort43Binary Search Algorithm44Strassen's Matrix Multiplication
Tutorials/Data Structures & Algorithms/Strassen's Matrix Multiplication
🧮Data Structures & Algorithms

Strassen's Matrix Multiplication

Updated 2026-04-20
4 min read

Introduction

Matrix multiplication is a fundamental operation in linear algebra with numerous applications in computer graphics, machine learning, and scientific computing. The traditional method of multiplying two matrices involves three nested loops, resulting in a time complexity of \(O(n^3)\). However, Strassen's Matrix Multiplication algorithm reduces this complexity to approximately \(O(n^{2.807})\), making it more efficient for large matrices.

In this tutorial, we will explore the principles behind Strassen's algorithm, its implementation, and its advantages over traditional matrix multiplication methods.

Understanding Traditional Matrix Multiplication

Before diving into Strassen's algorithm, let's review how traditional matrix multiplication works. Given two matrices \(A\) and \(B\), where both are \(n \times n\) matrices, the product matrix \(C = A \times B\) is computed as follows:

\[ C[i][j] = \sum_{k=0}^{n-1} A[i][k] \times B[k][j] \]

This operation requires three nested loops, leading to a time complexity of \(O(n^3)\).

Strassen's Matrix Multiplication

Strassen's algorithm is based on the divide and conquer approach. It divides each matrix into four smaller submatrices and recursively computes their products using seven multiplications instead of the traditional eight.

Algorithm Steps

  1. Divide Matrices: Divide matrices \(A\) and \(B\) into four equal-sized submatrices.
  2. Compute Intermediate Products:
    • Compute seven intermediate products: \(P_1, P_2, \ldots, P_7\).
  3. Combine Results: Combine the intermediate products to form the final product matrix.

Mathematical Formulation

Given two matrices \(A\) and \(B\):

\[ A = \begin{bmatrix} A_{11} & A_{12} \\ A_{21} & A_{22} \end{bmatrix}, \quad B = \begin{bmatrix} B_{11} & B_{12} \\ B_{21} & B_{22} \end{bmatrix} \]

The product matrix \(C\) is computed as:

\[ C = \begin{bmatrix} C_{11} & C_{12} \\ C_{21} & C_{22} \end{bmatrix} \]

Where:

  • \(C_{11} = P_1 + P_4 - P_5 + P_7\)
  • \(C_{12} = P_3 + P_5\)
  • \(C_{21} = P_2 + P_4\)
  • \(C_{22} = P_1 - P_2 + P_3 + P_6\)

And the intermediate products are:

  • \(P_1 = A_{11} \times (B_{12} - B_{22})\)
  • \(P_2 = (A_{11} + A_{12}) \times B_{22}\)
  • \(P_3 = (A_{21} + A_{22}) \times B_{11}\)
  • \(P_4 = A_{22} \times (B_{21} - B_{11})\)
  • \(P_5 = (A_{11} + A_{22}) \times (B_{11} + B_{22})\)
  • \(P_6 = (A_{12} - A_{22}) \times (B_{21} + B_{22})\)
  • \(P_7 = (A_{11} - A_{21}) \times (B_{11} + B_{12})\)

Implementation

Below is a Python implementation of Strassen's Matrix Multiplication algorithm:

def add_matrices(A, B):
    return [[A[i][j] + B[i][j] for j in range(len(A[0]))] for i in range(len(A))]

def subtract_matrices(A, B):
    return [[A[i][j] - B[i][j] for j in range(len(A[0]))] for i in range(len(A))]

def strassen_matrix_multiply(A, B):
    n = len(A)
    
    # Base case: if matrices are 1x1
    if n == 1:
        return [[A[0][0] * B[0][0]]]
    
    # Splitting the matrices into four submatrices
    A11 = [row[:n//2] for row in A[:n//2]]
    A12 = [row[n//2:] for row in A[:n//2]]
    A21 = [row[:n//2] for row in A[n//2:]]
    A22 = [row[n//2:] for row in A[n//2:]]
    
    B11 = [row[:n//2] for row in B[:n//2]]
    B12 = [row[n//2:] for row in B[:n//2]]
    B21 = [row[:n//2] for row in B[n//2:]]
    B22 = [row[n//2:] for row in B[n//2:]]
    
    # Calculating the seven products
    P1 = strassen_matrix_multiply(add_matrices(A11, A22), add_matrices(B11, B22))
    P2 = strassen_matrix_multiply(add_matrices(A21, A22), B11)
    P3 = strassen_matrix_multiply(A11, subtract_matrices(B12, B22))
    P4 = strassen_matrix_multiply(A22, subtract_matrices(B21, B11))
    P5 = strassen_matrix_multiply(add_matrices(A11, A12), B22)
    P6 = strassen_matrix_multiply(subtract_matrices(A21, A11), add_matrices(B11, B12))
    P7 = strassen_matrix_multiply(subtract_matrices(A12, A22), add_matrices(B21, B22))
    
    # Calculating the four submatrices of the result matrix
    C11 = add_matrices(add_matrices(P1, P4), subtract_matrices(P5, P7))
    C12 = add_matrices(P3, P5)
    C21 = add_matrices(P2, P4)
    C22 = add_matrices(subtract_matrices(P1, P2), add_matrices(P3, P6))
    
    # Combining the submatrices into a single matrix
    result = []
    for i in range(n//2):
        result.append(C11[i] + C12[i])
    for i in range(n//2):
        result.append(C21[i] + C22[i])
    
    return result

# Example usage
A = [[1, 3], [7, 5]]
B = [[6, 8], [4, 2]]

C = strassen_matrix_multiply(A, B)
print("Resultant Matrix:")
for row in C:
    print(row)

Explanation of the Code

  1. Helper Functions:

    • add_matrices: Adds two matrices element-wise.
    • subtract_matrices: Subtracts one matrix from another element-wise.
  2. Base Case:

    • If the matrices are \(1 \times 1\), simply multiply their elements.
  3. Matrix Splitting:

    • Divide each matrix into four submatrices of equal size.
  4. Recursive Multiplication:

    • Compute the seven intermediate products using recursive calls to strassen_matrix_multiply.
  5. Combining Results:

    • Combine the intermediate products to form the final product matrix.

Advantages and Considerations

Advantages

  • Reduced Time Complexity: Strassen's algorithm has a time complexity of \(O(n^{2.807})\), which is faster than the traditional \(O(n^3)\) for large matrices.
  • Efficient for Large Matrices: The divide and conquer approach makes it more efficient for large-scale matrix operations.

Considerations

  • Overhead: For small matrices, the overhead of recursive calls may outweigh the benefits of reduced multiplications.
  • Numerical Stability: Strassen's algorithm can be less numerically stable than traditional methods due to the subtraction of potentially large numbers.
  • Implementation Complexity: The implementation is more complex and requires careful handling of matrix splitting and combining.

Best Practices

  1. Use for Large Matrices: Apply Strassen's algorithm when dealing with large matrices where performance optimization is critical.
  2. Optimize Base Case: For small matrices, switch to traditional multiplication to avoid the overhead of recursive calls.
  3. Handle Edge Cases: Ensure that matrices are square and have dimensions that are powers of two for optimal performance.

Conclusion

Strassen's Matrix Multiplication is a powerful algorithm that leverages the divide and conquer approach to reduce the time complexity of matrix multiplication. While it has some limitations, especially with small matrices and numerical stability, it remains an essential technique in computational linear algebra. By understanding its principles and implementation, you can optimize your applications for better performance when dealing with large-scale matrix operations.

Feel free to experiment with the provided code and explore further optimizations or extensions of Strassen's algorithm.


PreviousBinary Search AlgorithmNext Heap Data Structure

Recommended Gear

Binary Search AlgorithmHeap Data Structure