The Bellman-Ford Shortest Path Algorithm ��
Class Overview
Shortest Path Problem
Differences
The Bellman-Ford Algorithm
∞,nil
∞,nil
∞,nil
0
6
7
9
5
-3
8
7
-4
2
∞,nil
s
y
z
x
t
-2
The Bellman-Ford Algorithm
∞,nil
∞,nil
∞,nil
0
6
7
9
5
-3
8
7
-4
2
∞,nil
s
y
z
x
t
-2
6,s
7,s
∞,nil
0
6
7
9
5
-3
8
7
-4
2
∞,nil
s
y
z
x
t
-2
Initialization
After pass 1
6,s
7,s
4,y
0
6
7
9
5
-3
8
7
-4
2
2,t
s
y
z
x
t
-2
After pass 2
2,x
7,s
4,y
0
6
7
9
5
-3
8
7
-4
2
2,t
s
y
z
x
t
-2
After pass 3
The order of edges examined in each pass:�(t, x), (t, z), (x, t), (y, x), (y, t), (y, z), (z, x), (z, s), (s, t), (s, y)
The Bellman-Ford Algorithm
After pass 4
2,x
7,s
4,y
0
6
7
9
5
-3
8
7
-4
2
-2,t
s
y
z
x
t
-2
The order of edges examined in each pass:�(t, x), (t, z), (x, t), (y, x), (y, t), (y, z), (z, x), (z, s), (s, t), (s, y)
Bellman-Ford algorithm
8
The Bellman-Ford Algorithm
Bellman-Ford(G, w, s)
Relax(u, v, w)
if d[v] > d[u] + w(u, v)
then d[v] := d[u] + w(u, v)
parent[v] := u
So if Bellman-Ford has not converged after V(G) - 1 iterations, then there cannot be a shortest path tree, so there must be a negative weight cycle.
Time Complexity
Bellman-Ford(G, w, s)
O(|V|)
O(|V||E|)
O(|E|)
Time complexity: O(|V||E|)