This Java program is to implement the Floyd-Warshall algorithm.The algorithm is a graph analysis algorithm for finding shortest paths in a weighted graph with positive or negative edge weights (but with no negative cycles) and also for finding transitive closure of a relation R.
Here is the source code of the Java program to implement Floyd-Warshall algorithm. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.
import java.util.Scanner;
public class FloydWarshall
{
private int distancematrix[][];
private int numberofvertices;
public static final int INFINITY = 999;
public FloydWarshall(int numberofvertices)
{
distancematrix = new int[numberofvertices + 1][numberofvertices + 1];
this.numberofvertices = numberofvertices;
}
public void floydwarshall(int adjacencymatrix[][])
{
for (int source = 1; source <= numberofvertices; source++)
{
for (int destination = 1; destination <= numberofvertices; destination++)
{
distancematrix[destination] = adjacencymatrix[destination];
}
}
for (int intermediate = 1; intermediate <= numberofvertices; intermediate++)
{
for (int source = 1; source <= numberofvertices; source++)
{
for (int destination = 1; destination <= numberofvertices; destination++)
{
if (distancematrix[intermediate] + distancematrix[intermediate][destination]
< distancematrix[destination])
distancematrix[destination] = distancematrix[intermediate]
+ distancematrix[intermediate][destination];
}
}
}
for (int source = 1; source <= numberofvertices; source++)
System.out.print("\t" + source);
System.out.println();
for (int source = 1; source <= numberofvertices; source++)
{
System.out.print(source + "\t");
for (int destination = 1; destination <= numberofvertices; destination++)
{
System.out.print(distancematrix[destination] + "\t");
}
System.out.println();
}
}
public static void main(String... arg)
{
int adjacency_matrix[][];
int numberofvertices;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of vertices");
numberofvertices = scan.nextInt();
adjacency_matrix = new int[numberofvertices + 1][numberofvertices + 1];
System.out.println("Enter the Weighted Matrix for the graph");
for (int source = 1; source <= numberofvertices; source++)
{
for (int destination = 1; destination <= numberofvertices; destination++)
{
adjacency_matrix[destination] = scan.nextInt();
if (source == destination)
{
adjacency_matrix[destination] = 0;
continue;
}
if (adjacency_matrix[destination] == 0)
{
adjacency_matrix[destination] = INFINITY;
}
}
}
System.out.println("The Transitive Closure of the Graph");
FloydWarshall floydwarshall = new FloydWarshall(numberofvertices);
floydwarshall.floydwarshall(adjacency_matrix);
scan.close();
}
}
$javac FloydWarshall.java $java FloydWarshall Enter the number of vertices 4 Enter the Weighted Matrix for the graph 0 0 3 0 2 0 0 0 0 7 0 1 6 0 0 0 The Transitive Closure of the Graph 1 2 3 4 1 0 10 3 4 2 2 0 5 6 3 7 7 0 1 4 6 16 9 0
Related posts:
The Difference Between Collection.stream().forEach() and Collection.forEach()
Using Optional with Jackson
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Getting a File’s Mime Type in Java
Initialize a HashMap in Java
Call Methods at Runtime Using Java Reflection
Logging a Reactive Sequence
An Example of Load Balancing with Zuul and Eureka
Unsatisfied Dependency in Spring
Java Program to Implement Self Balancing Binary Search Tree
Java Program to Implement HashTable API
Immutable ArrayList in Java
Jackson Annotation Examples
Guide to Guava Multimap
Java Program to Implement Network Flow Problem
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Spring Security – security none, filters none, access permitAll
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
The HttpMediaTypeNotAcceptableException in Spring MVC
Spring Cloud Connectors and Heroku
Mockito and JUnit 5 – Using ExtendWith
Set Interface trong Java
Java Program to Print only Odd Numbered Levels of a Tree
Java Program to Implement Queue
Spring RestTemplate Error Handling
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Comparing Dates in Java
Spring Data Java 8 Support
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Một số tính năng mới về xử lý ngoại lệ trong Java 7
@DynamicUpdate with Spring Data JPA