Bellman Ford Problem
Algorithm
Input Graph (G).
Output Sorted path from source to destination .
Complexity O(VE).
Initialize (G, s)
1 for each vertex v in V[G]
2 do d[v]= ∞
3 parent[v] =NIL
4 d[s] =0
Relax (u, v, w)
1 if d[v] > d[u] + w (u, v)
2 then d[v] = d[u] + w (u, v)
3 parent[v] ← u
BELLMAN-FORD (G, w, s)
1 Initialize (G, s)
2 for i ← 1 to |V[G]| - 1
3 do
for each edge (u, v) in E[G]
4 do
Relax (u, v, w)
5 for each edge (u, v) in E [G]
6 do if d[v] > d[u] + w (u, v)
7 then return FALSE
8 return TRUE
Algorithm Description
- V(G) :set of vertexes of Graph. E (G):set of edges of the graph. d[v] :distance of each vertex from source. Parent[v] :parent or predecessor of
- Initialize () function initialize the distance of every vertex from source.(its assume that all nodes are disconnected.) this function initialize parent of each node to null because all nodes are disconnected.
- Relax () function check the condition d[v] > d[u] + w (u, v). Where u and v are adjacent node. If this condition is true then its function set d[v] > d[u] + w (u, v). and parent of v is become u.(parent of any node become anode which update it recently);
- Bellman ford algorithm run the loop n-1 times. (Number of node in graph-1). In every iteration of loop relax function is called on every edge.
- In step 5, 6, 7 this algorithm check for negative cycle. if condition d[v] > d[u] + w (u, v) is true then there exist a negative cycle .when there exist a negative cycle then sorted path can’t be find.