Sei sulla pagina 1di 6

6/6/2017 BellmanfordAlgorithm

Bellmanford Algorithm
BellmanFord algorithm is an algorithm that solves the shortest path from a single source vertex to all of the
other vertices in a weighted digraph.

BellmanFord algorithm returns a boolean value indicating whether or not there is a negativeweight cycle that is
reachable from the source. If there is such a cycle, the algorithm indicates that no solution exists. If there is no
such cycle, the algorithm produces the shortest paths and their weights.

This algorithm returns TRUE if and only if the graph contains no negativeweight cycles that are reachable from
the source.

Bellmanford Algorithm
BELLMANFORD(G,w,s)
1INITIALIZESINGLESOURCE(G,s)
2fori1to|V[G]|1
3 doforeachedge(u,v) E[G]
4 doRELAX(u,v,w)
5foreachedge(u,v) E[G]
6 doifd[v]>d[u]+w(u,v)
7 thenreturnFALSE
8returnTRUE

Example

http://scanftree.com/Data_Structure/bellmanfordalgorithm 1/6
6/6/2017 BellmanfordAlgorithm

Procedure for Bellmanford Algorithm


Step1

Consider A as source vertex


No. of Nodes A B C D E
Distance 0 6 7
Distance FromA A A

Step2

Now consider vertex B


No. of Nodes A B C D E
Distance 0 6 7 112
http://scanftree.com/Data_Structure/bellmanfordalgorithm 2/6
6/6/2017 BellmanfordAlgorithm

Distance From A AA B B

Step3

Now consider vertex E


No. of Nodes A B C D E
Distance 0 6 7 9 2
Distance From A A A E B

Step4

Now consider vertex C


No. of Nodes A B C D E
http://scanftree.com/Data_Structure/bellmanfordalgorithm 3/6
6/6/2017 BellmanfordAlgorithm

Distance 0 6 7 4 2
Distance FromA AA C B

Step5

Now consider vertex D


No. of Nodes AB C D E
Distance 0 2 7 4 2
Distance From A DA CB

Step6

Now consider vertex B


No. of Nodes AB C D E
Distance 0 27 4 2

http://scanftree.com/Data_Structure/bellmanfordalgorithm 4/6
6/6/2017 BellmanfordAlgorithm

Distance From AD A C B

Step7

Now consider vertex E


No. of Nodes AB C D E
Distance 0 2 7 4 2
Distance From A DA C B

Vertex Distance from A


A 0
B 2
C 7
D 4
http://scanftree.com/Data_Structure/bellmanfordalgorithm 5/6
6/6/2017 BellmanfordAlgorithm

E 2

http://scanftree.com/Data_Structure/bellmanfordalgorithm 6/6

Potrebbero piacerti anche