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:
Send email with JavaMail
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Java Program to Find Transpose of a Graph Matrix
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Java Program to Implement Unrolled Linked List
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Introduction to Spring Security Expressions
Simple Single Sign-On with Spring Security OAuth2
Giới thiệu Aspect Oriented Programming (AOP)
Java Program to Implement the Checksum Method for Small String Messages and Detect
Một số ký tự đặc biệt trong Java
Custom Thread Pools In Java 8 Parallel Streams
Java String Conversions
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Guava – Join and Split Collections
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Using JWT with Spring Security OAuth (legacy stack)
Converting Between Byte Arrays and Hexadecimal Strings in Java
Java 9 Stream API Improvements
A Guide to EnumMap
Guide to PriorityBlockingQueue in Java
Java Program to Implement Gabow Algorithm
Java Program to Perform Stooge Sort
Java Program to Find Inverse of a Matrix
Java Program to Describe the Representation of Graph using Incidence Matrix
Dynamic Proxies in Java
Unsatisfied Dependency in Spring
Instance Profile Credentials using Spring Cloud
Java Program to Generate Random Hexadecimal Byte
Java Program to Implement TreeMap API