![]() Chapter 28: Section 28.2: Strassen's algorithm for matrix multiplication, pp. Introduction to Algorithms, Second Edition. Thanks to Strassen's Algorithm, we were able to shorten the time it takes to calculate the inner product between two matrices. In this article we have implemented Strassen Algorithm in pure python and experimented on its speed. Below is a graph that shows how long it takes for each algorithm (naive, divide-and-conquer, and Strassen) to finish the inner product calculation. ![]() ![]() def strassen_multiplication(A: List], B: List], n_switch= 128) -> List]: Multiplying matrices with sizes the set value, we resort to using the naive algorithm. Instead of base case being, we can set it to. We can optimize the divide-and-conquer algorithm by changing the base case. This might be because of the time it takes to fill in and call the functions in the call stack when we do recursions. * In multiplying small square matrices, I found out that the naive algorithm is the fastest. Divide matrices A and B in 4 sub-matrices of size N/2 x N/2 as shown in the below diagram. Important points that I discovered as I was experimenting with the implemented algorithm are the following. Divide and Conquer : Following is simple Divide and Conquer method to multiply two square matrices. Since we have only 7 subproblems, by using Master Theorem, we have which translates to time complexity. Strassen’s algorithm multiplies two matrices in O (n2.8974) time. Implementing this in pseudo-code, we get something like this: def strassen_multiplication(A: List], B: List]) -> List]: A simple method to multiply two matrices needs 3 nested loops and is O (n3). The computations for the 7 subproblems are listed below. Strassen Algorithm ImplementationĪs mentioned in the previous part of this article, the Strassen Algorithm uses 7 subproblems only. This is where Strassen's algorithm comes in. Can we do better than this? Yes! It turns out that we can reduce the number of subproblems in this recursion from 8 to 7. Therefore, the whole matrix multiplication algorithm has time complexity which is the same as the naive algorithm's time complexity. This results in this equation: which implies that the multiplication part alone has. For the multiplication part, we will have to apply Master Theorem. Notice that for each part of submatrix, we are conducting multiplication and addition of matrix. # Colate results for submatrices # Recurse until 1 x 1Ĭ = combine_results(C_00, C_01, C_10, C_11) Implementing this in pseudo-code, def multiply_matrix(A: List], B: List]) -> List]: We recurse until the submatrix shape is 1 x 1.We split each matrix A and B into four submatrices, each submatrix having N/2 by N/2 shape.Matrix Multiplication Divide-and-Conquer Algorithm:Īssuming we are dealing with A x B and each matrix have N by N shape, We use a divide-and-conquer approach and try optimizing the naive algorithm. def multiply(A: List], B: List]):Ĭ = for _ in range(n)] Because of the three for loops each running a calculation n times, we conclude that the naive solution runs at. Contentsīelow is the naive implementation of matrix multiplication. In this article, we will focus on the core idea of optimizing square matrix multiplication. In the modern day, we have GPU to compute large matrix operations and even though implementing a naive algorithm on a GPU is still fast, there is still a need for optimizing algorithms implemented on GPU. In machine learning and/or deep learning, we deal with large matrices and their computations. Implementing fast and efficient algorithms is one of the many skills a computer scientist is expected to have. Var sparePeg = hanoi.getSparePeg(fromPeg, toPeg) īut despite all that, even though it only seems to ask for the solveHanoi() to be filled out with the parameters, it wont progress.Hi! This is Mark Bajo and I am here to share an algorithm for fast multiplication of square matrices. You can find the spare peg by using the getSparePeg function.Ī call to hanoi.getSparePeg(peg1,peg2) returns the remaining peg that isn't peg1 or peg2. move (numDisks - 1) disks to the spare peg. Make a recursive function call to move the disks sitting on top of the bottom disk on the fromPeg to the spare peg, i.e. Posting here really about the(just prior to this page) stage 2 Challenge Solve hanoi recursively (no place to put questions on that page). Θ ( n lg n ) \Theta(n \lg n) Θ ( n l g n ) \Theta, left parenthesis, n, \lg, n, right parenthesis Θ ( n ) \Theta(n) Θ ( n ) \Theta, left parenthesis, n, right parenthesis Θ ( n 2 ) \Theta(n^2) Θ ( n 2 ) \Theta, left parenthesis, n, squared, right parenthesis
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |