Java program to find the shortest path between vertices using bellman-ford algorithm

Distance Vector Algorithm is a decentralized routing algorithm that requires that each router simply inform its neighbors of its routing table. For each network path, the receiving routers pick the neighbor advertising the lowest cost, then add this entry into its routing table for re-advertisement. To find the shortest path, Distance Vector Algorithm is based on one of two basic algorithms: the Bellman-Ford and the Dijkstra algorithms. Routers that use this algorithm have to maintain the distance tables (which is a one-dimension array — “a vector”), which tell the distances and shortest path to sending packets to each node in the network. The information in the distance table is always upd by exchanging information with the neighboring nodes. The number of data in the table equals to that of all nodes in networks (excluded itself). The columns of table represent the directly attached neighbors whereas the rows represent all destinations in the network. Each data contains the path for sending packets to each destination in the network and distance/or time to transmit on that path (we call this as “cost”).

The measurements in this algorithm are the number of hops, latency, the number of outgoing packets, etc. The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph. It is slower than Dijkstra’s algorithm for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers. Negative edge weights are found in various applications of graphs, hence the usefulness of this algorithm. If a graph contains a “negative cycle” (i.e. a cycle whose edges sum to a negative value) that is reachable from the source, then there is no cheapest path: any path that has a point on the negative cycle can be made cheaper by one more walk around the negative cycle. In such a case, the Bellman–Ford algorithm can detect negative cycles and report their existence.

				
					import java.util.Scanner;
public class Bellmanford 
{
private int distance[];
private int numb_vert;
public static final int MAX_VALUE=999;
public Bellmanford(int numb_vert)
{
this.numb_vert=numb_vert;
distance=new int[numb_vert+1];
}
public void BellmanfordpEvaluation(int source,int adj_matrix[][])
{
for(int node=1;node<=numb_vert;node++)
{
distance[node]=MAX_VALUE;
}
distance[source]=0;
for(int node=1;node<=numb_vert;node++)
{
for(int src_node=1;src_node<=numb_vert;src_node++)
{
for(int dest_node=1;dest_node<=numb_vert;dest_node++)
{
if(adj_matrix[src_node][dest_node]!=MAX_VALUE)
{
if(distance[dest_node]>distance[src_node]+adj_matrix[src_node][dest_node])
distance[dest_node]=distance[src_node]+adj_matrix[src_node][dest_node];
}
}
}
}
for(int src_node=1;src_node<=numb_vert;src_node++)
{
for(int dest_node=1;dest_node<=numb_vert;dest_node++)
{
if(adj_matrix[src_node][dest_node]!=MAX_VALUE)
{
if(distance[dest_node]>distance[src_node]+adj_matrix[src_node][dest_node])
System.out.println("the graph contains negative edge cycle");
}
}
}
System.out.println("Routing table for Router"+source+"i");
System.out.println("destination distance/cost\t");
 for(int vertex=1;vertex<=numb_vert;vertex++)
{
System.out.println(+vertex+"\t\t\t"+distance[vertex]);
}
}

public static void main(String...arg)
{
int numb_vert=0;
Scanner scanner=new Scanner(System.in);
 System.out.println("enter the number of vertices");
 numb_vert=scanner.nextInt();
int adj_matrix[][]=new int[numb_vert+1][numb_vert+1];
System.out.println("enter the adjacency matrix");

for(int src_node=1;src_node<=numb_vert;src_node++)
{
for(int dest_node=1;dest_node<=numb_vert;dest_node++)
{
adj_matrix[src_node][dest_node]=scanner.nextInt();
 if(src_node==dest_node)
 {
adj_matrix[src_node][dest_node]=0;
continue;
  }
 if(adj_matrix[src_node][dest_node]==0)
 {
adj_matrix[src_node][dest_node]=MAX_VALUE;
}
}
}
for(int i=1;i<=numb_vert;i++)
{
Bellmanford bellmanford=new Bellmanford(numb_vert);
bellmanford.BellmanfordpEvaluation(i,adj_matrix);
}
scanner.close();
}
}
				
			
OUTPUT:

1)

enter the number of vertices

6

enter the adjacency matrix

0    2    999    999    4    999

2    0     3     999    1     5

999  3     0      4    999    2

999 999    4      0    999    3

4    1    999    999    0     1

999  5     2      3     1     0

Routing table for Router1i

destination distance/cost         

1                                  0

2                                  2

3                                  5

4                                  7

5                                  3

6                                  4

Routing table for Router2i

destination distance/cost         

1                                  2

2                                  0

3                                  3

4                                  5

5                                  1

6                                  2

Routing table for Router3i

destination distance/cost         

1                                  5

2                                  3

3                                  0

4                                  4

5                                  3

6                                  2
Routing table for Router4i

destination distance/cost         

1                                  7

2                                  5

3                                  4

4                                  0

5                                  4

6                                  3

Routing table for Router5i

destination distance/cost         

1                                  3

2                                  1

3                                  3

4                                  4

5                                  0

6                                  1

Routing table for Router6i

destination distance/cost         

1                                  4

2                                  2

3                                  2

4                                  3

5                                  1

6                                  0
2)

enter the number of vertices

5

enter the adjacency matrix

0  3  4  0  0

3  0  0  6  1

4  0  0  5  3

0  6  5  0  2

0  1  3  2  0

Routing table for Router1i

destination distance/cost         

1                                  0

2                                  3

3                                  4

4                                  6

5                                  4

Routing table for Router2i

destination distance/cost         

1                                  3

2                                  0

3                                  4

4                                  3

5                                  1

Routing table for Router3i

destination distance/cost         

1                                  4

2                                  4

3                                  0

4                                  5

5                                  3
Routing table for Router4i

destination distance/cost         

1                                  6

2                                  3

3                                  5

4                                  0

5                                  2

Routing table for Router5i

destination distance/cost         

1                                  4

2                                  1

3                                  3

4                                  2

5                                  0

Leave a Reply