0% found this document useful (0 votes)
15 views18 pages

Strassian Matrix

Strassen's algorithm is a fast matrix multiplication method that reduces the time complexity from O(n^3) to approximately O(n^2.81) by using a divide-and-conquer approach. It involves breaking matrices into smaller submatrices and calculating seven intermediate matrices to optimize the multiplication process. While it is more efficient for large matrices, it may be slower than traditional methods for smaller matrices due to overhead.

Uploaded by

heyprettyaera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views18 pages

Strassian Matrix

Strassen's algorithm is a fast matrix multiplication method that reduces the time complexity from O(n^3) to approximately O(n^2.81) by using a divide-and-conquer approach. It involves breaking matrices into smaller submatrices and calculating seven intermediate matrices to optimize the multiplication process. While it is more efficient for large matrices, it may be slower than traditional methods for smaller matrices due to overhead.

Uploaded by

heyprettyaera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Strassen’s matrix multiplication

 Strassen's algorithm, developed by Volker Strassen in 1969, is a fast algorithm for


matrix multiplication.

 It is an efficient divide-and-conquer method that reduces the number of arithmetic


operations required to multiply two matrices compared to the conventional matrix
multiplication algorithm (the naive approach).

 The traditional matrix multiplication algorithm has a time complexity of O(n3) for
multiplying two n x n matrices. However, Strassen's algorithm improves this to
O(n^log2(7)), which is approximately O(n2.81).

 The algorithm achieves this improvement by recursively breaking down the matrix
multiplication into smaller subproblems and combining the results.
Complexity Analysis

Time Complexity: O(N^3), where the given matrices are square matrices of size
N*N each.

For multiplying every column with every element in the row from the given
matrices uses two loops, and adding up the values takes another loop. Therefore
the overall time complexity turns out to be O(N^3^).

Space Complexity: O(N^2), where the given matrices are square matrices of size
N*N each.

The matrix of size N*N has been used to store the result when the two matrices
are multiplied.
In the Strassen matrix multiplication algorithm, seven intermediate matrices are
defined to break down the multiplication of two matrices into smaller,
manageable operations.

Here are the definitions of these seven intermediate matrices:


Given two matrices A and B, divided into sub-matrices:

A is divided into four quadrants: B is similarly divided:

A 11 (top-left) B 11(top-left)
A 12 (top-right) B12(top-right)
A21 (bottom-left) B 21(bottom-left)
A22 (bottom-right) B22(bottom-right)
Intermediate Matrices Definition

Strassen’s algorithm then calculates seven intermediate matrices,


denoted as M1,M2,……,M7​, as follows:

1.Matrix M1 :M1=(A11+A22)×(B11+B22)

2.Matrix M2 : M2=(A21 + A22) x B11

3. Matrix M3 : M3 = A11 x (B12 – B22)

4. Matrix M4 : M4 = A22 x (B21 – B11)

5. Matrix M5 : M5 = (A11+A12) x B22

6. Matrix M6 : M6 = (A21 – A11) x (B11 + B12)

7. Matrix M7 : M7 = (A12 – A22) x (B21 + B22)

Next, we recursively compute seven products of these submatrices, i.e., M1, M2, M3, M4, M5, M6, and M7.
Finally, we combine the results to obtain the four submatrices of the resulting
matrix C of size n x n:

C11 = M1 + M4 – M5 + M7

C12 = M3 + M5

C21 = M2 + M4

C22 = M1 – M2 + M3 + M6

Concatenate the four submatrices C11, C12, C21, and C22 to obtain the final
result matrix C.

Strassen's algorithm optimally reduces the number of necessary multiplications


by combining submatrices intelligently through these intermediate matrices.
The efficiency of Strassen's algorithm comes from the fact that it reduces the
number of recursive calls, which means fewer multiplication operations are
needed overall.

However, due to its higher constant factors and increased overhead, Strassen's
algorithm is sometimes slower than the naive algorithm for small matrices or
practical implementations.

For huge matrices, it can provide a significant speedup.

Additionally, further optimized algorithms like Coppersmith-Winograd algorithm


have been developed to improve matrix multiplication even more, especially for
huge matrices.
Pseudo code for Strassen’s matrix multiplication
function Strassen(A, B):
if size(A) == 1:
return A[0][0] * B[0][0]

// Split matrix A and B into quadrants


n = size(A) / 2
A11, A12, A21, A22 = A[0:n][0:n], A[0:n][n:], A[n:][0:n], A[n:][n:]
B11, B12, B21, B22 = B[0:n][0:n], B[0:n][n:], B[n:][0:n], B[n:][n:]

// Compute the 7 products


M1 = Strassen(A11 + A22, B11 + B22)
M2 = Strassen(A21 + A22, B11)
M3 = Strassen(A11, B12 - B22)
M4 = Strassen(A22, B21 - B11)
M5 = Strassen(A11 + A12, B22)
M6 = Strassen(A21 - A11, B11 + B12)
M7 = Strassen(A12 - A22, B21 + B22)
// Combine products to get the final result
C11 = M1 + M4 - M5 + M7
C12 = M3 + M5
C21 = M2 + M4
C22 = M1 - M2 + M3 + M6

return combine(C11, C12, C21, C22)

function combine(C11, C12, C21, C22):


n = size(C11)
C = new matrix of size 2n
C[0:n][0:n] = C11
C[0:n][n:] = C12
C[n:][0:n] = C21
C[n:][n:] = C22
return C

This pseudocode represents the Strassen algorithm, which achieves faster matrix multiplication compared
to the conventional approach by recursively breaking down the problem.
Python implementation of Strassen's algorithm for matrix multiplication.
This implementation assumes that the input matrices are square matrices of size 2n×2n for some
integer n. If the matrices are not of that size, you would typically need to pad them with zeros to the
next power of two.

Here's the implementation:

import numpy as np

def add_matrix(A, B):


"""Add two matrices A and B."""
return A + B

def subtract_matrix(A, B):


"""Subtract matrix B from matrix A."""
return A - B

def strassen(A, B):


"""Perform matrix multiplication using Strassen's algorithm."""
if len(A) == 1: # Base case for 1x1 matrix
return A * B
# Splitting the matrices into quadrants
mid = len(A) /2
A11 = A[mid, mid]
A12 = A[mid, mid]
A21 = A[mid, mid]
A22 = A[mid, mid]

B11 = B[mid, mid]


B12 = B[mid, mid]
B21 = B[mid, mid]
B22 = B[mid, mid]

# Applying Strassen's formulas


P1 = strassen(A11, subtract_matrix(B12, B22)) # P1 = A11 * (B12 - B22)
P2 = strassen(add_matrix(A11, A12), B22) # P2 = (A11 + A12) * B22
P3 = strassen(add_matrix(A21, A22), B11) # P3 = (A21 + A22) * B11
P4 = strassen(A22, subtract_matrix(B21, B11)) # P4 = A22 * (B21 - B11)
P5 = strassen(add_matrix(A11, A22), add_matrix(B11, B22)) # P5 = (A11 + A22) * (B11 + B22)
P6 = strassen(subtract_matrix(A12, A22), add_matrix(B21, B22)) # P6 = (A12 - A22) * (B21 + B22)
P7 = strassen(subtract_matrix(A11, A21), add_matrix(B11, B12)) # P7 = (A11 - A21) * (B11 + B12)
# Combining the results
C11 = add_matrix(subtract_matrix(add_matrix(P5, P4), P2), P6)
C12 = add_matrix(P1, P2)
C21 = add_matrix(P3, P4)
C22 = add_matrix(subtract_matrix(add_matrix(P5, P1), P3), P7)

# Assemble the resultant matrix


C = np.zeros((len(A), len(A))) # Create an empty matrix for the result
C[:mid, :mid] = C11
C[:mid, mid:] = C12
C[mid:, :mid] = C21
C[mid:, mid:] = C22

return C

You might also like