Strassen's Algorithm

Distribute Education like Computer Geek

Strassen’s Algorithm: Fast Matrix Multiplication

Introduction

Strassen’s Algorithm is a revolutionary approach in the field of Algorithm Design & Analysis that improved the efficiency of matrix multiplication.

Traditional matrix multiplication algorithms, such as the naive approach, exhibit a time complexity of O(n3), which can become a bottleneck when dealing with large matrices.

Volker Strassen invented Strassen’s algorithm in 1969. The algorithm is a matrix multiplication algorithm for linear algebra. Strassen’s Algorithm offers a more efficient alternative, with a time complexity of approximately O(n^log2(7)) which is O(n2.81).

Let’s go into the inner workings of Strassen’s Algorithm and understand how it optimizes matrix multiplication.

.

Strassen’s AlgorithmDivide and Conquer

Strassen’s Algorithm employs a divide-and-conquer strategy to reduce the number of multiplications required to compute the matrix product. The algorithm breaks down the input matrices into smaller submatrices, each with dimensions halved. This recursive subdivision continues until the base case is reached, typically matrices of size 1×1.

.

Only Seven Multiplications needed

Strassen’s Algorithm uses just seven multiplications every recursive step, as compared to the traditional method, which takes eight different multiplications to compute each element of the resultant matrix. The approach considerably reduces the overall number of multiplications required by intelligently combining these seven multiplications.

.

Intermediate Matrix Products

Strassen’s Algorithm computes intermediate matrix products, which are combinations of the submatrices, during the recursive calls. These intermediate products are created by using specific equations to add or subtract correctly sized submatrices. The algorithm runs these equations iteratively until the beginning case is reached.

.

Combining Intermediate Products

After reaching the beginning situation and reducing the submatrices to 1×1 matrices, the algorithm continues merging the intermediate products to achieve the final output. Strassen’s Algorithm effectively combines these intermediate results by using addition and subtraction operations on correctly sized submatrices.

.

Working of Strassen’s Algorithm

This algorithm makes use of the same divide and conquer approach as in the matrix multiplication, but instead uses only 7 recursive multiplications rather than 8.

Here we can save one recursive call, but have several new additions of n/2*n/2 matrices.

n/2*n/2 matrices of recursive call

ProductStrassen's Algorithm formula

P1 = A11 * (B12 – B22)

P2 = (A11 + A12) * B22

P3 = (A21 + A22) * B11

P4 = A22 * (B21 – B11)

P5 = (A11 + A22) * (B11 + B22)

P6 = (A12 – A22) * (B21 + B22)

P7 = (A11 – A21) * (B11 + B12)

.

Matrix C

C11 = P5 + P4 – P2 + P6

C12 = P1 + P2

C21 = P3 + P4

C22 = P5 + P1 – P3 – P7

In this example,

Strassen's Algorithm formula

From master’s theorem

Time Complexity = O(n^log27) or O(n2.81)

Which beats the divide & conquer approach asymptotically.

.

Limitations and Considerations

While Strassen’s Algorithm offers improved time complexity, it does have some limitations and considerations to keep in mind.

Strassen's Algorithm graph

From the diagram, we can see that the naïve algorithm or traditional matrix multiplication has a lower time cost than the Strassen’s multiplication in the first halve.

But later on, in the second halve, the naïve multiplication has a higher time complexity than the Strassen’s multiplication.

.

Algorithm for Strassen’s Method

1. Divide the input matrices, A and B, into four equally sized submatrices

A11, A12, A21, and A22 for matrix A

B11, B12, B21, and B22 for matrix B

.

2. Create seven new matrices, P1 to P7, as follows

P1 = A11 * (B12 – B22)

P2 = (A11 + A12) * B22

P3 = (A21 + A22) * B11

P4 = A22 * (B21 – B11)

P5 = (A11 + A22) * (B11 + B22)

P6 = (A12 – A22) * (B21 + B22)

P7 = (A11 – A21) * (B11 + B12)

.

3. Recursively compute the following intermediate matrices

C11 = P5 + P4 – P2 + P6

C12 = P1 + P2

C21 = P3 + P4

C22 = P5 + P1 – P3 – P7

4. Combine the intermediate matrices to form the resulting matrix, C

Advantage of Strassen’s Algorithm
  1. Strassen’s algorithm has a lower asymptotic complexity compared to the traditional matrix multiplication algorithm.
  2. Strassen’s algorithm performs well for large matrices due to its recursive nature. By dividing the matrices into smaller submatrices and recursively applying the algorithm, it reduces the overall number of multiplications required.
Disadvantage of Strassen’s Algorithm
  1. Increased Overhead
  2. Precision and Rounding Errors
  3. Memory Usage for product.
  4. Not Suitable for Irregular Matrices. Strassen’s algorithm assumes that the input matrices have dimensions that are powers of 2.
  5. Also, It is just for the computers. Humans always use the traditional method because it is simple. Humans want convenience and therefore use the traditional method.

Applications of Strassen’s Multiplication
  1. Scientific Computing
  2. Image filtering, compression, and pattern recognition
  3. Machine Learning and Data Analysis.
  4. Speed up cryptography.
  5. Parallel and Distributed Computing.

Test Yourself

Q1- Explain the concept of Strassen’s Algorithm and how it improves matrix multiplication efficiency.

 

Strassen’s Algorithm is a divide-and-conquer approach that reduces the number of multiplications needed to compute the matrix product. By breaking down the matrices into smaller submatrices and using specific equations to combine intermediate results, it reduces the time complexity to approximately O(n^log2(7)). This optimization improves the efficiency of matrix multiplication compared to traditional algorithms.

Q2- Discuss the limitations and considerations of Strassen’s Algorithm.

The algorithm performs better for larger matrices, but the initial stages may be slower than traditional multiplication. Additionally, it requires the input matrices to have dimensions that are powers of 2, and precision and rounding errors can occur due to floating-point arithmetic.

Also, It is just for the computers. Humans always use the traditional method because it is simple. 

Humans want convenience and therefore use the traditional method.

Q3- How does Strassen’s Algorithm achieve a reduction in the number of multiplications?

Strassen’s Algorithm combines seven multiplications in each recursive step, instead of the traditional eight multiplications. By strategically computing intermediate matrix products and applying addition and subtraction operations, it intelligently reduces the overall number of multiplications required.

Q4- Describe the recursive nature of Strassen’s Algorithm.

Strassen’s Algorithm uses a recursive divide-and-conquer strategy. It breaks down the input matrices into smaller submatrices, each with dimensions halved, and applies the algorithm recursively until the base case is reached (typically 1×1 matrices).

Q5- Can Strassen’s Algorithm be applied to matrices with non-power-of-2 dimensions?

Strassen’s Algorithm assumes that the input matrices have dimensions that are powers of 2. Therefore, it may not directly apply to matrices with non-power-of-2 dimensions without additional preprocessing steps to adjust the dimensions.

Q6- What is the main advantage of Strassen’s Algorithm?
  1. Reduced memory usage
  2. Faster for small matrices
  3. Improved efficiency for large matrices
  4. Simplicity of implementation

Ans – (3)

Explanation – Strassen’s Algorithm provides better efficiency for large matrices due to its recursive nature and reduction in the number of multiplications.

Q7- What type of matrices does Strassen’s Algorithm require as input?
  1. Matrices with any dimensions
  2. Matrices with dimensions that are powers of 2
  3. Matrices with prime dimensions
  4. Matrices with equal dimensions

Ans – (2)

Explanation – Strassen’s Algorithm assumes that the input matrices have dimensions that are powers of 2 to ensure proper recursive subdivision.

Q8- Which step of Strassen’s Algorithm involves combining intermediate matrix products?
  1. Divide the input matrices
  2. Recursive computation of intermediate matrices
  3. Merge submatrices to form the final output
  4. Compute intermediate matrix products using specific equations

Ans – (3)

Explanation – The final step of Strassen’s Algorithm involves merging the intermediate matrices obtained from the recursive calls to obtain the final output matrix.

Q9- What is the main limitation of Strassen’s Algorithm?
  1. Higher memory usage
  2. Limited applicability to small matrices
  3. Inability to handle non-square matrices
  4. Precision and rounding errors

Ans – (4)

Explanation – Strassen’s Algorithm, like any floating-point arithmetic, can introduce precision and rounding errors due to the nature of the calculations involved.

Q10- Which traditional algorithm does Strassen’s Algorithm improve upon?
  1. Bubble sort
  2. Quick sort
  3. Matrix addition
  4. Matrix multiplication

Ans – (4)

Explanation – Strassen’s Algorithm is an optimization for matrix multiplication, improving the efficiency of this operation compared to traditional algorithms.

Q11- What is the main advantage of Strassen’s Algorithm in scientific computing applications?
  1. Higher precision of results
  2. Ability to handle irregular matrices
  3. Reduced computational complexity
  4. Improved parallelization potential

Ans – (4)

Explanation – Strassen’s Algorithm’s recursive nature and reduced number of multiplications allow for easier parallelization, which is beneficial in scientific computing applications.

Q12- What is the base case in Strassen’s Algorithm?
  1. Matrices with dimensions 1×1
  2. Matrices with dimensions 2×2
  3. Matrices with dimensions 3×3
  4. Matrices with dimensions 4×4

Ans – (1)

Explanation – The base case in Strassen’s Algorithm is typically reached when the input matrices are reduced to 1×1 size, and direct multiplication can be performed.

Q13- Which operation is NOT involved in Strassen’s Algorithm?
  1. Matrix addition
  2. Matrix subtraction
  3. Matrix division
  4. Matrix multiplication

Ans – (3)

Explanation – Strassen’s Algorithm uses matrix addition, subtraction, and multiplication, but it does not involve matrix division.

Q14- What is the main trade-off of using Strassen’s Algorithm?
  1. Higher memory usage
  2. Slower execution for small matrices
  3. Inaccuracy in results
  4. Difficulty in implementation

Ans – (1)

Explanation – Strassen’s Algorithm requires additional memory to store the submatrices and intermediate results, which can result in higher memory usage compared to traditional matrix multiplication algorithms.

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha