VOOZH about

URL: https://www.geeksforgeeks.org/dsa/strassens-matrix-multiplication/

⇱ Matrix Multiplication - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Matrix Multiplication

Last Updated : 29 Aug, 2025

Given two matrices mat1[][] of size n × m and mat2[][] of size m × q, find their matrix product, where the resulting matrix has dimensions n × q

Examples:

Input: mat1[][] = [[1, 2, 3],
[4, 5, 6]],
mat2[][] = [[7, 8],
[9, 10],
[11, 12]]
Output: [[58, 64],
[139, 154]]
Explanation: Matrix mat1[ ][ ] (2×3) is multiplied with Matrix mat2[ ][ ] (3×2) by computing the dot product of a's rows with b's columns. This results in a new matrix of size 2×2 containing the summed products for each position.

Input: mat1[][] = [[17, 4],
[17, 16]],
mat2[][] = [9, 2],
[7, 1]]
Output: [[181, 38],
[265, 50]]
Explanation: Matrix mat1[ ][ ] (2×2) is multiplied with Matrix mat2[ ][ ] (2×2) by computing the dot product of a's rows with b's columns. This results in a new matrix of size 2×2 containing the summed products for each position.

[Approach 1] - Using Nested Loops - O(n * m * q) Time and O(n * q) Space

The main idea is to initializes the result matrix with zeros and fills each cell by computing the dot product of the corresponding row from a and column from . This is done using three nested loops: the outer two iterate over the result matrix cells, and the innermost loop performs the multiplication and addition.


Output
58 64 
139 154 

[Approach 2] - Using Divide and Conquer - O(n * m * q) Time and O(n * q) Space

The main idea is to multiply two matrices by following the standard row-by-column multiplication method. For each element in the result matrix, it takes a row from the first matrix and a column from the second matrix, multiplies their corresponding elements, and adds them up to get a single value. This process is repeated for every position in the result matrix.

👁 1-

Output
58 64 
139 154 

[Approach 3] - Using Strassen's Method

Strassen’s algorithm originally applies to square matrices, but when adapted for multiplying an n*m matrix with an m*q matrix, the matrices are padded to form square matrices of size s*s where s = next power of two max(n, m, q). The algorithm then performs 7 recursive multiplications on these square blocks.

👁 2

Note: Strassen's Method is often not preferred in practice due to its high constant factors, making the naive method faster for typical applications. It performs poorly with sparse matrices, where specialized algorithms work better. The recursive submatrix divisions require extra memory. Additionally, due to limited precision in floating-point arithmetic, Strassen’s algorithm tends to accumulate more errors than the naive approach.


Output
58 64 
139 154 

Time Complexity: O(s³) to O(s^log₂7) ≈ O(s^2.807), where s is the next power of two greater than max(n, m, q), due to only 7 recursive multiplications.
Auxiliary Space: O(s²) because of the padding and the creation of intermediate submatrices during recursion.

Comment
Article Tags: