Computer Network Laboratory [BCS502] 2024-25
Program- 6
Write a 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 up date 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.
Source code:
import [Link];
public class BellmanFord
{
private int D[];
private int num_ver,n;
public static final int MAX_VALUE = 999;
public BellmanFord(int n)
{
this.n=n;
D = new int[n+1];
}
Page 34
Computer Network Laboratory [BCS502] 2024-25
public void shortest(int s,int A[][])
{
for (int i=1;i<=n;i++)
{
D[i]=MAX_VALUE;
}
D[s] = 0;
for(int k=1;k<=n-1;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(A[i][j]!=MAX_VALUE)
{
if(D[j]>D[i]+A[i][j])
D[j]=D[i]+A[i][j];
}
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(A[i][j]!=MAX_VALUE)
{
if(D[j]>D[i]+A[i][j])
{
[Link]("The Graph contains negative egde cycle");
Page 35
Computer Network Laboratory [BCS502] 2024-25
return;
}
}
}
}
for(int i=1;i<=n;i++)
{
[Link]("Distance of source " + s + " to "+ i + " is " + D[i]);
}
}
public static void main(String[ ] args)
{
int n=0,s;
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of vertices");
n = [Link]();
int A[][] = new int[n+1][n+1];
[Link]("Enter the Weighted matrix");
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
A[i][j]=[Link]();
if(i==j)
{
A[i][j]=0;
continue;
}
if(A[i][j]==0)
{
Page 36
Computer Network Laboratory [BCS502] 2024-25
A[i][j]=MAX_VALUE;
}
}}
[Link]("Enter the source vertex");
s=[Link]();
BellmanFord b = new BellmanFord(n);
[Link](s,A);
[Link]();
}
}
Output:
Enter the number of vertices
4
Enter the adjacency matrix
0
5
0
0
5
0
3
4
0
3
0
2
0
4
2
0
Page 37