Bellman Ford Algorithm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package AdvancedAlgo;
import java.io.*;
import java.util.Scanner;
public class BellmanFord
{
private int distances[ ];
private int numberofvertices;
public static final int MAX_VALUE = 999;
private int source;
private int adjacencymatrix[ ][ ];
public BellmanFord( ) throws IOException{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of vertices");
numberofvertices = scanner.nextInt();
distances = new int[numberofvertices + 1];
adjacencymatrix = new int[numberofvertices + 1][numberofvertices + 1];
System.out.println("Enter the adjacency matrix");
for (int sourcenode = 1; sourcenode <= numberofvertices; sourcenode++)
for (int destinationnode = 1; destinationnode <= numberofvertices; destinationnode++)
{
adjacencymatrix[sourcenode][destinationnode] = scanner.nextInt();
if (sourcenode == destinationnode)
{
adjacencymatrix[sourcenode][destinationnode] = 0;
continue;
}
if (adjacencymatrix[sourcenode][destinationnode] == 0)
adjacencymatrix[sourcenode][destinationnode] = MAX_VALUE;
}
System.out.println("Enter the source vertex");
source = scanner.nextInt();
BellmanFordEvaluation( );
scanner.close();
}
public void BellmanFordEvaluation( ){
for (int node = 1; node <= numberofvertices; node++)
distances[node] = MAX_VALUE;
distances[source] = 0;
for (int node = 1; node <= numberofvertices - 1; node++)
for (int sourcenode = 1; sourcenode <= numberofvertices; sourcenode++)
for (int destinationnode = 1; destinationnode <= numberofvertices; destinationnode++)
if (adjacencymatrix[sourcenode][destinationnode] != MAX_VALUE)
if (distances[destinationnode] > distances[sourcenode] + adjacencymatrix[sourcenode][destinationnode])
distances[destinationnode] = distances[sourcenode] + adjacencymatrix[sourcenode][destinationnode];
for (int sourcenode = 1; sourcenode <= numberofvertices; sourcenode++)
for (int destinationnode = 1; destinationnode <= numberofvertices; destinationnode++)
if (adjacencymatrix[sourcenode][destinationnode] != MAX_VALUE)
if (distances[destinationnode] > distances[sourcenode] + adjacencymatrix[sourcenode][destinationnode]){
System.out.println("The Graph contains negative edge cycle");
return;
}
for (int vertex = 1; vertex <= numberofvertices; vertex++)
System.out.println("distance of source " + source + " to " + vertex + " is " + distances[vertex]);
}
public static void main(String... arg) throws IOException {
BellmanFord bellmanford = new BellmanFord( );
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Input/Output:
Enter the number of vertices
5
Enter the adjacency matrix
0 6 999 999 7
999 0 5 -4 8
999 -2 0 999 999
2 999 7 0 999
0 999 -3 9 0
Enter the source vertex
1
distance of source 1 to 1 is 0
distance of source 1 to 2 is 2
distance of source 1 to 3 is 4
distance of source 1 to 4 is -2
distance of source 1 to 5 is 7
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package AdvancedAlgo;
import java.io.*;
import java.util.Scanner;
public class BellmanFord
{
private int distances[ ];
private int numberofvertices;
public static final int MAX_VALUE = 999;
private int source;
private int adjacencymatrix[ ][ ];
public BellmanFord( ) throws IOException{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of vertices");
numberofvertices = scanner.nextInt();
distances = new int[numberofvertices + 1];
adjacencymatrix = new int[numberofvertices + 1][numberofvertices + 1];
System.out.println("Enter the adjacency matrix");
for (int sourcenode = 1; sourcenode <= numberofvertices; sourcenode++)
for (int destinationnode = 1; destinationnode <= numberofvertices; destinationnode++)
{
adjacencymatrix[sourcenode][destinationnode] = scanner.nextInt();
if (sourcenode == destinationnode)
{
adjacencymatrix[sourcenode][destinationnode] = 0;
continue;
}
if (adjacencymatrix[sourcenode][destinationnode] == 0)
adjacencymatrix[sourcenode][destinationnode] = MAX_VALUE;
}
System.out.println("Enter the source vertex");
source = scanner.nextInt();
BellmanFordEvaluation( );
scanner.close();
}
public void BellmanFordEvaluation( ){
for (int node = 1; node <= numberofvertices; node++)
distances[node] = MAX_VALUE;
distances[source] = 0;
for (int node = 1; node <= numberofvertices - 1; node++)
for (int sourcenode = 1; sourcenode <= numberofvertices; sourcenode++)
for (int destinationnode = 1; destinationnode <= numberofvertices; destinationnode++)
if (adjacencymatrix[sourcenode][destinationnode] != MAX_VALUE)
if (distances[destinationnode] > distances[sourcenode] + adjacencymatrix[sourcenode][destinationnode])
distances[destinationnode] = distances[sourcenode] + adjacencymatrix[sourcenode][destinationnode];
for (int sourcenode = 1; sourcenode <= numberofvertices; sourcenode++)
for (int destinationnode = 1; destinationnode <= numberofvertices; destinationnode++)
if (adjacencymatrix[sourcenode][destinationnode] != MAX_VALUE)
if (distances[destinationnode] > distances[sourcenode] + adjacencymatrix[sourcenode][destinationnode]){
System.out.println("The Graph contains negative edge cycle");
return;
}
for (int vertex = 1; vertex <= numberofvertices; vertex++)
System.out.println("distance of source " + source + " to " + vertex + " is " + distances[vertex]);
}
public static void main(String... arg) throws IOException {
BellmanFord bellmanford = new BellmanFord( );
}
}
Input/Output:
Enter the number of vertices
5
Enter the adjacency matrix
0 6 999 999 7
999 0 5 -4 8
999 -2 0 999 999
2 999 7 0 999
0 999 -3 9 0
Enter the source vertex
1
distance of source 1 to 1 is 0
distance of source 1 to 2 is 2
distance of source 1 to 3 is 4
distance of source 1 to 4 is -2
distance of source 1 to 5 is 7
No comments:
Post a Comment