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:
So sánh ArrayList và Vector trong Java
Reversing a Linked List in Java
A Guide to the ViewResolver in Spring MVC
Java Program to Check whether Undirected Graph is Connected using BFS
A Quick Guide to Spring Cloud Consul
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Setting Up Swagger 2 with a Spring REST API
Xử lý ngoại lệ trong Java (Exception Handling)
Disable Spring Data Auto Configuration
Spring MVC Async vs Spring WebFlux
Java 14 Record Keyword
Java Program to Implement Kosaraju Algorithm
Practical Java Examples of the Big O Notation
Java Program to Implement Interval Tree
Find the Registered Spring Security Filters
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
HttpClient Connection Management
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Function trong Java 8
Java Program to Implement Min Hash
Giới thiệu Google Guice – Aspect Oriented Programming (AOP)
Java Program to Create a Balanced Binary Tree of the Incoming Data
Java Program to Implement Sorted Singly Linked List
Java Program to Implement Pollard Rho Algorithm
Spring Boot - Application Properties
HttpAsyncClient Tutorial
Quick Guide to Spring MVC with Velocity
Check If a String Is Numeric in Java
Java Program to Implement Horner Algorithm
Sorting in Java
Spring Security with Maven
Spring Boot with Multiple SQL Import Files