Monday 27 November 2017

Java Program to Implement Kosaraju Algorithm


Code:

import java.util.*;

/** class Kosaraju **/
public class Kosaraju
{
    /** DFS function **/
    public void DFS(List[] graph, int v, boolean[] visited, List comp) 
    {
        visited[v] = true;
        for (int i = 0; i < graph[v].size(); i++)
            if (!visited[graph[v].get(i)])
                DFS(graph, graph[v].get(i), visited, comp);
        comp.add(v);
    }
    /** function fill order **/
    public List fillOrder(List[] graph, boolean[] visited) 
    {        
        int V = graph.length;
        List order = new ArrayList();

        for (int i = 0; i < V; i++)
            if (!visited[i])
                DFS(graph, i, visited, order);
        return order;
    }
    /** function to get transpose of graph **/
    public List[] getTranspose(List[] graph)
    {
        int V = graph.length;
        List[] g = new List[V];
        for (int i = 0; i < V; i++)
            g[i] = new ArrayList();
        for (int v = 0; v < V; v++)
            for (int i = 0; i < graph[v].size(); i++)
                g[graph[v].get(i)].add(v);
        return g;
    }
    /** function to get all SCC **/
    public List> getSCComponents(List[] graph)
    {
        int V = graph.length;
        boolean[] visited = new boolean[V];
        List order = fillOrder(graph, visited);       
        /** get transpose of the graph **/
        List[] reverseGraph = getTranspose(graph);        
        /** clear visited array **/
        visited = new boolean[V];
        /** reverse order or alternatively use a stack for order **/
        Collections.reverse(order);

        /** get all scc **/
        List> SCComp = new ArrayList<>();
        for (int i = 0; i < order.size(); i++)
        {
            /** if stack is used for order pop out elements **/
            int v = order.get(i);
            if (!visited[v]) 
            {
                List comp = new ArrayList<>();
                DFS(reverseGraph, v, visited, comp);
                SCComp.add(comp);
            }
        }
        return SCComp;
    }
    /** main **/
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Kosaraju algorithm Test\n");
        Kosaraju k = new Kosaraju();

        System.out.println("Enter number of Vertices");
        /** number of vertices **/
        int V = scan.nextInt();
        List[] g = new List[V];
        for (int i = 0; i < V; i++)
            g[i] = new ArrayList();

        System.out.println("\nEnter number of edges");
        int E = scan.nextInt();
        /** all edges **/
        System.out.println("Enter "+ E +" x, y coordinates");
        for (int i = 0; i < E; i++)
        {
            int x = scan.nextInt();
            int y = scan.nextInt();
            /** add edge **/
            g[x].add(y);
        }
        System.out.println("\nSCC : ");
        /** print all strongly connected components **/
        List> scComponents = k.getSCComponents(g);
        System.out.println(scComponents);    
    }    
}


Output:

Enter number of Vertices
8

Enter number of edges
14
Enter 14 x, y coordinates
0 1
1 2
2 3
3 2
3 7
7 3
2 6
7 6
5 6
6 5
1 5
4 5
4 0
1 4

SCC :
[[1, 4, 0], [7, 3, 2], [5, 6]]



More Java Programs:


















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...