Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matrix multiplication algorithm time complexity

I came up with this algorithm for matrix multiplication. I read somewhere that matrix multiplication has a time complexity of o(n^2). But I think my this algorithm will give o(n^3). I don't know how to calculate time complexity of nested loops. So please correct me.

for i=1 to n    for j=1 to n          c[i][j]=0      for k=1 to n          c[i][j] = c[i][j]+a[i][k]*b[k][j] 
like image 629
zedai Avatar asked Dec 17 '11 17:12

zedai


People also ask

What is the time complexity of multiplication algorithm?

The algorithm has a time complexity of Θ(n log(n) log(log(n))) and is used in practice for numbers with more than 10,000 to 40,000 decimal digits.

Which algorithm is faster for matrix multiplication?

In linear algebra, the Strassen algorithm, named after Volker Strassen, is an algorithm for matrix multiplication. It is faster than the standard matrix multiplication algorithm for large matrices, with a better asymptotic complexity, although the naive algorithm is often better for smaller matrices.

What is the time complexity of naive matrix multiplication?

The "naive" matrix multiplication for A×B involves multiplying and adding N terms for each of MP entries in AB. So the complexity is O(NMP). And then multiplying this M×P matrix by C requires multiplying and adding P terms for each of MN entries. So the total complexity is O(M2N2P2).


2 Answers

Using linear algebra, there exist algorithms that achieve better complexity than the naive O(n3). Solvay Strassen algorithm achieves a complexity of O(n2.807) by reducing the number of multiplications required for each 2x2 sub-matrix from 8 to 7.

The fastest known matrix multiplication algorithm is Coppersmith-Winograd algorithm with a complexity of O(n2.3737). Unless the matrix is huge, these algorithms do not result in a vast difference in computation time. In practice, it is easier and faster to use parallel algorithms for matrix multiplication.

like image 93
viswanathgs Avatar answered Sep 19 '22 14:09

viswanathgs


The naive algorithm, which is what you've got once you correct it as noted in comments, is O(n^3).

There do exist algorithms that reduce this somewhat, but you're not likely to find an O(n^2) implementation. I believe the question of the most efficient implementation is still open.

See this wikipedia article on Matrix Multiplication for more information.

like image 33
Don Roby Avatar answered Sep 22 '22 14:09

Don Roby