Download Article
Total Page:16
File Type:pdf, Size:1020Kb
International Conference on Software Engineering and Computer Science (ICSECS2013) Analysis of the Time Complexity of Strassen Algorithm Xiang Wang School of Information and Electronic Engineering Tianjin Vocational Institute Tianjin, China e-mail: [email protected] Abstract—Algorithms of matrix multiplication are widely used The function of matrix multiplication is as follows: in software engineering field. Application of Strassen template<class Type> 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 multiplication algorithm is very Type c[][N]; important. This paper talks about the time complexity of Strassen algorithm and general algorithm for matrix for(int k=0;k<m;k++ ) for(int u=0;u<n;u++ ) multiplication, and makes a comparison between the two algorithm routines so as to discuss the advantages and for(int v=0;v<p;v++) disadvantages of Strassen algorithm. The rational utilization c[k][u]=c[k][u]+a[k][v]*b[v][u]; 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., AA11 12 BB11 12 A B AAA11 12 1s BBB11 12 1t AA21 22 BB21 22 AAA21 22 2s BBB21 22 2t A B . ABABABAB 11 11 12 21 11 12 12 22 AB (3) ABABABAB21 11 22 21 21 12 22 22 AAAr1 r 2 rs BBBs1 s 2 st CCC 11 12 1t In this case, if we adopt the general algorithm for matrix CCC21 22 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 r1 r 2 rt (1) following recursive expression, i.e., Thus AB can be formed blockwise, yielding as an (m×p) 1 n 1 matrix with r row partitions and t column partitions. The 2 matrices in your matrix are calculated by multiplying: T() n n n (4) 8T 4 n 1 2 2 s CAB and i=1,…,r;j=1,…,t. (2) ij i j 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 mathematician 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: 7T2 18 2 F1=A11(B12-B22), 2-k 2-k 2 k-1 2 F2=(A11+A12)B22, 77T2 18 2 18 2 F3=(A21+A22)B11, 2 k-2 2-k 2 k-1 2 F4=A22(B21-B11), 7 T 2 7 18 2 18 2 F5=(A11+A22)(B11+B22), 2 k-3 3-k 2 F6=(A12-A22)(B21+B22), 7 7T2 18 2 F7=(A -A )(B +B ) 11 21 11 12 2 2 Thus 7 18 22-k 18 2 1-k 3 k-3 2 3-k 2 FFFFFF 7 T 2 7 18 2 AB 5 4 2 6 1 2 2-k 2 1-k 2 FFFFFF3 4 1 5 3 7 7 18 2 18 2 (5) k1 2 The numbers of n/2×n/2 matrix multiplication may set 7i 182 1-i-k back to 7 through the Strassen algorithm, when compared i0 with the general algorithm for matrix multiplication. This k1 i meantime, there are 18 matrix addition or matrix subtraction. 2 7 182k As n is a power of 2, if let k be nonnegative integer, then n i 1 2 is k-th power of 2. If k=0, then the time complexity of i0 2 matrix multiplication is 1. If k>0, generally we can explain k1 i time complexity of matrix multiplication through the 9 k 2 7 following recursive expression, 2 2 i0 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, 6 7k 6 4k Since 1 n 1 2 k log2 n T() n n n 7 7 7T 81 n 1 2 2 log2 n i.e., (7) logn 7 n If n>1, then nlog2 n logn 7 log2 7 2 log2 n n n n log2 n T(n) 7T 81 2 2 log 7 (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 k2 k 2log2 n k 4 2 2 k 2 1 2 2 2 log2 n 2 1 2 2 n , 23k 2 2 k Then, we have 23log2 n 22log2 n 1 n 1 3 2 T() n .2 81 2 n n 6n 6 n n 1 (9) Thus, we have This meantime, we can explain the time complexity through the following expression, i.e., O(n2.81). It is evident 1 n 1 that the Strassen algorithm was more efficient than the T() n 3 2 (11) general algorithm for matrix multiplication. In fact, if n is n n n 1 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 3 2 2.81 2 design requirement above is achieved, the dimension will K( n ) ( n n ) ( n 6 n ) increase twofold at most. Thus, the time complexity of i.e., K() n n3 n 2.81 5 n2 (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 K( n ) 3 n 2.81 n 10 n (13) k power of 2, i.e., n=2 . When n>1, we can deeply analyze K( n ) 6 n 5.0861 n .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 K( n ) 0 and 2 n n K( n ) 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 T(n) 8T 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. 2 8T 2k1 4 2k1 However, the general algorithm for matrix multiplication is no problem on this aspect. Tests show when the dimension k1 of matrix is less than 500, this situation will get even worse. 2 8i 4 2 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 i0 Strassen algorithm or the general algorithm for matrix k1 i k 2 8 multiplication based on the comparisons between the 4 2 dimension and the numeric value. i 1 2 i0 2 The function of Strassen algorithm is as follows: k1 k 2 i 2 2 i0 105 subtraction) to decrease the number of block matrix template<class Type> 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.