Algorithm 试图理解迪克斯特拉';s算法

Algorithm 试图理解迪克斯特拉';s算法,algorithm,dijkstra,directed-graph,undirected-graph,Algorithm,Dijkstra,Directed Graph,Undirected Graph,我试图更好地理解Dijkstra的算法。我在课本上附上了算法的图片。伪代码显示输入是无向图,但对于有向图,算法有什么不同吗?我用有向图的输入查找了算法,但没有发现任何差异 Algorithm ShortestPath(G, v) Input: A simple undirected weighted graph G with nonnegative edge weights and a distinguished vertex v of G Output: A label D[u], for

我试图更好地理解Dijkstra的算法。我在课本上附上了算法的图片。伪代码显示输入是无向图,但对于有向图,算法有什么不同吗?我用有向图的输入查找了算法,但没有发现任何差异

Algorithm ShortestPath(G, v)

Input: A simple undirected weighted graph G with nonnegative edge weights and a distinguished vertex v of G

Output: A label D[u], for each vertex u of G, such that D[u] is the length of a shortest path from v to u in G

Initialize D[v]<--0 and D[u]<--+infinity for each vertex u != v.

Let priority queue Q contain all the vertices of G using the D labels as keys.

while Q is not empty do

    {pull a new vertex u into the cloud}

u<-- Q.removeMin()

for each vertex z adjacent to u such that z is in Q do

    {preform the relaxation procedure on edge (u,z)}

    if D[u]+w((u,z))<D[z] then

         D[z]<-- D[u]+w((u,z))

         change to D[z] the of vertex z in Q

return the label D[u] of each vertex u
算法最短路径(G,v)
输入:一个简单的无向加权图G,具有非负边权和G的可分辨顶点v
输出:一个标签D[u],用于G的每个顶点u,使得D[u]是G中从v到u的最短路径的长度

初始化D[v]您可以在有向图和无向图中使用Dijkstra算法,因为当您从邻接列表中有一条边要移动时,您只需将边添加到优先级队列中。例如,如果我的一个节点从A到B的边的权重为3,那么如果它是从B定向的,我将无法将该边添加回A,而从A我可以将其添加到B


与其他答案一样,请确保不要将其与权重混淆。Dijkstra的算法在正加权图上运行,否则优先级队列将毫无用处。

请不要将文本作为图像发布。它是不可搜索的,也不能被屏幕阅读器解释。好吧,更正了它。使用有向图,算法将集中在边上,并说“对于每个边z-u(…)”,我想我现在明白了。谢谢大家的帮助!