From db95498514e3550e85d408f252a2b9ef5237e113 Mon Sep 17 00:00:00 2001 From: Shivansh Date: Thu, 8 Jan 2026 12:58:42 +0530 Subject: [PATCH 1/2] docs: add complexity notes to Strassen algorithm --- .../strassen_matrix_multiplication.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/divide_and_conquer/strassen_matrix_multiplication.py b/divide_and_conquer/strassen_matrix_multiplication.py index f529a255d2ef..efafd0c09a0b 100644 --- a/divide_and_conquer/strassen_matrix_multiplication.py +++ b/divide_and_conquer/strassen_matrix_multiplication.py @@ -75,6 +75,13 @@ def actual_strassen(matrix_a: list, matrix_b: list) -> list: """ Recursive function to calculate the product of two matrices, using the Strassen Algorithm. It only supports square matrices of any size that is a power of 2. + Complexity: + - Strassen’s algorithm runs in O(n^(log2(7))) ≈ O(n^2.81), + which is asymptotically faster than the classical matrix multiplication + algorithm O(n^3). + - For small matrices, Strassen may be slower due to overhead, so it is + typically beneficial for large n. + """ if matrix_dimensions(matrix_a) == (2, 2): return default_matrix_multiplication(matrix_a, matrix_b) @@ -106,10 +113,20 @@ def actual_strassen(matrix_a: list, matrix_b: list) -> list: def strassen(matrix1: list, matrix2: list) -> list: """ +Perform matrix multiplication using Strassen’s algorithm. + + Examples: >>> strassen([[2,1,3],[3,4,6],[1,4,2],[7,6,7]], [[4,2,3,4],[2,1,1,1],[8,6,4,2]]) [[34, 23, 19, 15], [68, 46, 37, 28], [28, 18, 15, 12], [96, 62, 55, 48]] + >>> strassen([[3,7,5,6,9],[1,5,3,7,8],[1,4,4,5,7]], [[2,4],[5,2],[1,7],[5,5],[7,8]]) [[139, 163], [121, 134], [100, 121]] + + Complexity Notes: + - Classical matrix multiplication: O(n^3). + - Strassen’s algorithm: O(n^(log2(7))) ≈ O(n^2.81). + - Strassen reduces the number of multiplications from 8 to 7 per recursion, + trading them for additional additions/subtractions. """ if matrix_dimensions(matrix1)[1] != matrix_dimensions(matrix2)[0]: msg = ( From dda49c5d8e985408f96e20868cd2cd7fdb4df7a3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 8 Jan 2026 07:30:56 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../strassen_matrix_multiplication.py | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/divide_and_conquer/strassen_matrix_multiplication.py b/divide_and_conquer/strassen_matrix_multiplication.py index efafd0c09a0b..9268b6d7c103 100644 --- a/divide_and_conquer/strassen_matrix_multiplication.py +++ b/divide_and_conquer/strassen_matrix_multiplication.py @@ -81,7 +81,7 @@ def actual_strassen(matrix_a: list, matrix_b: list) -> list: algorithm O(n^3). - For small matrices, Strassen may be slower due to overhead, so it is typically beneficial for large n. - + """ if matrix_dimensions(matrix_a) == (2, 2): return default_matrix_multiplication(matrix_a, matrix_b) @@ -113,20 +113,20 @@ def actual_strassen(matrix_a: list, matrix_b: list) -> list: def strassen(matrix1: list, matrix2: list) -> list: """ -Perform matrix multiplication using Strassen’s algorithm. + Perform matrix multiplication using Strassen’s algorithm. - Examples: - >>> strassen([[2,1,3],[3,4,6],[1,4,2],[7,6,7]], [[4,2,3,4],[2,1,1,1],[8,6,4,2]]) - [[34, 23, 19, 15], [68, 46, 37, 28], [28, 18, 15, 12], [96, 62, 55, 48]] + Examples: + >>> strassen([[2,1,3],[3,4,6],[1,4,2],[7,6,7]], [[4,2,3,4],[2,1,1,1],[8,6,4,2]]) + [[34, 23, 19, 15], [68, 46, 37, 28], [28, 18, 15, 12], [96, 62, 55, 48]] - >>> strassen([[3,7,5,6,9],[1,5,3,7,8],[1,4,4,5,7]], [[2,4],[5,2],[1,7],[5,5],[7,8]]) - [[139, 163], [121, 134], [100, 121]] + >>> strassen([[3,7,5,6,9],[1,5,3,7,8],[1,4,4,5,7]], [[2,4],[5,2],[1,7],[5,5],[7,8]]) + [[139, 163], [121, 134], [100, 121]] - Complexity Notes: - - Classical matrix multiplication: O(n^3). - - Strassen’s algorithm: O(n^(log2(7))) ≈ O(n^2.81). - - Strassen reduces the number of multiplications from 8 to 7 per recursion, - trading them for additional additions/subtractions. + Complexity Notes: + - Classical matrix multiplication: O(n^3). + - Strassen’s algorithm: O(n^(log2(7))) ≈ O(n^2.81). + - Strassen reduces the number of multiplications from 8 to 7 per recursion, + trading them for additional additions/subtractions. """ if matrix_dimensions(matrix1)[1] != matrix_dimensions(matrix2)[0]: msg = (