Java Program to Implement Warshall Algorithm

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:

@Order in Spring
How to Get the Last Element of a Stream in Java?
Concatenating Strings In Java
Configuring a DataSource Programmatically in Spring Boot
Java Program to Implement Max Heap
Java Program to Find Nearest Neighbor for Dynamic Data Set
How to Get All Spring-Managed Beans?
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
HTTP Authentification and CGI/Servlet
Functional Interface trong Java 8
Java Program to Implement Hash Tables with Linear Probing
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Spring Boot - Internationalization
Compare Two JSON Objects with Jackson
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Java Program to Find a Good Feedback Vertex Set
Generating Random Dates in Java
Converting a Stack Trace to a String in Java
Giới thiệu Google Guice – Binding
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Spring Data MongoDB – Indexes, Annotations and Converters
Java Program to Implement HashTable API
A Quick Guide to Spring MVC Matrix Variables
Java Program to Check whether Graph is a Bipartite using DFS
Inject Parameters into JUnit Jupiter Unit Tests
Java Program to Perform Insertion in a BST
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Practical Java Examples of the Big O Notation
Introduction to Apache Commons Text
Java Program to Represent Linear Equations in Matrix Form