Data Structures and Algorithms
with Object-Oriented Design Patterns in C# |
Given an matrix A and an matrix B, the product C=AB is an matrix. The elements of the result matrix are given by
Accordingly, in order to compute the produce matrix, C, we need to compute mp summations each of which is the sum of n product terms. An algorithm to compute the matrix product is given in Program . The algorithm given is a direct implementation of Equation .
Program: DenseMatrix class times method.
The algorithm begins by checking to see that the matrices to be multiplied have compatible dimensions. That is, the number of columns of the first matrix must be equal to the number of rows of the second one. This check takes O(1) time in the worst case.
Next a matrix in which the result will be formed is constructed (line 12-13). The running time for this is O(mp). For each value of i and j, the innermost loop (lines 19-20) does n iterations. Each iteration takes a constant amount of time.
The body of the middle loop (lines 16-22) takes time O(n) for each value of i and j. The middle loop is done for p iterations, giving the running time of O(np) for each value of i. Since, the outer loop does m iterations, its overall running time is O(mnp). Finally, the result matrix is returned on line 24. This takes a constant amount of time.
In summary, we have shown that lines 9-11 are O(1); lines 12-13 are O(mp); lines 14-23 are O(mnp); and line 24 is O(1). Therefore, the running time of the canonical matrix multiplication algorithm is O(mnp).