0% found this document useful (0 votes)
6 views

Lab Program 6 (3)

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

Lab Program 6 (3)

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

8 .

Write a program to find the shortest path between vertices using Bellman ford
Algorithm

import java.util.Scanner;

public class BellmanFord

private int distances[];

private int numberofvertices;

public static final int MAX_VALUE = 999;

public BellmanFord(int numberofvertices)

this.numberofvertices = numberofvertices;

distances = new int[numberofvertices + 1];

public void Evaluation(int source, int destination,int adjacencymatrix[][])

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 vertex = 1; vertex <= numberofvertices; vertex++)

if (vertex == destination)

System.out.println("Distance from source node " + source + " to destination


node ” + vertex + " is " + distances[vertex]);

public static void main(String[] args)

{
int numberofvertices = 0;

int source, destination;

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of vertices");

numberofvertices = scanner.nextInt();

int 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 node");

source = scanner.nextInt();

System.out.println("Enter the destination node: ");

destination = scanner.nextInt();

BellmanFord bellmanford = new BellmanFord(numberofvertices);

bellmanford.Evaluation(source, destination, adjacencymatrix);

scanner.close();

}
OUTPUT

Enter the number of vertices

Enter the adjacency matrix

0 1 2 999 999
1 0 999 1 999
2 999 0 3 999
999 1 3 0 2
999 999 999 2 0

Enter the source node


1

Enter the destination node


5

Distance from source node 1 to destination node 5 is 4

You might also like