BFS的基本算法:
set start vertex to visited
load it into queue
while queue not empty
for each edge incident to vertex
if its not visited
load into queue
mark vertex所以我认为时间复杂度应该是:
v1 + (incident edges) + v2 + (incident edges) + .... + vn + (incident edges) 其中v是顶点1 to n
首先,我所说的是正确的吗?其次,这是怎样的O(N + E),以及为什么会很好的直觉。谢谢
发布于 2012-07-13 18:29:31
你的总和
v1 + (incident edges) + v2 + (incident edges) + .... + vn + (incident edges)可以重写为
(v1 + v2 + ... + vn) + [(incident_edges v1) + (incident_edges v2) + ... + (incident_edges vn)]第一组是O(N),另一组是O(E)。
发布于 2014-12-17 14:04:35
非常简化,没有太多的形式:每条边被恰好考虑两次,每个节点被恰好处理一次,所以复杂度必须是边的数量以及顶点的数量的恒定倍数。
发布于 2015-09-10 23:39:04
时间复杂度是O(E+V)而不是O(2E+V),因为如果时间复杂度是n^2+2n+7,那么它就写成O(n^2)。
因此,O(2E+V)写成O(E+V)
因为n^2和n之间的差异很重要,但n和2n之间并不重要。
https://stackoverflow.com/questions/11468621
复制相似问题