# Bellman-Ford Algorithm by pptfiles

VIEWS: 3 PAGES: 11

• pg 1
Bellman-Ford Algorithm

Shortest path alg, even for negative
weights
Relaxation
• A key technique in shortest path algorithms is
relaxation
– Idea: for all v, maintain upper bound on distance
Relax(u,v,w) {
if (d[v] > d[u]+w) then d[v]=d[u]+w;
}

2                               2
5                    9          5                      6

Relax                           Relax
2                               2
5                    7          5                      6
Bellman-Ford Algorithm
BellmanFord()
for each v  V                  Initialize d[], which
d[v] = ;                    will converge to
d[s] = 0;                       shortest-path value 
for i=1 to |V|-1                Relaxation:
for each edge (u,v)  E      Make |V|-1 passes,
Relax(u,v, w(u,v));       relaxing each edge
for each edge (u,v)  E         Test for solution
if (d[v] > d[u] + w(u,v))    Under what condition
return “no solution”;   do we get a solution?

Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w
Bellman-Ford Algorithm
BellmanFord()
What will be the
for each v  V                  running time?
d[v] = ;
d[s] = 0;
for i=1 to |V|-1
for each edge (u,v)  E
Relax(u,v, w(u,v));
for each edge (u,v)  E
if (d[v] > d[u] + w(u,v))
return “no solution”;

Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w
Bellman-Ford Algorithm
BellmanFord()
What will be the
for each v  V                  running time?
d[v] = ;
d[s] = 0;                       A: O(VE)
for i=1 to |V|-1
for each edge (u,v)  E
Relax(u,v, w(u,v));
for each edge (u,v)  E
if (d[v] > d[u] + w(u,v))
return “no solution”;

Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w
Bellman-Ford Example
t   5      x
-2
6                 
-3
s   0       8
-4   7
2
7
   9      
y          z
Bellman-Ford Example
t   5      x                       t   5     x
-2                                -2
6                                6       6         
-3                                -3
s   0       8
-4   7   s   0       8
-4   7
2                                 2
7                                  7
   9                             7   9     
y          z                       y         z
Bellman-Ford Example
t   5        x                           t   5     x
-2                                      -2
6                                      6       6         
-3                                      -3
s   0       8
-4       7   s   0       8
-4   7
2                                       2
7                                        7
   9                                   7   9     
y            z                           y         z
t    5           x
-2
6       6            4
-3
s   0       8
-4       7
2
7
7    9       2
y                z
Bellman-Ford Example
t   5        x                           t   5     x
-2                                      -2
6                                      6       6         
-3                                      -3
s   0       8
-4       7   s   0       8
-4   7
2                                       2
7                                        7
   9                                   7   9     
y            z                           y         z
t    5           x                       t   5     x
-2                                      -2
6       6            4                   6       2         4
-3                                     -3
s   0       8
-4       7   s   0       8
-4   7
2                                     2
7                                        7
7    9       2                           7   9     2
y                z                       y         z
Bellman-Ford Example
t   5     x
-2
6       2         4
-3
s    0       8
-4   7
2
7
7   9    -2
y         z

• Bellman-Ford running time:
– (|V|-1)|E| + |E| = Q(VE)
Bellman-Ford
• Prove: after |V|-1 passes, all d values correct
– Consider shortest path from s to v:
s  v1  v2  v3  v4  v
• Initially, d[s] = 0 is correct, and doesn’t change (Why?)
• After 1 pass through edges, d[v1] is correct (Why?) and
doesn’t change
• After 2 passes, d[v2] is correct and doesn’t change
• …
• Terminates in |V| - 1 passes: (Why?)
• What if it doesn’t?

To top