The Wayback Machine - https://web.archive.org/web/20250513114317/https://www.geeksforgeeks.org/strassens-matrix-multiplication/
Open In App

Strassen’s Matrix Multiplication

Last Updated : 26 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given two square matrices arr[][] and brr[][] of order n * n. Your task is to multiply both the matrices and find the resultant matrix.

Examples:

Input: arr[][] = [ [7, 8], [2, 9] ]
brr[][] = [ [14, 5], [5, 18] ]
Output: [ [138, 179], [73, 172] ]

Input: arr[][] = [ [17, 4], [17, 16] ]
brr[][] = [ [9, 2], [7, 1] ]
Output: [ [181, 38], [265, 50] ]

[Naive Approach] – Using Nested Loops – O(n ^ 3) Time and O(n ^ 2) Space

The idea is to use simple matrix multiplication where we use nested for loops to multiply each elements of both the matrices one by one.

C++
#include <bits/stdc++.h>
using namespace std;

typedef vector<vector<int>> Matrix;

// to multiply two matrices
Matrix mult(Matrix &arr, Matrix &brr) {
    int n = arr.size();

    // to store the resultant matrix
    Matrix res(n, vector<int>(n, 0));

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            for(int k = 0; k < n; k++) {
                res[i][j] += arr[i][k] * brr[k][j];
            }
        }
    }

    return res;
}

int main() {
    Matrix arr = {{7, 8}, {2, 9}};
    Matrix brr = {{14, 5}, {5, 18}};
    Matrix res = mult(arr, brr);
    for(int i = 0; i < res.size(); i++) {
        for(int j = 0; j < res[i].size(); j++) {
            cout << res[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}
Java
import java.util.*;

class GfG {

    // to multiply two matrices
    static int[][] multiply(int[][] arr, int[][] brr) {
        int n = arr.length;

        // to store the resultant matrix
        int[][] res = new int[n][n];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    res[i][j] += arr[i][k] * brr[k][j];
                }
            }
        }

        return res;
    }

    public static void main(String[] args) {
        int[][] arr = { {7, 8}, {2, 9} };
        int[][] brr = { {14, 5}, {5, 18} };
        int[][] res = multiply(arr, brr);
        for (int i = 0; i < res.length; i++) {
            for (int j = 0; j < res[i].length; j++) {
                System.out.print(res[i][j] + " ");
            }
            System.out.println();
        }
    }
}
Python
# to multiply two matrices
def multiply(arr, brr):
    n = len(arr)

    # to store the resultant matrix
    res = [[0 for _ in range(n)] for _ in range(n)]

    for i in range(n):
        for j in range(n):
            for k in range(n):
                res[i][j] += arr[i][k] * brr[k][j]

    return res

if __name__ == "__main__":
    arr = [[7, 8], [2, 9]]
    brr = [[14, 5], [5, 18]]
    res = multiply(arr, brr)
    for i in range(len(res)):
        for j in range(len(res[i])):
            print(res[i][j], end=" ")
        print()
C#
using System;
using System.Collections.Generic;

class GfG {

    // to multiply two matrices
    static List<List<int>> multiply(List<List<int>> arr, 
    List<List<int>> brr) {
        int n = arr.Count;

        // to store the resultant matrix
        List<List<int>> res = new List<List<int>>();
        for (int i = 0; i < n; i++) {
            List<int> row = new List<int>();
            for (int j = 0; j < n; j++) {
                row.Add(0);
            }
            res.Add(row);
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    res[i][j] += arr[i][k] * brr[k][j];
                }
            }
        }

        return res;
    }

    public static void Main() {
        List<int> row1 = new List<int> {7, 8};
        List<int> row2 = new List<int> {2, 9};
        List<List<int>> arr = new List<List<int>> {row1, row2};

        List<int> row3 = new List<int> {14, 5};
        List<int> row4 = new List<int> {5, 18};
        List<List<int>> brr = new List<List<int>> {row3, row4};

        List<List<int>> res = multiply(arr, brr);
        for (int i = 0; i < res.Count; i++) {
            for (int j = 0; j < res[i].Count; j++) {
                Console.Write(res[i][j] + " ");
            }
            Console.WriteLine();
        }
    }
}
JavaScript
// to multiply two matrices
function multiply(arr, brr) {
    let n = arr.length;

    // to store the resultant matrix
    let res = new Array(n);
    for (let i = 0; i < n; i++) {
        res[i] = new Array(n).fill(0);
    }

    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
            for (let k = 0; k < n; k++) {
                res[i][j] += arr[i][k] * brr[k][j];
            }
        }
    }

    return res;
}

let arr = [[7, 8], [2, 9]];
let brr = [[14, 5], [5, 18]];
let res = multiply(arr, brr);
for (let i = 0; i < res.length; i++) {
    console.log(res[i].join(" "));
}

Output
138 179 
73 172 

[Better Approach] – Using Divide and Conquer – O(n ^ 3) Time and O(n ^ 2) Space

The idea is to divide the given matrix into four sub-matrices of order (n / 2) * (n / 2). And then calculate the values ae + bg, af + bh, ce + dg and cf + dh, as shown in image.

strassen_new

C++
#include <bits/stdc++.h>
using namespace std;

typedef vector<vector<int>> Matrix;

// to add two matrices
Matrix add(Matrix arr, Matrix brr, int n) {
    Matrix res(n, vector<int>(n, 0));
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            res[i][j] = arr[i][j] + brr[i][j];
        }
    }
    return res;
}

// to multiply two matrices
Matrix mult(Matrix &arr, Matrix &brr) {
    int n = arr.size();

    // to store the resultant matrix
    Matrix res(n, vector<int>(n, 0));

    if(n == 1) {
        res[0][0] = arr[0][0] * brr[0][0];
        return res;
    }

    // construct eight submatrices
    vector<Matrix> subMat(8, Matrix(n/2, vector<int>(n/2, 0)));

    for(int i = 0; i < n / 2; i++) {
        for(int j = 0; j < n / 2; j++) {
            subMat[0][i][j] = arr[i][j];
            subMat[1][i][j] = arr[i][j + n / 2];
            subMat[2][i][j] = arr[i + n / 2][j];
            subMat[3][i][j] = arr[i + n / 2][j + n / 2];
            subMat[4][i][j] = brr[i][j];
            subMat[5][i][j] = brr[i][j + n / 2];
            subMat[6][i][j] = brr[i + n / 2][j];
            subMat[7][i][j] = brr[i + n / 2][j + n / 2];
        }
    }

    // create four matrices to store the result
    vector<Matrix> resMat(4, Matrix(n/2, vector<int>(n/2, 0)));

    // calculate the product and add them
    resMat[0] = add(mult(subMat[0], subMat[4]), mult(subMat[1], subMat[6]), n/2);
    resMat[1] = add(mult(subMat[0], subMat[5]), mult(subMat[1], subMat[7]), n/2);
    resMat[2] = add(mult(subMat[2], subMat[4]), mult(subMat[3], subMat[6]), n/2);
    resMat[3] = add(mult(subMat[2], subMat[5]), mult(subMat[3], subMat[7]), n/2);

    // copy the result to the resultant matrix
    for(int i = 0; i < n / 2; i++) {
        for(int j = 0; j < n / 2; j++) {
            res[i][j] = resMat[0][i][j];
            res[i][j + n / 2] = resMat[1][i][j];
            res[i + n / 2][j] = resMat[2][i][j];
            res[i + n / 2][j + n / 2] = resMat[3][i][j];
        }
    }

    return res;
}

int main() {
    Matrix arr = {{7, 8}, {2, 9}};
    Matrix brr = {{14, 5}, {5, 18}};
    Matrix res = mult(arr, brr);
    for(int i = 0; i < res.size(); i++) {
        for(int j = 0; j < res[i].size(); j++) {
            cout << res[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}
Java
// to add two matrices
public static int[][] add(int[][] arr, int[][] brr) {
    int n = arr.length;
    int[][] res = new int[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            res[i][j] = arr[i][j] + brr[i][j];
        }
    }
    return res;
}

// to multiply two matrices
public static int[][] mult(int[][] arr, int[][] brr) {
    int n = arr.length;

    // to store the resultant matrix
    int[][] res = new int[n][n];

    if (n == 1) {
        res[0][0] = arr[0][0] * brr[0][0];
        return res;
    }

    // construct eight submatrices
    int[][][] subMat = new int[8][n / 2][n / 2];

    for (int i = 0; i < n / 2; i++) {
        for (int j = 0; j < n / 2; j++) {
            subMat[0][i][j] = arr[i][j];
            subMat[1][i][j] = arr[i][j + n / 2];
            subMat[2][i][j] = arr[i + n / 2][j];
            subMat[3][i][j] = arr[i + n / 2][j + n / 2];
            subMat[4][i][j] = brr[i][j];
            subMat[5][i][j] = brr[i][j + n / 2];
            subMat[6][i][j] = brr[i + n / 2][j];
            subMat[7][i][j] = brr[i + n / 2][j + n / 2];
        }
    }

    // create four matrices to store the result
    int[][][] resMat = new int[4][n / 2][n / 2];

    // calculate the product and add them
    resMat[0] = add(mult(subMat[0], subMat[4]), mult(subMat[1], subMat[6]));
    resMat[1] = add(mult(subMat[0], subMat[5]), mult(subMat[1], subMat[7]));
    resMat[2] = add(mult(subMat[2], subMat[4]), mult(subMat[3], subMat[6]));
    resMat[3] = add(mult(subMat[2], subMat[5]), mult(subMat[3], subMat[7]));

    // copy the result to the resultant matrix
    for (int i = 0; i < n / 2; i++) {
        for (int j = 0; j < n / 2; j++) {
            res[i][j] = resMat[0][i][j];
            res[i][j + n / 2] = resMat[1][i][j];
            res[i + n / 2][j] = resMat[2][i][j];
            res[i + n / 2][j + n / 2] = resMat[3][i][j];
        }
    }

    return res;
}

public static void main(String[] args) {
    int[][] arr = {{7, 8}, {2, 9}};
    int[][] brr = {{14, 5}, {5, 18}};
    int[][] res = mult(arr, brr);
    for (int[] row : res) {
        System.out.println(Arrays.toString(row));
    }
}
Python
# to add two matrices

def add(arr, brr):
    n = len(arr)
    res = [[0] * n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            res[i][j] = arr[i][j] + brr[i][j]
    return res

# to multiply two matrices
def mult(arr, brr):
    n = len(arr)

    # to store the resultant matrix
    res = [[0] * n for _ in range(n)]

    if n == 1:
        res[0][0] = arr[0][0] * brr[0][0]
        return res

    # construct eight submatrices
    subMat = [[[[0] * (n // 2) for _ in range(n // 2)] for _ in range(8)]]

    for i in range(n // 2):
        for j in range(n // 2):
            subMat[0][i][j] = arr[i][j]
            subMat[1][i][j] = arr[i][j + n // 2]
            subMat[2][i][j] = arr[i + n // 2][j]
            subMat[3][i][j] = arr[i + n // 2][j + n // 2]
            subMat[4][i][j] = brr[i][j]
            subMat[5][i][j] = brr[i][j + n // 2]
            subMat[6][i][j] = brr[i + n // 2][j]
            subMat[7][i][j] = brr[i + n // 2][j + n // 2]

    # create four matrices to store the result
    resMat = [[[[0] * (n // 2) for _ in range(n // 2)] for _ in range(4)]]

    # calculate the product and add them
    resMat[0] = add(mult(subMat[0], subMat[4]), mult(subMat[1], subMat[6]))
    resMat[1] = add(mult(subMat[0], subMat[5]), mult(subMat[1], subMat[7]))
    resMat[2] = add(mult(subMat[2], subMat[4]), mult(subMat[3], subMat[6]))
    resMat[3] = add(mult(subMat[2], subMat[5]), mult(subMat[3], subMat[7]))

    # copy the result to the resultant matrix
    for i in range(n // 2):
        for j in range(n // 2):
            res[i][j] = resMat[0][i][j]
            res[i][j + n // 2] = resMat[1][i][j]
            res[i + n // 2][j] = resMat[2][i][j]
            res[i + n // 2][j + n // 2] = resMat[3][i][j]

    return res

if __name__ == '__main__':
    arr = [[7, 8], [2, 9]]
    brr = [[14, 5], [5, 18]]
    res = mult(arr, brr)
    for row in res:
        print(' '.join(map(str, row)))
C#
// to add two matrices
public static int[,] Add(int[,] arr, int[,] brr) {
    int n = arr.GetLength(0);
    int[,] res = new int[n, n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            res[i, j] = arr[i, j] + brr[i, j];
        }
    }
    return res;
}

// to multiply two matrices
public static int[,] Mult(int[,] arr, int[,] brr) {
    int n = arr.GetLength(0);

    // to store the resultant matrix
    int[,] res = new int[n, n];

    if (n == 1) {
        res[0, 0] = arr[0, 0] * brr[0, 0];
        return res;
    }

    // construct eight submatrices
    int[][,] subMat = new int[8][,];
    for (int i = 0; i < 8; i++) {
        subMat[i] = new int[n / 2, n / 2];
    }

    for (int i = 0; i < n / 2; i++) {
        for (int j = 0; j < n / 2; j++) {
            subMat[0][i, j] = arr[i, j];
            subMat[1][i, j] = arr[i, j + n / 2];
            subMat[2][i, j] = arr[i + n / 2, j];
            subMat[3][i, j] = arr[i + n / 2, j + n / 2];
            subMat[4][i, j] = brr[i, j];
            subMat[5][i, j] = brr[i, j + n / 2];
            subMat[6][i, j] = brr[i + n / 2, j];
            subMat[7][i, j] = brr[i + n / 2, j + n / 2];
        }
    }

    // create four matrices to store the result
    int[][,] resMat = new int[4][,];
    for (int i = 0; i < 4; i++) {
        resMat[i] = new int[n / 2, n / 2];
    }

    // calculate the product and add them
    resMat[0] = Add(Mult(subMat[0], subMat[4]), Mult(subMat[1], subMat[6]));
    resMat[1] = Add(Mult(subMat[0], subMat[5]), Mult(subMat[1], subMat[7]));
    resMat[2] = Add(Mult(subMat[2], subMat[4]), Mult(subMat[3], subMat[6]));
    resMat[3] = Add(Mult(subMat[2], subMat[5]), Mult(subMat[3], subMat[7]));

    // copy the result to the resultant matrix
    for (int i = 0; i < n / 2; i++) {
        for (int j = 0; j < n / 2; j++) {
            res[i, j] = resMat[0][i, j];
            res[i, j + n / 2] = resMat[1][i, j];
            res[i + n / 2, j] = resMat[2][i, j];
            res[i + n / 2, j + n / 2] = resMat[3][i, j];
        }
    }

    return res;
}

public static void Main() {
    int[,] arr = {{7, 8}, {2, 9}};
    int[,] brr = {{14, 5}, {5, 18}};
    int[,] res = Mult(arr, brr);
    for (int i = 0; i < res.GetLength(0); i++) {
        for (int j = 0; j < res.GetLength(1); j++) {
            Console.Write(res[i, j] + " ");
        }
        Console.WriteLine();
    }
}
JavaScript
// to add two matrices
function add(arr, brr) {
    const n = arr.length;
    const res = Array.from({ length: n }, () => Array(n).fill(0));
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
            res[i][j] = arr[i][j] + brr[i][j];
        }
    }
    return res;
}

// to multiply two matrices
function mult(arr, brr) {
    const n = arr.length;

    // to store the resultant matrix
    const res = Array.from({ length: n }, () => Array(n).fill(0));

    if (n === 1) {
        res[0][0] = arr[0][0] * brr[0][0];
        return res;
    }

    // construct eight submatrices
    const subMat = Array.from({ length: 8 }, () => Array.from({ length: n / 2 }, () => Array(n / 2).fill(0)));

    for (let i = 0; i < n / 2; i++) {
        for (let j = 0; j < n / 2; j++) {
            subMat[0][i][j] = arr[i][j];
            subMat[1][i][j] = arr[i][j + n / 2];
            subMat[2][i][j] = arr[i + n / 2][j];
            subMat[3][i][j] = arr[i + n / 2][j + n / 2];
            subMat[4][i][j] = brr[i][j];
            subMat[5][i][j] = brr[i][j + n / 2];
            subMat[6][i][j] = brr[i + n / 2][j];
            subMat[7][i][j] = brr[i + n / 2][j + n / 2];
        }
    }

    // create four matrices to store the result
    const resMat = Array.from({ length: 4 }, () => Array.from({ length: n / 2 }, () => Array(n / 2).fill(0)));

    // calculate the product and add them
    resMat[0] = add(mult(subMat[0], subMat[4]), mult(subMat[1], subMat[6]));
    resMat[1] = add(mult(subMat[0], subMat[5]), mult(subMat[1], subMat[7]));
    resMat[2] = add(mult(subMat[2], subMat[4]), mult(subMat[3], subMat[6]));
    resMat[3] = add(mult(subMat[2], subMat[5]), mult(subMat[3], subMat[7]));

    // copy the result to the resultant matrix
    for (let i = 0; i < n / 2; i++) {
        for (let j = 0; j < n / 2; j++) {
            res[i][j] = resMat[0][i][j];
            res[i][j + n / 2] = resMat[1][i][j];
            res[i + n / 2][j] = resMat[2][i][j];
            res[i + n / 2][j + n / 2] = resMat[3][i][j];
        }
    }

    return res;
}

const arr = [[7, 8], [2, 9]];
const brr = [[14, 5], [5, 18]];
const res = mult(arr, brr);
res.forEach(row => console.log(row.join(' ')));

Output
138 179 
73 172 

[Expected Approach] – Using Strassen’s Method – O(n ^ 3) Time and O(n ^ 2) Space

In the above divide and conquer method, the main component for high time complexity is 8 recursive calls. The idea of Strassen’s method is to reduce the number of recursive calls to 7. Strassen’s method is similar to above simple divide and conquer method in the sense that this method also divide matrices to sub-matrices of order (n / 2) * (n / 2) as shown in the diagram, but in Strassen’s method, the four sub-matrices of result are calculated using following formulae.

stressen_formula_new_new

Generally Strassen’s Method is not preferred for practical applications for following reasons. 

  1. The constants used in Strassen’s method are high and for a typical application Naive method works better. 
  2. For Sparse matrices, there are better methods especially designed for them. 
  3. The submatrices in recursion take extra space. 
  4. Because of the limited precision of computer arithmetic on non integer values, larger errors accumulate in Strassen’s algorithm than in Naive Method
C++
#include <bits/stdc++.h>
using namespace std;

typedef vector<vector<int>> Matrix;

// Function to add or subtract two matrices
Matrix add(Matrix arr, Matrix brr, int n, int sign = 1) {
    Matrix res(n, vector<int>(n, 0));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            res[i][j] = arr[i][j] + sign * brr[i][j];
    return res;
}

// Strassen's matrix multiplication function
Matrix mult(Matrix arr, Matrix brr) {
    int n = arr.size();
    Matrix res(n, vector<int>(n, 0));

    // Base case
    if (n == 1) {
        res[0][0] = arr[0][0] * brr[0][0];
        return res;
    }

    int nwSz = n / 2; // New Size
    Matrix a11(nwSz, vector<int>(nwSz, 0));
    Matrix a12(nwSz, vector<int>(nwSz, 0));
    Matrix a21(nwSz, vector<int>(nwSz, 0));
    Matrix a22(nwSz, vector<int>(nwSz, 0));
    Matrix b11(nwSz, vector<int>(nwSz, 0));
    Matrix b12(nwSz, vector<int>(nwSz, 0));
    Matrix b21(nwSz, vector<int>(nwSz, 0));
    Matrix b22(nwSz, vector<int>(nwSz, 0));

    // Splitting arr and brr into submatrices
    for (int i = 0; i < nwSz; i++)
        for (int j = 0; j < nwSz; j++) {
            a11[i][j] = arr[i][j];
            a12[i][j] = arr[i][j + nwSz];
            a21[i][j] = arr[i + nwSz][j];
            a22[i][j] = arr[i + nwSz][j + nwSz];
            b11[i][j] = brr[i][j];
            b12[i][j] = brr[i][j + nwSz];
            b21[i][j] = brr[i + nwSz][j];
            b22[i][j] = brr[i + nwSz][j + nwSz];
        }

    // Strassen's seven multiplications
    Matrix m1 = mult(add(a11, a22, nwSz), add(b11, b22, nwSz));
    Matrix m2 = mult(add(a21, a22, nwSz), b11);
    Matrix m3 = mult(a11, add(b12, b22, nwSz, -1));
    Matrix m4 = mult(a22, add(b21, b11, nwSz, -1));
    Matrix m5 = mult(add(a11, a12, nwSz), b22);
    Matrix m6 = mult(add(a21, a11, nwSz, -1), add(b11, b12, nwSz));
    Matrix m7 = mult(add(a12, a22, nwSz, -1), add(b21, b22, nwSz));

    // Calculating result submatrices
    Matrix c11 = add(add(m1, m4, nwSz), add(m7, m5, nwSz, -1), nwSz);
    Matrix c12 = add(m3, m5, nwSz);
    Matrix c21 = add(m2, m4, nwSz);
    Matrix c22 = add(add(m1, m3, nwSz), add(m6, m2, nwSz, -1), nwSz);

    // Copy results into result matrix
    for (int i = 0; i < nwSz; i++)
        for (int j = 0; j < nwSz; j++) {
            res[i][j] = c11[i][j];
            res[i][j + nwSz] = c12[i][j];
            res[i + nwSz][j] = c21[i][j];
            res[i + nwSz][j + nwSz] = c22[i][j];
        }

    return res;
}

int main() {
    Matrix arr = {{7, 8}, {2, 9}};
    Matrix brr = {{14, 5}, {5, 18}};
    Matrix res = mult(arr, brr);

    for (int i = 0; i < res.size(); i++) {
        for (int j = 0; j < res[i].size(); j++) {
            cout << res[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}
Java
// Function to add or subtract two matrices
public static int[][] add(int[][] arr, int[][] brr, int n, int sign) {
    int[][] res = new int[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            res[i][j] = arr[i][j] + sign * brr[i][j];
        }
    }
    return res;
}

// Strassen's matrix multiplication function
public static int[][] mult(int[][] arr, int[][] brr) {
    int n = arr.length;
    int[][] res = new int[n][n];

    // Base case
    if (n == 1) {
        res[0][0] = arr[0][0] * brr[0][0];
        return res;
    }

    int nwSz = n / 2; // New Size
    int[][] a11 = new int[nwSz][nwSz];
    int[][] a12 = new int[nwSz][nwSz];
    int[][] a21 = new int[nwSz][nwSz];
    int[][] a22 = new int[nwSz][nwSz];
    int[][] b11 = new int[nwSz][nwSz];
    int[][] b12 = new int[nwSz][nwSz];
    int[][] b21 = new int[nwSz][nwSz];
    int[][] b22 = new int[nwSz][nwSz];

    // Splitting arr and brr into submatrices
    for (int i = 0; i < nwSz; i++) {
        for (int j = 0; j < nwSz; j++) {
            a11[i][j] = arr[i][j];
            a12[i][j] = arr[i][j + nwSz];
            a21[i][j] = arr[i + nwSz][j];
            a22[i][j] = arr[i + nwSz][j + nwSz];
            b11[i][j] = brr[i][j];
            b12[i][j] = brr[i][j + nwSz];
            b21[i][j] = brr[i + nwSz][j];
            b22[i][j] = brr[i + nwSz][j + nwSz];
        }
    }

    // Strassen's seven multiplications
    int[][] m1 = mult(add(a11, a22, nwSz, 1), add(b11, b22, nwSz, 1));
    int[][] m2 = mult(add(a21, a22, nwSz, 1), b11);
    int[][] m3 = mult(a11, add(b12, b22, nwSz, -1));
    int[][] m4 = mult(a22, add(b21, b11, nwSz, -1));
    int[][] m5 = mult(add(a11, a12, nwSz, 1), b22);
    int[][] m6 = mult(add(a21, a11, nwSz, -1), add(b11, b12, nwSz, 1));
    int[][] m7 = mult(add(a12, a22, nwSz, -1), add(b21, b22, nwSz, 1));

    // Calculating result submatrices
    int[][] c11 = add(add(m1, m4, nwSz, 1), add(m7, m5, nwSz, -1), nwSz);
    int[][] c12 = add(m3, m5, nwSz, 1);
    int[][] c21 = add(m2, m4, nwSz, 1);
    int[][] c22 = add(add(m1, m3, nwSz, 1), add(m6, m2, nwSz, -1), nwSz);

    // Copy results into result matrix
    for (int i = 0; i < nwSz; i++) {
        for (int j = 0; j < nwSz; j++) {
            res[i][j] = c11[i][j];
            res[i][j + nwSz] = c12[i][j];
            res[i + nwSz][j] = c21[i][j];
            res[i + nwSz][j + nwSz] = c22[i][j];
        }
    }

    return res;
}

public static void main(String[] args) {
    int[][] arr = {{7, 8}, {2, 9}};
    int[][] brr = {{14, 5}, {5, 18}};
    int[][] res = mult(arr, brr);

    for (int[] row : res) {
        System.out.println(Arrays.toString(row));
    }
}
Python
# Function to add or subtract two matrices
def add(arr, brr, n, sign=1):
    res = [[0] * n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            res[i][j] = arr[i][j] + sign * brr[i][j]
    return res

# Strassen's matrix multiplication function
def mult(arr, brr):
    n = len(arr)
    res = [[0] * n for _ in range(n)]

    # Base case
    if n == 1:
        res[0][0] = arr[0][0] * brr[0][0]
        return res

    nwSz = n // 2  # New Size
    a11 = [[0] * nwSz for _ in range(nwSz)]
    a12 = [[0] * nwSz for _ in range(nwSz)]
    a21 = [[0] * nwSz for _ in range(nwSz)]
    a22 = [[0] * nwSz for _ in range(nwSz)]
    b11 = [[0] * nwSz for _ in range(nwSz)]
    b12 = [[0] * nwSz for _ in range(nwSz)]
    b21 = [[0] * nwSz for _ in range(nwSz)]
    b22 = [[0] * nwSz for _ in range(nwSz)]

    # Splitting arr and brr into submatrices
    for i in range(nwSz):
        for j in range(nwSz):
            a11[i][j] = arr[i][j]
            a12[i][j] = arr[i][j + nwSz]
            a21[i][j] = arr[i + nwSz][j]
            a22[i][j] = arr[i + nwSz][j + nwSz]
            b11[i][j] = brr[i][j]
            b12[i][j] = brr[i][j + nwSz]
            b21[i][j] = brr[i + nwSz][j]
            b22[i][j] = brr[i + nwSz][j + nwSz]

    # Strassen's seven multiplications
    m1 = mult(add(a11, a22, nwSz), add(b11, b22, nwSz))
    m2 = mult(add(a21, a22, nwSz), b11)
    m3 = mult(a11, add(b12, b22, nwSz, -1))
    m4 = mult(a22, add(b21, b11, nwSz, -1))
    m5 = mult(add(a11, a12, nwSz), b22)
    m6 = mult(add(a21, a11, nwSz, -1), add(b11, b12, nwSz))
    m7 = mult(add(a12, a22, nwSz, -1), add(b21, b22, nwSz))

    # Calculating result submatrices
    c11 = add(add(m1, m4, nwSz), add(m7, m5, nwSz, -1), nwSz)
    c12 = add(m3, m5, nwSz)
    c21 = add(m2, m4, nwSz)
    c22 = add(add(m1, m3, nwSz), add(m6, m2, nwSz, -1), nwSz)

    # Copy results into result matrix
    for i in range(nwSz):
        for j in range(nwSz):
            res[i][j] = c11[i][j]
            res[i][j + nwSz] = c12[i][j]
            res[i + nwSz][j] = c21[i][j]
            res[i + nwSz][j + nwSz] = c22[i][j]

    return res

if __name__ == '__main__':
    arr = [[7, 8], [2, 9]]
    brr = [[14, 5], [5, 18]]
    res = mult(arr, brr)

    for row in res:
        print(' '.join(map(str, row)))
C#
// Function to add or subtract two matrices
public static int[,] add(int[,] arr, int[,] brr, int n, int sign) {
    int[,] res = new int[n, n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            res[i, j] = arr[i, j] + sign * brr[i, j];
        }
    }
    return res;
}

// Strassen's matrix multiplication function
public static int[,] mult(int[,] arr, int[,] brr) {
    int n = arr.GetLength(0);
    int[,] res = new int[n, n];

    // Base case
    if (n == 1) {
        res[0, 0] = arr[0, 0] * brr[0, 0];
        return res;
    }

    int nwSz = n / 2; // New Size
    int[,] a11 = new int[nwSz, nwSz];
    int[,] a12 = new int[nwSz, nwSz];
    int[,] a21 = new int[nwSz, nwSz];
    int[,] a22 = new int[nwSz, nwSz];
    int[,] b11 = new int[nwSz, nwSz];
    int[,] b12 = new int[nwSz, nwSz];
    int[,] b21 = new int[nwSz, nwSz];
    int[,] b22 = new int[nwSz, nwSz];

    // Splitting arr and brr into submatrices
    for (int i = 0; i < nwSz; i++) {
        for (int j = 0; j < nwSz; j++) {
            a11[i, j] = arr[i, j];
            a12[i, j] = arr[i, j + nwSz];
            a21[i, j] = arr[i + nwSz, j];
            a22[i, j] = arr[i + nwSz, j + nwSz];
            b11[i, j] = brr[i, j];
            b12[i, j] = brr[i, j + nwSz];
            b21[i, j] = brr[i + nwSz, j];
            b22[i, j] = brr[i + nwSz, j + nwSz];
        }
    }

    // Strassen's seven multiplications
    int[,] m1 = mult(add(a11, a22, nwSz, 1), add(b11, b22, nwSz, 1));
    int[,] m2 = mult(add(a21, a22, nwSz, 1), b11);
    int[,] m3 = mult(a11, add(b12, b22, nwSz, -1));
    int[,] m4 = mult(a22, add(b21, b11, nwSz, -1));
    int[,] m5 = mult(add(a11, a12, nwSz, 1), b22);
    int[,] m6 = mult(add(a21, a11, nwSz, -1), add(b11, b12, nwSz, 1));
    int[,] m7 = mult(add(a12, a22, nwSz, -1), add(b21, b22, nwSz, 1));

    // Calculating result submatrices
    int[,] c11 = add(add(m1, m4, nwSz, 1), add(m7, m5, nwSz, -1), nwSz);
    int[,] c12 = add(m3, m5, nwSz, 1);
    int[,] c21 = add(m2, m4, nwSz, 1);
    int[,] c22 = add(add(m1, m3, nwSz, 1), add(m6, m2, nwSz, -1), nwSz);

    // Copy results into result matrix
    for (int i = 0; i < nwSz; i++) {
        for (int j = 0; j < nwSz; j++) {
            res[i, j] = c11[i, j];
            res[i, j + nwSz] = c12[i, j];
            res[i + nwSz, j] = c21[i, j];
            res[i + nwSz, j + nwSz] = c22[i, j];
        }
    }

    return res;
}

public static void Main(string[] args) {
    int[,] arr = {{7, 8}, {2, 9}};
    int[,] brr = {{14, 5}, {5, 18}};
    int[,] res = mult(arr, brr);

    foreach (var row in res) {
        Console.WriteLine(string.Join(", ", row));
    }
}
JavaScript
// Function to add or subtract two matrices
function add(arr, brr, n, sign = 1) {
    const res = Array.from({ length: n }, () => Array(n).fill(0));
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
            res[i][j] = arr[i][j] + sign * brr[i][j];
        }
    }
    return res;
}

// Strassen's matrix multiplication function
function mult(arr, brr) {
    const n = arr.length;
    const res = Array.from({ length: n }, () => Array(n).fill(0));

    // Base case
    if (n === 1) {
        res[0][0] = arr[0][0] * brr[0][0];
        return res;
    }

    const nwSz = Math.floor(n / 2);  // New Size
    const a11 = Array.from({ length: nwSz }, () => Array(nwSz).fill(0));
    const a12 = Array.from({ length: nwSz }, () => Array(nwSz).fill(0));
    const a21 = Array.from({ length: nwSz }, () => Array(nwSz).fill(0));
    const a22 = Array.from({ length: nwSz }, () => Array(nwSz).fill(0));
    const b11 = Array.from({ length: nwSz }, () => Array(nwSz).fill(0));
    const b12 = Array.from({ length: nwSz }, () => Array(nwSz).fill(0));
    const b21 = Array.from({ length: nwSz }, () => Array(nwSz).fill(0));
    const b22 = Array.from({ length: nwSz }, () => Array(nwSz).fill(0));

    // Splitting arr and brr into submatrices
    for (let i = 0; i < nwSz; i++) {
        for (let j = 0; j < nwSz; j++) {
            a11[i][j] = arr[i][j];
            a12[i][j] = arr[i][j + nwSz];
            a21[i][j] = arr[i + nwSz][j];
            a22[i][j] = arr[i + nwSz][j + nwSz];
            b11[i][j] = brr[i][j];
            b12[i][j] = brr[i][j + nwSz];
            b21[i][j] = brr[i + nwSz][j];
            b22[i][j] = brr[i + nwSz][j + nwSz];
        }
    }

    // Strassen's seven multiplications
    const m1 = mult(add(a11, a22, nwSz), add(b11, b22, nwSz));
    const m2 = mult(add(a21, a22, nwSz), b11);
    const m3 = mult(a11, add(b12, b22, nwSz, -1));
    const m4 = mult(a22, add(b21, b11, nwSz, -1));
    const m5 = mult(add(a11, a12, nwSz), b22);
    const m6 = mult(add(a21, a11, nwSz, -1), add(b11, b12, nwSz));
    const m7 = mult(add(a12, a22, nwSz, -1), add(b21, b22, nwSz));

    // Calculating result submatrices
    const c11 = add(add(m1, m4, nwSz), add(m7, m5, nwSz, -1), nwSz);
    const c12 = add(m3, m5, nwSz);
    const c21 = add(m2, m4, nwSz);
    const c22 = add(add(m1, m3, nwSz), add(m6, m2, nwSz, -1), nwSz);

    // Copy results into result matrix
    for (let i = 0; i < nwSz; i++) {
        for (let j = 0; j < nwSz; j++) {
            res[i][j] = c11[i][j];
            res[i][j + nwSz] = c12[i][j];
            res[i + nwSz][j] = c21[i][j];
            res[i + nwSz][j + nwSz] = c22[i][j];
        }
    }

    return res;
}

const arr = [[7, 8], [2, 9]];
const brr = [[14, 5], [5, 18]];
const res = mult(arr, brr);

for (const row of res) {
    console.log(row.join(' '));
}

Output
138 179 
73 172 

Time Complexity of Strassen’s Method

Addition and Subtraction of two matrices takes O(N2) time. So time complexity can be written as 

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

From Master's Theorem, time complexity of above method is
O(nLog7) which is approximately O(n2.8074)

Generally Strassen’s Method is not preferred for practical applications for following reasons. 

  1. The constants used in Strassen’s method are high and for a typical application Naive method works better. 
  2. For Sparse matrices, there are better methods especially designed for them. 
  3. The submatrices in recursion take extra space. 
  4. Because of the limited precision of computer arithmetic on noninteger values, larger errors accumulate in Strassen’s algorithm than in Naive Method


Next Article
Article Tags :
Practice Tags :

Similar Reads