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:
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Tính trừu tượng (Abstraction) trong Java
Guide to java.util.concurrent.Future
Extra Login Fields with Spring Security
Getting Started with Forms in Spring MVC
Java Program to Implement Aho-Corasick Algorithm for String Matching
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Java – String to Reader
Lập trình hướng đối tượng (OOPs) trong java
Java Program to Implement Hash Tables Chaining with List Heads
Spring Boot - Scheduling
Automatic Property Expansion with Spring Boot
Merging Two Maps with Java 8
Giới thiệu Google Guice – Binding
Consumer trong Java 8
How to Kill a Java Thread
Java – Reader to InputStream
Jackson – Bidirectional Relationships
Hướng dẫn sử dụng Java Generics
Java Program to Implement Min Heap
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Database Migrations with Flyway
Java Program to Implement Best-First Search
Giới thiệu HATEOAS
Filtering a Stream of Optionals in Java
Java Program to Check whether Undirected Graph is Connected using DFS
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Collect a Java Stream to an Immutable Collection
Java Program to Implement Unrolled Linked List
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Hashing a Password in Java