Function graph_algorithms::list_of_algorithms::bellmanford::bellmanford::bellmanford
source · [−]pub fn bellmanford()Expand description
Performs Bellmanford algorithm on a given weighted graph. Prints a graph with shortest distance from one vertex to another vertex.
Input
no_of_vertices- Input the number of vertices in the graphno_of_edges- Input the number of edges in the graphsource- The source vertex of an edge in the graphdestination- The destination of an edge in the graphweight- The weight of the corresponding edge
Output
Prints the shortest distance of the vertices in the graph
Sample input
Please Enter Number of Vertices : 5
Enter Source Vertex : 0
Please Enter Number of edges in the graph : 8
Source : 0
Destination : 1
Weight : -1
Source : 0
Destination : 2
Weight : 4
Source : 1
Destination : 2
Weight : 3
Source : 3
Destination : 2
Weight : 5
Source : 3
Destination : 1
Weight : 1
Source : 1
Destination : 3
Weight : 2
Source : 1
Destination : 4
Weight : 2
Source : 4
Destination : 3
Weight : -3Sample output
Distance from vertex 0 to vertex 0 is 0
Distance from vertex 0 to vertex 1 is -1
Distance from vertex 0 to vertex 2 is 2
Distance from vertex 0 to vertex 3 is -2
Distance from vertex 0 to vertex 4 is 1