The Bellman-Ford algorithm

This algorithm deals with the general case, where G is a directed, weight graph, and it can contains negative edge weigths, cycles and even negative-weight cycles. It runs in O(VE) time, which means O(V^3) when the graph is dense. 

This algorithm detects cycles by checking "v.d > u.d + w(u, v)", this is viable because if there are no negative-weight cycles,  and there is an edge which satisfies "v.d > u.d + w(u, v)", v.d would be replaced by u.d + w(u, v) which is exactly what Relax(u,v, w) does. Thus at the time of checking all edges would satisfiy "v.d <= u.d + w(u, v)". Consider the Relax(u,v,w) procedure as "If there are still space for optimizing, then optimize it." 

If there are negative-weight cycles, we can consider this as there are infinite optimizing space which can never be fulled exploited. So there will always be some edge whose shortest distance can still be optimized after the "enough" Relax process.

posted @ 2014-04-14 06:05  门对夕阳  阅读(580)  评论(0编辑  收藏  举报