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

Assignment

This Java code implements the Bellman-Ford algorithm to find the shortest paths from a source vertex 's' to all other vertices in a weighted graph. It initializes distances, iterates through the vertices to relax edges and update distances, and checks for negative cycles. If no negative cycles exist, it prints the distances from the source.

Uploaded by

Ranga Shetty
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Assignment

This Java code implements the Bellman-Ford algorithm to find the shortest paths from a source vertex 's' to all other vertices in a weighted graph. It initializes distances, iterates through the vertices to relax edges and update distances, and checks for negative cycles. If no negative cycles exist, it prints the distances from the source.

Uploaded by

Ranga Shetty
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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++) {

for (int j = 1; j <= n; j++) {

if (A[i][j] != MAX_VALUE) {

if (D[j] > D[i] + A[i][j]) {

System.out.println("The Graph contains a negative edge cycle");

return;

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) {

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();

You might also like