public List<Set<Vertex<Integer>>> scc(Graph<Integer> graph) {

        //it holds vertices by finish time in reverse order.
        Deque<Vertex<Integer>> stack = new ArrayDeque<>();
        //holds visited vertices for DFS.
        Set<Vertex<Integer>> visited = new HashSet<>();

        //populate stack with vertices with vertex finishing last at the top.
        for (Vertex<Integer> vertex : graph.getAllVertex()) {
            if (visited.contains(vertex)) {
                continue;
            }
            DFSUtil(vertex, visited, stack);
        }

        //reverse the graph.
        Graph<Integer> reverseGraph = reverseGraph(graph);

        //Do a DFS based off vertex finish time in decreasing order on reverse graph..
        visited.clear();
        List<Set<Vertex<Integer>>> result = new ArrayList<>();
        while (!stack.isEmpty()) {
            Vertex<Integer> vertex = reverseGraph.getVertex(stack.poll().getId());
            if(visited.contains(vertex)){
                continue;
            }
            Set<Vertex<Integer>> set = new HashSet<>();
            DFSUtilForReverseGraph(vertex, visited, set);
            result.add(set);
        }
        return result;
    }
Date 10/01/2014
@author Tushar Roy

Given a directed graph, find all strongly connected components in this graph.
We are going to use Kosaraju's algorithm to find strongly connected component.

Algorithm
Create a order of vertices by finish time in decreasing order.
Reverse the graph
Do a DFS on reverse graph by finish time of vertex and created strongly connected
components.

Runtime complexity - O(V + E)
Space complexity - O(V)

References
https://en.wikipedia.org/wiki/Strongly_connected_component
http://www.geeksforgeeks.org/strongly-connected-components/
src/com/interview/graph/StronglyConnectedComponent.java