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:
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Java Program to Implement the One Time Pad Algorithm
Java – Combine Multiple Collections
Java Program to implement Bit Matrix
Jackson – Decide What Fields Get Serialized/Deserialized
Server-Sent Events in Spring
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Spring Security OAuth2 – Simple Token Revocation
Java Program to Find All Pairs Shortest Path
Hướng dẫn Java Design Pattern – Proxy
Java Program to do a Depth First Search/Traversal on a graph non-recursively
Dockerizing a Spring Boot Application
Java Program to Implement PriorityBlockingQueue API
Request Method Not Supported (405) in Spring
Java – Random Long, Float, Integer and Double
Queue và PriorityQueue trong Java
Java Program to Generate Random Numbers Using Multiply with Carry Method
Java equals() and hashCode() Contracts
How to use the Spring FactoryBean?
Java Program to Implement Hopcroft Algorithm
Java Program to Implement Adjacency List
wait() and notify() Methods in Java
A Comparison Between Spring and Spring Boot
Feign – Tạo ứng dụng Java RESTful Client
Java Program to Implement Horner Algorithm
Java 8 and Infinite Streams
Java Program to Implement Double Ended Queue
Concurrent Test Execution in Spring 5
Spring WebClient Requests with Parameters
Hướng dẫn Java Design Pattern – Factory Method
Exception Handling in Java
Java 8 – Powerful Comparison with Lambdas