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:
Guide to Spring 5 WebFlux
A Guide to Java SynchronousQueue
Java Program to Create a Random Graph Using Random Edge Generation
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Debug a HttpURLConnection problem
Java Program to do a Depth First Search/Traversal on a graph non-recursively
Lập trình đa luồng với CompletableFuture trong Java 8
Removing all duplicates from a List in Java
Collect a Java Stream to an Immutable Collection
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Java Program to Implement Binomial Heap
Disable DNS caching
A Guide to Apache Commons Collections CollectionUtils
Using Optional with Jackson
Spring Security Login Page with React
Immutable ArrayList in Java
Apache Commons Collections BidiMap
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Check Whether a Given Point is in a Given Polygon
Java 9 Stream API Improvements
Giới thiệu Json Web Token (JWT)
The Basics of Java Security
Introduction to Apache Commons Text
Functional Interfaces in Java 8
Set Interface trong Java
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Java Program to Implement DelayQueue API
Spring 5 Functional Bean Registration
Hướng dẫn Java Design Pattern – Flyweight
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Tính kế thừa (Inheritance) trong java
Spring Boot - Hystrix