您的位置:首页 > 其它

算法导论 单源最短路径算法总结

2011-03-31 21:15 423 查看
BELLMAN-FORD(G, w, s)

1 INITIALIZE-SINGLE-SOURCE(G, s)

2 for i ← 1 to |V[G]| - 1

3 do for each edge (u, v) ∈ E[G]

4 do RELAX(u, v, w)

5 for each edge (u, v) ∈ E[G]

6 do if d[v] > d[u] + w(u, v)

7 then return FALSE

8 return TRUE

Since any acyclic path in a graph G = (V, E) contains at most |V| distinct vertices, it also contains at most |V| - 1 edges. Thus, we can restrict our attention to shortest paths of at most |V| - 1 edges.

非环路如果有|V|个节点,那么边最多有|V|-1个,所以算法的外循环对多只要|V|-1。

Let G = (V, E) be a weighted, directed graph with source s and weight function w : E → R, and assume that G contains no negative-weight cycles that are reachable from s. Then, after the |V| - 1 iterations of the for loop of lines 2-4 of BELLMAN-FORD, we have d[v] = δ(s, v) for all vertices v that are reachable from s.

Proof We prove the lemma by appealing to the path-relaxation property. Consider any vertex v that is reachable from s, and let p = 〈v0, v1,..., vk〉, where v0 = s and vk = v, be any acyclic shortest path from s to v. Path p has at most |V| - 1 edges, and so k ≤ |V| - 1. Each of the |V| - 1 iterations of the for loop of lines 2-4 relaxes all E edges. Among the edges relaxed in the ith iteration, for i = 1, 2,..., k, is (vi-1, vi). By the path-relaxation property, therefore, d[v] = d[vk] = δ(s, vk) = δ(s, v).

这条定理说的是,如果s到v的路径上有v0, v1,..., vk 有这k+1各节点,如果有v0~v1,v1~v2, ... Vk-1~vk,的路径是按先后顺序确定的,并且不管两次确定中是否有其他的路径确定,那么最终得到的是v0到vk的最短路径。

每执行一次外层的while循环,就确定了由原点s到图中某一点最短路径的一条边,而确定的顺序是由s向外。执行|V|-1次while循环,也就确定了一条长度为|V|-1的边,这条边就是有s到这条边终点的最短路径。并不是所有的最短路径都有|V|-1的长度,所以一部分的最短路径可以在循环结束之前确定。

总的来说,如果分立的看,就是把每一次while外循环看作是s到图中一个节点的一次靠近。将这种观点整合,就是把每一次while外循环看作是s到所有点的靠近。

Section 24.1 presents the Bellman-Ford algorithm, which solves the single-source shortest-paths problem in the general case in which edges can have negative weight. The Bellman-Ford algorithm is remarkable in its simplicity, and it has the further benefit of detecting whether a negative-weight cycle is reachable from the source.

这个算法解决的是单源的,有向有权值的图,并且可以检测是否图中有权值为负值的可到达环路,如果有,返回true,反之,返回false。

dag:无回路有向图

DAG-SHORTEST-PATHS(G[/i], w[/i], s[/i])
1  topologically sort the vertices of G[/i]
2  INITIALIZE-SINGLE-SOURCE(G[/i], s[/i])
3  for[/b] each vertex u[/i], taken in topologically sorted order
4       do for[/b] each vertex v[/i] ∈ Adj[/i][u[/i]]
5              do[/b] RELAX(u[/i], v[/i], w[/i])

这个算法用来解决单源的无回路有向图的最短路径问题。

DIJKSTRA(G, w, s)

1 INITIALIZE-SINGLE-SOURCE(G, s)

2 S ← Ø

3 Q ← V[G]

4 while Q ≠ Ø

5 do u ← EXTRACT-MIN(Q)

6 S ← S ∪{u}

7 for each vertex v ∈ Adj[u]

8 do RELAX(u, v, w)

这个算法解决的问题是单源的,有向有权图,且权值不为负值。

由于总是从距离源点s最近的点开始向外扩展,所以后扩展的节点到s距离一定比先钱的节点大,所以后来的节点的距离值的更新不会影响到先前的节点的最短距离值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: