0% found this document useful (0 votes)
460 views3 pages

Write A Program To Find The Shortest Path Between Vertices Using Bellman-Ford Algorithm

This program uses the Bellman-Ford algorithm to find the shortest path between vertices in a graph. It takes a weighted adjacency matrix and source vertex as input, initializes all distances to MAX_VALUE, and runs Bellman-Ford to update distances if a shorter path is found. It then prints the shortest path distance from the source to each vertex. The main method gets input, creates the graph, runs Bellman-Ford on it, and closes the scanner.

Uploaded by

Ayush dravid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
460 views3 pages

Write A Program To Find The Shortest Path Between Vertices Using Bellman-Ford Algorithm

This program uses the Bellman-Ford algorithm to find the shortest path between vertices in a graph. It takes a weighted adjacency matrix and source vertex as input, initializes all distances to MAX_VALUE, and runs Bellman-Ford to update distances if a shorter path is found. It then prints the shortest path distance from the source to each vertex. The main method gets input, creates the graph, runs Bellman-Ford on it, and closes the scanner.

Uploaded by

Ayush dravid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Computer Network Laboratory(18CSL57)

2. Write a program to find the shortest path between vertices using bellman-ford algorithm.

Source code:

import java.util.Scanner;
public class BellmanFord
{
private int D[];
private int n;
public static final int MAX_VALUE = 999;
public BellmanFord(int n)
{
this.n=n;
D = new int[n+1];
}
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++)
System.out.println("Distance of source " + s + " to "+ i + " is " + D[i]);
}
public static void main(String[ ] args)
{

DSATM,Dept.of CSE 2020-21 Page 1


Computer Network Laboratory(18CSL57)

int n=0,s;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of vertices");
n = sc.nextInt();
int A[][] = new int[n+1][n+1];
System.out.println("Enter the Weighted matrix");
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
A[i][j]=sc.nextInt();
if(i==j)
{
A[i][j]=0;
continue;
}
if(A[i][j]==0)
A[i][j]=MAX_VALUE;
}
System.out.println("Enter the source vertex");
s=sc.nextInt();
BellmanFord b = new BellmanFord(n);
b.shortest(s,A);
sc.close();
}
}

DSATM,Dept.of CSE 2020-21 Page 2


Computer Network Laboratory(18CSL57)

DSATM,Dept.of CSE 2020-21 Page 3

You might also like