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 graph
  • no_of_edges - Input the number of edges in the graph
  • source - The source vertex of an edge in the graph
  • destination - The destination of an edge in the graph
  • weight - 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 : -3

Sample 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