简体   繁体   English

有向无环图算法中单源最短路径的运行时间

[英]Runtime of singe-source shortest paths in the directed acyclic graphs algorithm

Here is the Algorithm: 这是算法:

Topologically sort the Vertices of G
Initialize - Single - Source(G,s)
for each vertex u, taken in topologically sorted order
     for each vertex v in G.Adjacent[u]
         Relax(u,v,w) 
  • Topological sort has Runtime O(V + E), where V - is the number of Vertices and E - is a number of edges 拓扑排序具有运行时O(V + E),其中V-是顶点数,E-是边数
  • Initialize - Single - Source(G,s) has runtime O(V) 初始化-单个-源(G,s)具有运行时O(V)
  • The main question is double for Loop: The running time of the double for Loop is O(V + E). 循环的主要问题是double:Loop的double的运行时间为O(V + E)。 But I cannot understand, why it's not O(V*E)? 但是我不明白,为什么不是O(V * E)? Because for every Vertices we go through every edge and normally one nested Loop(all together 2 for Loops) have complexity O(N^2), but in this case it's not true. 因为对于每个顶点,我们都要经过每个边,并且通常一个嵌套的Loop(总共2个Loop)的复杂度为O(N ^ 2),但在这种情况下不是真的。

For each vertex u , you only iterate through the edges that go out from u . 对于每一个顶点u,你只能通过从u走出边缘迭代。 Each distinct edge is visited only once, and that's why the algorithm takes O(V+E) time. 每个唯一的边缘仅被访问一次,这就是为什么该算法需要O(V + E)时间的原因。

This assumes you are using a graph representation (like adjacency lists, not a matrix) that allows quick access to every vertex's adjacent edges. 假设您使用的是图形表示形式(如邻接列表,而不是矩阵),该表示形式允许快速访问每个顶点的相邻边。 The topological sort also requires this. 拓扑排序也需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM