International Conference on Software Engineering and Computer Science (ICSECS2013)

Analysis of the Time Complexity of

Xiang Wang School of Information and Electronic Engineering Tianjin Vocational Institute Tianjin, China e-mail: [email protected]

Abstract—Algorithms of are widely used The function of matrix multiplication is as follows: in software engineering field. Application of Strassen template algorithm makes a significant contribution to optimize the Type M (Type a[][N], Type b[][N],int m, int n, int p) algorithm . Therefore, thorough study based on time { complexity of matrix is very Type c[][N]; important. This paper talks about the time complexity of for(int k=0;k

plan of matrix multiplication algorithms is also discussed. return *c; } Keywords-algorithms for matrix multiplication; time complexity; Strassen algorithm We can obtain the following conclusion very easily: time complexity of a general algorithm for matrix I. A GENERAL ALGORITHM FOR MATRIX multiplication depends on the rows and columns of matrix A MULTIPLICATION and matrix B., and generally we can explain the time Let A=(aij)m × n be an m × n matrix, and let B=(bij)n × p complexity through the following expression, i.e., be an n × p matrix. Given an (m×p) matrix A with r row O(m×n×p). If matrix A and matrix B are both n × n matrices, partitions and s column partitions and a (p×n) matrix with s A×B is just a special case of matrix multiplication. And if n row partitions and t column partitions that are compatible is a power of 2, both matrix A and matrix B can be with the partitions of A, the matrix product C=A×B. partitioned into 4 n/2×n/2 blocks. The partitioned matrix can generally we can explain the matrix multiplication through then be written as the following expression, i.e.,

 AA 1211   BB 1211  A    B     11 12  AAA 1s   11 12  BBB 1t           AA 2221   BB 2221   21 22  AAA 2s   21 22  BBB 2t  A  B  .    BABABABA        21121111 22121211     AB    (3)       122121221121  BABABABA 2222   rr 21  AAA rs   ss 21  BBB st    CCC   11 12 1t  In this case, if we adopt the general algorithm for matrix  21 22  CCC 2t  multiplication, then we need make eight n/2×n/2 Matrix AB  Multiplications and four Matrix additions (or matrix      subtractions). Let T(n) be time complexity of matrix   CCC  multiplication, we can explain the T(n) through the  rr 21 rt  (1) following recursive expression, i.e.,

Thus AB can be formed blockwise, yielding as an (m×p)   1n1 matrix with r row partitions and t column partitions. The  2 matrices in your matrix are calculated by multiplying: nT )(   n   n  (4) 8T   4   1n   2   2  s   BAC and i=1,…,r;j=1,…,t. (2) ij   ji and generally we can explain the time complexity  1 through the following expression, i.e., O(n3).

© 2013. The authors - Published by Atlantis Press 103 II. ANALYSIS OF TIME COMPLEXITY OF THE STRASSEN 2  2k   2k  ALGORITHM T(n)  7T   81   Strassen algorithm used for matrix multiplication, which  2   2  is invented by the German Volker Strassen     in 1969, is considered as one of the fastest and best matrix 1-k 1-k 2 multiplication algorithms. We define 7 new matrices: 7T  2812  F1=A11(B12-B22), 2-k 2-k 2 k-1 2 F2=(A11+A12)B22,  77T     2812812  F3=(A21+A22)B11, 2-k2 2-k 2 k-1 2 F4=A22(B21-B11),      28128172T7  F5=(A11+A22)(B11+B22), 3-k2 3-k 2 F6=(A12-A22)(B21+B22),  7 7T  2812   F7=(A -A )(B +B ) 11 21 11 12 2 2 Thus 2-k  2812817 1-k  2   23-k3  28172T7 3-k    6245  FFFFFF 21  AB    2 2    FFFFFF  2-k  2812817 1-k  43 7351  (5)     k1 2 The numbers of n/2×n/2 matrix multiplication may set  7i  182 1-i-k  back to 7 through the Strassen algorithm, when compared i0 with the general algorithm for matrix multiplication. This k1 i meantime, there are 18 matrix addition or matrix subtraction. 2 7  182k  As n is a power of 2, if let k be nonnegative integer, then n  1i 2 is k-th power of 2. If k=0, then the time complexity of i0 2  matrix multiplication is 1. If k>0, generally we can explain k1 i time complexity of matrix multiplication through the 9 k 2  7  following recursive expression,  2    2 i0  4  k 2  7   n   n  1   7T   81   9 k 2  4  2 2 2   i.e.,     (6) 2 7 1 4 so we can explain the T(n) through the following recursive expression, k  4676 k Since  1  1n  2 k log2 n nT )(   n   n   77 7T   81    1n  2 2 log2 n i.e.,      (7) n 7log  n If n>1, then  n 2 n n 7loglog 2 7log 2 log2 n  n   n   n log2 n T(n)  7T   81   2 2 7log     (8)  n 2 We come next to the analysis of equation(8). We replace  n .2 807355 variable n with 2k. .2 81  n

and

104 2kk log2 2 n k  224 k 2  21 2 2   log2 n 2  21 2  n ,  22 23 kk Then, we have log3 2 n  22 log2 2 n   1n1 23 nT )(   .2 81 2  nn  nn  1n66 (9) Thus, we have This meantime, we can explain the time complexity through the following expression, i.e., O(n2.81). It is evident   1n1 that the Strassen algorithm was more efficient than the nT )(   23 (11) general algorithm for matrix multiplication. In fact, if n is  nn  1n not a power of 2, the matrix can be embedded into another matrix (let the matrix be matrix E) whose number of Let us begin to consider the behavior of a new function: dimension is a power of 2. In matrix multiplication, if the dimension of matrix E take minimum value within the .223 81 2 design requirement above is achieved, the dimension will  nnnnnK )6()()( increase twofold at most. Thus, the time complexity of i.e., )( .23 81  5nnnnK 2 (12) Strassen algorithm can still be described as O(n2.81).

III. VARIATION TRENDS OF TIME COMPLEXITY OF THE We can easily get the first order derivative and second STRASSEN ALGORITHM order derivative of the function, such that

For the general algorithm for matrix multiplication, let n 2 .1 81 be a power of 2. If let k be nonnegative integer, then n is k-th  .23)(  1081 nnnnK (13) k power of 2, i.e., n=2 . When n>1, we can deeply analyze   .56)( 0861nnnK .0 81 10 (14) time complexity of the general algorithm for matrix multiplication through the following recursive expression, We can obtain that if n>1, then  nK  0)( and 2  n   n   nK  0)( . This implies that Strassen algorithm is always T(n)  8T   4  better than the general algorithm for matrix multiplication in  2   2  (10) terms of time complexity. And the advantage of the Strassen algorithm will became more obvious with the increase of We come next to the analysis of equation(10). We dimension of matrix. Strassen algorithm adopts the method replace variable n with 2k. of recursive algorithms, which is helpful to the implementation and analysis of the algorithm. However, the 2 method make the algorithm require a great number of  2k   2k   8T(n) T   4  dynamic two dimensional arrays so as to assign memory  2   2  space. which enhance the time complexity and space     complexity when the dimension of matrix is smaller. k1 k1 2 However, the general algorithm for matrix multiplication is T  2428  no problem on this aspect. Tests show when the dimension k1 of matrix is less than 500, this situation will get even worse. 2  i  248 1-i-k In order to solve this problem, we can specify a numeric    value (such as 500) for algorithm routine, and we will adopt i0 Strassen algorithm or the general algorithm for matrix k1 i k 2 8 multiplication based on the comparisons between the  24  dimension and the numeric value.  1i 2 i0 2  The function of Strassen algorithm is as follows: k1 k 2 i     22 i0

105 subtraction) to decrease the number of block matrix template multiplication, and adopts divide and conquer algorithm to void S (int n,Type A[][N],Type B[][N],Type AB[][N]) complete matrix multiplication through recursive function. { Such methods can effectively reduce the time complexity of ...// definition of related array matrix multiplication. And Within the algorithm program, int i,j; the computing process is accomplished by the block if (n==2) matrixes(A11,A12,B11,B12)that act as middle variables, ...;//the general algorithm for matrix multiplication and recursive function is accomplished by S(n/2,A11,BB,Mi) else (i=1,2,3,4,5,6,7).While we pay particular attention to the obvious advantages of Strassen algorithm, we should notice { that Strassen algorithm need to create a large number of for(i=0;i

{ computing time to assign memory space. And, the higher

A11[i][j]=A[i][j]; the order of the matrices, the more memory space Strassen A12[i][j]=A[i][j+n/2]; algorithm program will require. Experiments indicate the A21[i][j]=A[i+n/2][j]; algorithm of Strassen is not much faster than the general A22[i][j]=A[i+n/2][j+n/2]; matrix multiplication algorithm when the order of the B11[i][j]=B[i][j]; matrices is relatively small (usually n<45). Therefore, it is B12[i][j]=B[i][j+n/2]; necessary to improve Strassen algorithm through setting a B21[i][j]=B[i+n/2][j]; boundary for the order of the matrices. When the order of B22[i][j]=B[i+n/2][j+n/2]; the matrices is bigger than the boundary we adopt Strassen } algorithm, otherwise we adopt the general matrix //the matrix is partitioned into 4 blocks. multiplication algorithm. S (n/2,A11,BB,M1); ...;// BB=B12-B22 REFERENCES S (n/2,AA,B22,M2); [1] Aho, A.V., Hopcroft, J.E.,and Ullman, J.D.(2007). The Design and ...;//AA=A11+A12 Analysis of Computer Algorithms(Huang, L.P., Wang, D.J., and Zhang, S., Trans.). Beijing, China: China Machine Press.(1974). [2] Deng, X.Y.,and Wan, T.T.(2006). Design and . Beijing, China: Metallurgical Industry Press. S (n/2,AA,B11,M3); [3] Wang, X.D.(2001). The Design and Analysis of Computer ...;// AA=A21+A22 Algorithms. Beijing, China: Publishing House of Electronics Industry. S (n/2,A22,BB,M4); [4] Liu, J.(2003). An Introduction to Computer Algorithm—Techniques ...;// BB=B21-B11 of Design and Analysis. Beijing, China:Science Press. S (n/2,AA,BB,M5); [5] Su, D.F.,and Zhong, C.(2001).The Design and Analysis of Computer Algorithms. Beijing, China: Publishing House of Electronics Industry. ...;//AA=A11+A22 ...;//BB=B11+B22 [6] E.Horowitz.,and S.Sahni.(1978). Fundamentals of Computer Algorithms. U.S.A.: Compter Science Press. S (n/2,AA,BB,M6); ...;//AA=A12-A22 [7] E.Horowitz., S.Sahni.,and S.Rajasekaran.(1996). Computer

...;//BB=B21-B22 Algorithms/C++. U.S.A.: Compter Science Press. S (n/2,AA,BB,M7); [8] T.H.Cormen., C.Leiserson.,and C.Stein.(2001). Introduction to Algorithms. U.S.A:The MIT Press. ...;//AA=A11-A21 [9] Alfred V.Aho.,John E. Hopcroft., and Jeffrey D.Ullman.(1983). Data ...;//BB=B11-B12 Structures and and Algorithms. U.S.A: Addison-Wesley. ...;//AB11=M5+M4-M2+M6 [10] Sara Baase., and A.Gelder.(2000). Computer Algorithms:Introduction ...;//AB12=M1+M2 to Design and Analysis. U.S.A: Addison-Wesley. ...;//AB21=M3+M4 ...;//AB22=M5+M1-M3-M7 for(i=0;i

106