@@ -78,7 +78,23 @@ def print_matrix(matrix: list) -> None:
7878def actual_strassen (matrix_a : list , matrix_b : list ) -> list :
7979 """
8080 Recursive function to calculate the product of two matrices, using the Strassen
81- Algorithm. It only supports square matrices of any size that is a power of 2.
81+ Algorithm.
82+
83+ Time complexity:
84+ The recurrence is T(n) = 7 T(n/2) + \u0398 (n^2), which solves to
85+ T(n) = \u0398 (n^{log_2 7}) \u2248 \u0398 (n^{2.8074}). This is asymptotically
86+ faster than the naive \u0398 (n^3) algorithm for sufficiently large n.
87+
88+ Space complexity:
89+ Uses additional memory for temporary submatrices and padding; overall
90+ space complexity is O(n^2).
91+
92+ Notes:
93+ This function expects square matrices whose size is a power of two.
94+ Matrices of other sizes are handled by `strassen` which pads to the
95+ next power of two.
96+
97+ It only supports square matrices of any size that is a power of 2.
8298
8399 Strassen's algorithm reduces the number of recursive multiplications needed to
84100 multiply two n x n matrices from the 8 required by the naive divide-and-conquer
@@ -123,13 +139,24 @@ def actual_strassen(matrix_a: list, matrix_b: list) -> list:
123139
124140def strassen (matrix1 : list , matrix2 : list ) -> list :
125141 """
126- Multiplies two matrices using Strassen's algorithm, which runs in
142+ Multiply two matrices using Strassen's divide-and-conquer algorithm.
143+
144+ Time complexity:
145+ \u0398 (n^{log_2 7}) \u2248 \u0398 (n^{2.8074})
146+ (recurrence T(n) = 7 T(n/2) + \u0398 (n^2)).
147+
148+ Space complexity:
149+ O(n^2) due to padding and temporary matrices used during recursion.
150+
151+ Multiply two matrices using Strassen's algorithm, which runs in
127152 O(n^log2(7)) ~= O(n^2.807) time, compared to O(n^3) for naive matrix
128153 multiplication. This implementation pads both input matrices with zeros
129154 until they are square matrices whose dimension is a power of 2 (required
130155 by the divide-and-conquer recursion in actual_strassen), performs the
131156 multiplication, then trims the padding back off the result.
132157
158+ Examples:
159+
133160 >>> 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]])
134161 [[34, 23, 19, 15], [68, 46, 37, 28], [28, 18, 15, 12], [96, 62, 55, 48]]
135162 >>> 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]])
0 commit comments