Lecture 26
Lecture 26
u v
<0
Relaxation
● A key technique in shortest path algorithms is
relaxation
■ Idea: for all v, maintain upper bound d[v] on (s,v)
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
shortest-path value
d[s] = 0;
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
if (d[v] > d[u] + w(u,v)) Test for solution
Under what condition
return “no solution”;
do we get a solution?
p2
u
s
y
x
p2
p2
u
s
y
x
p2