【发布时间】:2021-10-18 20:30:30
【问题描述】:
我正在尝试计算用python实现的卡恩算法的算法复杂度,我偶然发现了这篇文章:https://www.geeksforgeeks.org/topological-sorting-indegree-based-solution/ 在计算所有节点度的代码部分有这个
for each node in Nodes
If (list[node].size()!=0) then
for each dest in list
indegree[dest]++;
在文章中说复杂度是 O(V+E),但为什么呢? fors有两个嵌套,不应该是O(V*E)吗?
我在 python 中写了一个类似这样的实现:
for vertex in graph: # O(V) * O(E) = O(V * E). ??
for edge in graph[vertex]: # O(E)+O(1) => O(E)
indegree[dege] += 1 # O(1)
V = 图的顶点数
E = 边数
【问题讨论】:
标签: python-3.x time-complexity complexity-theory topological-sort