我对 Bellman-Ford 进行了轻微的修改,使它只做“有用的”放松。也就是说,更新了意味着 d(v) 的松弛。
define Relax(u, v):
if d(v) > d(u) + w(u,v) //w(u,v) = weight of edge u->v
d(v) = d(u) + w(u,v)
INIT // our usual initialization.
Queue Q
Q ← s // Q holds vertices whose d(v) values have been updated recently.
While (Q not empty)
{
u ← Frontof(Q);
for each neighbor v of u
{
Relax(u, v)
if d(v) was updated by Relax and v not in the Q //Here's where we're a bit smarter
ADD v to End of Q. //since we add to Q if
//the relaxation changed d(v)
}
}
现在,如果所有最短路径最多有 k 个弧。那么最坏情况的运行时间是 O(V*k),因为我们在这个智能版本中只经过 k 个弧。这比原来的 O(V*E) 快一点,因为 |k| < |E|
谁能告诉我这种改进版本不比原来的 Bellman-Ford 算法更好的图类型?也就是说,最佳情况下的性能是 O(V*E)