矩阵乘法算法时间复杂度
我想出了这个矩阵乘法的算法。我在某处读到矩阵乘法的时间复杂度为 o(n^2)。 但我认为我的这个算法会给出o(n^3)。 我不知道如何计算嵌套循环的时间复杂度。所以请纠正我。
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]
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用线性代数,存在比朴素 O(n3) 实现更好复杂度的算法。 Solvay Strassen 算法通过减少复杂度实现了 O(n2.807)每个 2x2 子矩阵所需的乘法次数从 8 到 7。
已知最快的矩阵乘法算法是 Coppersmith-Winograd 算法,复杂度为 O(n2.3737)。除非矩阵很大,否则这些算法不会导致计算时间的巨大差异。在实践中,使用并行算法进行矩阵乘法更容易、更快捷。
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.
简单的算法,即按照注释中所述进行更正后得到的算法,是 O(n^3)。
确实存在可以在一定程度上减少这种情况的算法,但您不太可能找到 O(n^2) 实现。我相信最有效的实施问题仍然悬而未决。
有关详细信息,请参阅关于矩阵乘法的维基百科文章。
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.
将 m×n 矩阵乘以 n×p 矩阵的标准方法的复杂度为 O(mnp)。如果所有这些对你来说都是“n”,那么它是 O(n^3),而不是 O(n^2)。编辑:一般情况下不会是 O(n^2) 。但是对于特定类型的矩阵有更快的算法 - 如果您了解更多,您可能会做得更好。
The standard way of multiplying an m-by-n matrix by an n-by-p matrix has complexity O(mnp). If all of those are "n" to you, it's O(n^3), not O(n^2). EDIT: it will not be O(n^2) in the general case. But there are faster algorithms for particular types of matrices -- if you know more you may be able to do better.
在矩阵乘法中,我们使用了 3 个 for 循环,因为每个 for 循环的执行都需要时间复杂度
O(n)
。因此,对于三个循环,它变为O(n^3)
In matrix multiplication there are 3 for loop, we are using since execution of each for loop requires time complexity
O(n)
. So for three loops it becomesO(n^3)
我最近在大学作业中遇到了一个矩阵乘法问题,这就是我在 O(n^2) 中解决它的方法。
}
I recently had a matrix multiplication problem in my college assignment, this is how I solved it in O(n^2).
}