Professional Documents
Culture Documents
Bellman Ford
Bellman Ford
Bellman Ford
9
x y
Negative Weight Cycle..
• What is the shortest path between A and E?
• You might at first feel as if its ABCE costing 6 ( 2+1+3 ). But actually, taking a
deeper look, you would observe a negative cycle, which is BCD. The weight of
BCD is 1+(-4)+2 = (-1).
• While traversing from A to E, i could keep cycling around inside BCD to reduce
my cost by 1 each time. Like, the path A(BCD)BCE costs 5 (2+(-1)+1+3).
• Now repeating the cycle infinite times would keep reducing the cost by 1 each
time. I could achieve a negative infinite shortest path between A and E.
• The problem is evident for any negative cycle in a graph. Hence, whenever a
negative cycle is present, the minimum weight is not defined or is negative
infinity.
Bellman-Ford returns a compact representation of the set of
shortest paths from s to all other vertices in the graph reachable
from s. This is contained in the predecessor subgraph.
u v
5
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U ∞ Nil
z 0 7
–4 V ∞ Nil
7 2 X ∞ Nil
Y ∞ Nil
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
u v
5
6
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 6 z
z 0 7
–4 V ∞ Nil
7 2 X ∞ Nil
Y ∞ Nil
7
9
x y
Pass 1
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
u v
5
6
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 6 z
z 0 7
–4 V ∞ Nil
7 2 X 7 z
Y ∞ Nil
7
9
x y
Pass 1
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
u v
5
6 11
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 6 z
z 0 7
–4 V 11 U
7 2 X 7 z
Y ∞ Nil
7
9
x y
Pass 2
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
u v
5
6 11
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 6 z
z 0 7
–4 V 11 U
7 2 X 7 z
Y 2 U
7 2
9
x y
Pass 2
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
u v
5
6 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 6 z
z 0 7
–4 V 4 X
7 2 X 7 z
Y 2 U
7 2
9
x y
Pass 2
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
u v
5
2 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 2 v
z 0 7
–4 V 4 X
7 2 X 7 z
Y -2 U
7 -2
9
x y
Pass 3
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
u v
5
2 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 2 v
z 0 7
–4 V 4 X
7 2 X 7 z
Y -2 U
7 -2
9
x y
Pass 4
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)
No reachable negative-weight u v
cycles. 5
2 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 2 v
z 0 7
–4 V 4 X
7 2 X 7 z
Y -2 U
7 -2
9
x y
Bellman Ford Algorithm Analysis
O(V)
O(E) O(VE)
O(1)
O(E)
Repeat V-1 times: relax all E edges.
Initialize
Pass 1
Pass 1
Pass 1
Pass 1
Pass 2
Pass 2
Pass 2
Pass 3