且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

矩阵乘法算法的时间复杂度

更新时间:2022-10-20 14:25:23

天真的算法,也就是你有什么,一旦你改正它作为评论指出,为O(n ^ 3)。

确实存在的算法,减少这个有点,但你不可能找到一个为O(n ^ 2)执行。我认为,最有效的执行问题仍然是开放的。

查看矩阵乘法这***的文章以了解更多信息。

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]

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.