Function Bellman-Ford (G, W, VS)
The above Function finds the shortest path from the given graph if no negative weight cycle exists. It makes a call to procedure Relax. The function returns ‘True’ if no negative weight cycle exists, that is reachable from the source.
Step 1 Initialization, loop.
For Vi V1, V2 ..... Vn R For each Vi in V G
{
set dist [Vi] +
set color [Vi] white
set pre [Vi] NULL
}
set dist [Vs] 0
Step 2 Loop 1, Relaxation
for i 1, 2, 3..... V – 1 passes, V G.
{
loop 2 starts
for each edge (Vi, Vj) E R E G.
{
call to Relax (Vi,Vj, W)
}
end of loop 2
Step 3 Coloring
set color [Vi] black
end of loop 1
Step 4 Checking, negative weight edge cycle
for each edge (Vi, Vj) E
E G
{
if (dist [Vj] >dist [Vi] + W (Vi, Vj) then
{
return false
}
}
Step 5 No negative weight cycle return at the point of call
return true.
No comments:
Post a Comment