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:
Apache Commons Collections OrderedMap
Converting a List to String in Java
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Tạo số và chuỗi ngẫu nhiên trong Java
JUnit 5 for Kotlin Developers
Converting a Stack Trace to a String in Java
Guide to System.gc()
Java Program to Implement Interpolation Search Algorithm
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Tiêu chuẩn coding trong Java (Coding Standards)
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Collection trong java
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Ignore Null Fields with Jackson
Apache Commons Collections SetUtils
The Registration API becomes RESTful
Spring Boot - Application Properties
Java Program to Implement Pagoda
Deploy a Spring Boot WAR into a Tomcat Server
Intersection of Two Lists in Java
Java Program to Find Strongly Connected Components in Graphs
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Java Program to Implement Patricia Trie
Java Program to Implement Interpolation Search Algorithm
Simplify the DAO with Spring and Java Generics
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Lớp Arrarys trong Java (Arrays Utility Class)
String Processing with Apache Commons Lang 3
Java Program to Implement Brent Cycle Algorithm
Introduction to Using Thymeleaf in Spring
Concatenating Strings In Java
Java Program to Find All Pairs Shortest Path