简体   繁体   English

Dijkstra的算法和贪婪策略

[英]Dijkstra's Algorithm and the greedy strategy

I seem to be having a bit of trouble understanding how the greedy strategy works and how Dijkstra's Algorithm keeps track of the shortest path. 在理解贪心策略的工作原理以及Dijkstra的算法如何跟踪最短路径时,我似乎有些麻烦。 For reference, here is the pseudo code for Dijkstra's Algorithm 供参考,这是Dijkstra算法的伪代码

DijkstrasAlgorithm(G, w, s)
    InitalizeSingleSource(G, s)
    S = 0
    Q = G.V
    while Q != 0
        u = ExtractMin(Q)
        S = S∪{u}
        for each vertex v ∈ G.Adj[u]
            Relax(u, v, w)

Please consider the following weight direction graph. 请考虑以下重量方向图。

There are 5 vertices: s, t, x, y, z There are 10 edges: 有5个顶点:s,t,x,y,z有10个边:

s->t = 3
s->y = 5
t->y = 2
t->x = 6
y->t = 1
y->x = 4
y->z = 6
x->z = 2
z->x = 7
z->s = 3

Our goal is to find the shortest path from s to x. 我们的目标是找到从s到x的最短路径。 My answer was s->t->y->x with length 9, I assumed that "S" in the pseudo code was the shortest path and that each ExtractMin from minQ was added onto the path. 我的答案是s-> t-> y-> x,长度为9,我假设伪代码中的“ S”是最短路径,并且将minQ中的每个ExtractMin添加到了路径上。

My teacher, however, has told me that this is wrong. 但是我的老师告诉我,这是错误的。 The correct answer is s->t->x with length 9. The difference in our answer is whether to include y. 正确答案是长度为9的s-> t-> x。我们答案的区别在于是否包含y。 My teacher says that since s->t->x is "found first", it is not updated to s->t->y->x, which is of equal length. 我的老师说,由于s-> t-> x是“先找到”的,因此不会更新为等长的s-> t-> y-> x。

This confuses me. 这使我感到困惑。 Dijkstra's Algorithm uses the greedy strategy, and I thought the greedy strategy was to always pick the shortest path available at the time. Dijkstra的算法使用贪婪策略,我认为贪婪策略总是选择当时可用的最短路径。 And when the the choice is between t->y and t->x, t->y is shorter and should therefore be picked. 当选择在t-> y和t-> x之间时,t-> y较短,因此应该选择。

My questions are: 我的问题是:

1) In what circumstances will the greedy strategy not pick the immediate shortest path for the end result? 1)在什么情况下,贪婪策略不会选择最终结果的最短路径?

2) If using ExtractMin on minQ isn't how we keep track of the overall path from s to x, then how do we keep track of the full path? 2)如果在minQ上使用ExtractMin不是我们如何跟踪从s到x的整体路径,那么我们如何跟踪完整的路径?

Your teacher's answer assumes that we explore paths in the order, "shortest first, broken by first seen first". 您老师的回答假设我们按“最短优先,先中断先发现”的顺序探索路径。

To start with we explore s->t we put x at cost 9 from s->t->x onto the list of things to explore 'some day'. 首先我们探讨s->t我们把x成本9从s->t->x上的事情的清单,探讨“有一天”。 But we first explore s->t->y because it is shorter. 但是我们首先探索s->t->y因为它更短。 At that point, we see that s->t->y->x is an option, also of cost 9. However because this is not an improvement we drop it. 到那时,我们看到s->t->y->x是一个选项,也是成本9。但是,由于这不是改进,因此我们放弃了它。

Therefore once we get to paths of length 9, we find s->t->x out because it went on the queue first. 因此,一旦到达长度为9的路径,我们就会发现s->t->x ,因为它首先进入了队列。

You would get your answer out if you modified Relax to save a possible path if it is better than or equal to the best so far found. 如果您修改了“ Relax以保存可能的路径,且其效果优于或等于迄今为止找到的最佳路径,那么您将得到答案。 This would get a correct answer. 这将得到正确的答案。

As for how the path is extracted from the end, each node knows how you get to it. 至于从末端提取路径的方式,每个节点都知道如何到达路径。 So start from the end and follow the cookie trail backwards to find the path in reverse, then reverse it to get the actual path. 因此,从头开始,并按照Cookie跟踪向后查找反向路径,然后将其反向以获得实际路径。

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

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