src/com/michaelho/GraphAlgorithms/GR1.java
/**
         * Find all strongly connected component in the graph.
         * Runtime: O(V+E)
         *
         * @param graph The input graph.
         *
         * @return The vertices values of SCCs in the graph.
         * */
Find all strongly connected component in the graph.

@param graph The input graph.

@return The vertices values of SCCs in the graph.
Find all strongly connected component in the graph . @param graph The input graph . @return The vertices values of SCCs in the graph .
List<List<Integer>> findSCCs(Graph graph) {
            Stack<Integer> stack = new Stack<>();
            boolean[] visited = new boolean[graph.V];
            for (int i = 0; i < graph.V; i++)
                if (!visited[i])
                    markAdjacentVisited(graph, i, visited, stack);
            Graph gReversed = reverseGraph(graph);
            for (int i = 0; i < graph.V; i++)
                visited[i] = false;
            List<List<Integer>> output = new ArrayList<>();
            while (!stack.empty()) {
                int v = stack.pop();
                if (!visited[v]) {
                    List<Integer> list = new ArrayList<>();
                    dfs.DFSHelper(gReversed, list, v, visited);
                    output.add(list);
                }
            }
            return output;
        }
List < List < Integer > > findSCCs ( Graph graph ) { Stack < Integer > stack = new Stack < > ( ) ; boolean [ ] visited = new boolean [ graph . V ] ; for ( int i = 0 ; i < graph . V ; i ++ ) if ( ! visited [ i ] ) markAdjacentVisited ( graph , i , visited , stack ) ; Graph gReversed = reverseGraph ( graph ) ; for ( int i = 0 ; i < graph . V ; i ++ ) visited [ i ] = false ; List < List < Integer > > output = new ArrayList < > ( ) ; while ( ! stack . empty ( ) ) { int v = stack . pop ( ) ; if ( ! visited [ v ] ) { List < Integer > list = new ArrayList < > ( ) ; dfs . DFSHelper ( gReversed , list , v , visited ) ; output . add ( list ) ; } } return output ; }
Find all strongly connected component in the graph.
Find all strongly connected component in the graph .
// Fill vertices in stack according to their finishing times // Create a reversed graph // Mark all the vertices as not visited (For second DFS)