以下是算法:
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) 发布于 2019-05-20 15:51:51
对于每个顶点u,您只迭代从u输出的边,每个不同的边只被访问一次,这就是为什么算法需要O(V+E)时间。
这假设您使用的是图表示(例如邻接列表,而不是矩阵),它允许快速访问每个顶点的相邻边缘。拓扑排序也需要这个。
https://stackoverflow.com/questions/56222301
复制相似问题