This is a java program to find shortest path from a single vertex. 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.[1] 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.
Here is the source code of the Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path Between Two Vertices Assuming that Negative Size Edges Exist in the Graph. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
package com.maixuanviet.graph; 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 BellmanFordEvaluation(int source, int destination, int adjacencymatrix[][]) { for (int node = 1; node <= numberofvertices; node++) { distances[node] = MAX_VALUE; } distances = 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 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]) System.out .println("The Graph contains negative egde cycle"); } } } for (int vertex = 1; vertex <= numberofvertices; vertex++) { if (vertex == destination) System.out.println("distance of source " + source + " to " + vertex + " is " + distances[vertex]); } } public static void main(String... arg) { 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 vertex"); source = scanner.nextInt(); System.out.println("Enter the destination vertex: "); destination = scanner.nextInt(); BellmanFord bellmanford = new BellmanFord(numberofvertices); bellmanford.BellmanFordEvaluation(source, destination, adjacencymatrix); scanner.close(); } }
Output:
$ javac BellmanFord.java $ java BellmanFord Output: Enter the number of vertices 6 Enter the adjacency matrix 0 4 0 0 -1 0 0 0 -1 0 -2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -5 0 3 0 0 0 0 0 0 Enter the source vertex 1 Enter the destination vertex: 4 distance of source 1 to 4 is -6
Related posts:
Java Program to Implement Efficient O(log n) Fibonacci generator
Java Program to Find the Minimum value of Binary Search Tree
Convert String to int or Integer in Java
Apache Commons Collections SetUtils
RegEx for matching Date Pattern in Java
Fixing 401s with CORS Preflights and Spring Security
New in Spring Security OAuth2 – Verify Claims
Java Program to Implement Sorted Circularly Singly Linked List
Java Program to Implement Pollard Rho Algorithm
Java 8 StringJoiner
Java Program to Implement Dijkstra’s Algorithm using Queue
Java Program to Implement Shell Sort
Java Program to Implement Queue using Two Stacks
Comparing Objects in Java
Java Program to Implement Interpolation Search Algorithm
Guide to Java OutputStream
Java Program to Implement Bit Array
Refactoring Design Pattern với tính năng mới trong Java 8
Java Program to Generate Date Between Given Range
Introduction to Java Serialization
Removing Elements from Java Collections
Serialization và Deserialization trong java
The Guide to RestTemplate
Java Program to Construct an Expression Tree for an Infix Expression
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Derived Query Methods in Spring Data JPA Repositories
Handling URL Encoded Form Data in Spring REST
Java Program to Perform Finite State Automaton based Search
Hướng dẫn Java Design Pattern – Flyweight
Java Program to Compute DFT Coefficients Directly
Hướng dẫn Java Design Pattern – DAO
LinkedHashSet trong java