Suppose that the lengths of links in a network G1 V1 E1 wit
Solution
/* Though the question had insufficient information i tried to answer the question .
If this is not what you are looking for please provide with the following information.
1. Do you require the Bellman-Ford algorithm to find the shortest distance from a source?
2.Provide the source vertex.
3. Graph g diagram.
4. Or this is relaed to Maths category , where you want the mathematical evaluation of Bellman equations?
*/
import java.util.Arrays;
public class BellmanFord {
public static final int MAX_VALUE = 999;
public static int[] bellmanFordEvaluation(int source, int adjacencymatrix[][]) {
int noOfvertices = adjacencymatrix.length;
int distances[] = new int[noOfvertices];
Arrays.fill(distances, MAX_VALUE);
distances[source] = 0;
for (int node = 0; node < noOfvertices - 1; node++) {
for (int sourcenode = 0; sourcenode < noOfvertices; sourcenode++) {
for (int destinationnode = 0; destinationnode < noOfvertices; destinationnode++) {
if (adjacencymatrix[sourcenode][destinationnode] != MAX_VALUE) {
if (distances[destinationnode] > distances[sourcenode]
+ adjacencymatrix[sourcenode][destinationnode])
distances[destinationnode] = distances[sourcenode]
+ adjacencymatrix[sourcenode][destinationnode];
}
}
}
}
return distances;
}
public static void main(String... arg) {
int noOfvertices = 7;
int inf = MAX_VALUE;
int adjacencymatrix[][] = { { 0, inf, 4, 10, 3, inf, inf }, { inf, 0, -1, -3, 2, 11, 0 },
{ inf, 9, 0, 8, 3, 2, 1 }, { inf, 4, 0, 0, 8, 6, 3 }, { inf, 0, 1, 2, 0, 3, -1 },
{ inf, -1, -1, 3, 2, 0, 0 }, { inf, 4, 3, inf, inf, 2, 0 } };
int distance[][] = new int[noOfvertices][];
for (int source = 0; source < noOfvertices; source++) {
distance[source] = BellmanFord.bellmanFordEvaluation(source, adjacencymatrix);
}
for (int i = 0; i < noOfvertices; i++) {
System.out.println(\"Displaying distance from source \'\" + i + \"\' to all the other vertices\");
for (int j = 0; j < noOfvertices; j++) {
System.out.print((distance[i][j] == inf ? \"inf\" : distance[i][j]) + \" \");
}
System.out.println(\"\ \");
}
}
}
![Suppose that the lengths of links in a network G_1 = (V_1, E_1) with V_1 = {1, ..., 7} is given by the following matrix A_1 = [a_ij]. A_1 = [0 infinity 4 10 3 Suppose that the lengths of links in a network G_1 = (V_1, E_1) with V_1 = {1, ..., 7} is given by the following matrix A_1 = [a_ij]. A_1 = [0 infinity 4 10 3](/WebImages/26/suppose-that-the-lengths-of-links-in-a-network-g1-v1-e1-wit-1067284-1761558491-0.webp)
![Suppose that the lengths of links in a network G_1 = (V_1, E_1) with V_1 = {1, ..., 7} is given by the following matrix A_1 = [a_ij]. A_1 = [0 infinity 4 10 3 Suppose that the lengths of links in a network G_1 = (V_1, E_1) with V_1 = {1, ..., 7} is given by the following matrix A_1 = [a_ij]. A_1 = [0 infinity 4 10 3](/WebImages/26/suppose-that-the-lengths-of-links-in-a-network-g1-v1-e1-wit-1067284-1761558491-1.webp)