# bellman-ford(贝尔曼-福特)算法

Bellman-Ford算法(根据发明者 Richard Bellman 和 Lester Ford 命名)是求解单源最短路径问题的一种算法。单源点的最短路径问题是指：给定一个加权有向图G和源点s，对于图G中的任意一点v，求从s到v的最短路径。有时候这种算法也被称为 Moore-Bellman-Ford 算法，因为 Edward F. Moore zu 也为这个算法的发展做出了贡献。

Bellman-Ford算法的伪代码如下：
========================================
01 for 每一个结点v 属于 V
02 Distanz(v) := 无穷大， Predecessor(v) := null
03 Distanz(s) := 0，Predecessor(s):= null;

04 循环 n-1 次
05     for 每一条路径 （u,v）属于 E
06         if Distanz(u) + weight(u,v) < Distanz(v)
07         then
08            Distanz(v) := Distanz(u) + weight(u,v)
09            Predecessor(v) := u;

10 for 每一条路径 （u，v）属于 E
11     if Distanz(u) + weight(u,v) < Distanz(v)
12     then
13       中止程序并且返回 “找到负循环”
14 返回
==============================================
BF(G,w,s) // G = Graph, w = weight, s=source
Determine Single Source(G,s);
set Distance(s) = 0; Predecessor(s) = nil;
for each vertex v in G other than s,
set Distance(v) = infinity, Predecessor(v) = nil;
for i <- 1 to |V(G)| - 1 do   //|V(G)| Number of vertices in the graph
for each edge (u,v) in G do
if Distance(v) > Distance(u) + w(u,v) then
set Distance(v) = Distance(u) + w(u,v), Predecessor(v) = u;
for each edge (u,r) in G do
if Distance(r) > Distance(u) + w(u,r);
return false; //This means that the graph contains a cycle of negative weight
//and the shortest paths are not well defined
return true; //Lengths of shortest paths are in Distance array
==============================================

bellman-ford(贝尔曼-福特)算法

• 0

开心

• 0

板砖

• 0

感动

• 0

有用

• 0

疑问

• 0

难过

• 0

无聊

• 0

震惊