This is a Java Program to Implement Warshall Transitive closure Algorithm. Warshall’s Transitive closure algorithm is used to determine if a path exists from vertex a to vertex b for all vertex pairs (a, b) in a graph.
Here is the source code of the Java Program to Implement Warshall Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
/**
** Java Program to Implement Warshall Algorithm
**/
import java.util.Scanner;
/** Class Warshall **/
public class Warshall
{
private int V;
private boolean[][] tc;
/** Function to make the transitive closure **/
public void getTC(int[][] graph)
{
this.V = graph.length;
tc = new boolean[V][V];
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
if (graph[i][j] != 0)
tc[i][j] = true;
tc[i][i] = true;
}
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (tc[j][i])
for (int k = 0; k < V; k++)
if (tc[j][i] && tc[i][k])
tc[j][k] = true;
}
}
}
/** Funtion to display the trasitive closure **/
public void displayTC()
{
System.out.println("\nTransitive closure :\n");
System.out.print(" ");
for (int v = 0; v < V; v++)
System.out.print(" " + v );
System.out.println();
for (int v = 0; v < V; v++)
{
System.out.print(v +" ");
for (int w = 0; w < V; w++)
{
if (tc[v][w])
System.out.print(" * ");
else
System.out.print(" ");
}
System.out.println();
}
}
/** Main function **/
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Warshall Algorithm Test\n");
/** Make an object of Warshall class **/
Warshall w = new Warshall();
/** Accept number of vertices **/
System.out.println("Enter number of vertices\n");
int V = scan.nextInt();
/** get graph **/
System.out.println("\nEnter matrix\n");
int[][] graph = new int[V][V];
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
graph[i][j] = scan.nextInt();
w.getTC(graph);
w.displayTC();
}
}
Warshall Algorithm Test
Enter number of vertices
6
Enter matrix
0 1 0 0 0 1
0 0 0 0 0 0
1 0 0 1 0 0
0 0 0 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
Transitive closure :
0 1 2 3 4 5
0 * * * * *
1 *
2 * * * * * *
3 *
4 * *
5 * * *
Related posts:
Java Program to Implement AttributeList API
Using the Map.Entry Java Class
Java Concurrency Interview Questions and Answers
Java Program to Solve any Linear Equation in One Variable
Java Program to Solve Knapsack Problem Using Dynamic Programming
A Guide to ConcurrentMap
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Filtering a Stream of Optionals in Java
Immutable Objects in Java
Các nguyên lý thiết kế hướng đối tượng – SOLID
Java Program to Perform Complex Number Multiplication
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Lấy ngày giờ hiện tại trong Java
The Dining Philosophers Problem in Java
Easy Ways to Write a Java InputStream to an OutputStream
Inject Parameters into JUnit Jupiter Unit Tests
Một số ký tự đặc biệt trong Java
So sánh Array và ArrayList trong Java
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Redirect to Different Pages after Login with Spring Security
Using Optional with Jackson
Java Program to Find Nearest Neighbor for Dynamic Data Set
@Order in Spring
Guide to System.gc()
Java Copy Constructor
Feign – Tạo ứng dụng Java RESTful Client
A Guide to JUnit 5
Returning Custom Status Codes from Spring Controllers
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Spring Security OAuth2 – Simple Token Revocation
Logging in Spring Boot