public static void main(String args[]) {
        Graph<Integer> graph = new Graph<>(true);
        graph.addEdge(0, 1);
        graph.addEdge(1, 4);
        graph.addEdge(1, 7);
        graph.addEdge(1, 6);
        graph.addEdge(4, 2);
        graph.addEdge(4, 3);
        graph.addEdge(2, 4);
        graph.addEdge(2, 7);
        graph.addEdge(2, 6);
        graph.addEdge(7, 8);
        graph.addEdge(7, 5);
        graph.addEdge(5, 2);
        graph.addEdge(5, 3);
        graph.addEdge(3, 7);
        graph.addEdge(3, 6);
        graph.addEdge(3, 4);
        graph.addEdge(6, 5);
        graph.addEdge(6, 8);

        AllCyclesInDirectedGraphTarjan tarjan = new AllCyclesInDirectedGraphTarjan();
        List<List<Vertex<Integer>>> result = tarjan.findAllSimpleCycles(graph);
        result.forEach(cycle -> {
            cycle.forEach(v -> System.out.print(v.getId() + " "));
            System.out.println();
        });
    }
Find all simple cycles in a directed graph using Tarjan's algorithm.

Space complexity - O(E + V + S) where S is length of all cycles
Time complexity - O(E*V(C+1) where C is total number of cycles

Reference - https://ecommons.cornell.edu/handle/1813/5941
https://github.com/jgrapht/jgrapht/tree/master/jgrapht-core/src/main/java/org/jgrapht/alg/cycle
src/com/interview/graph/AllCyclesInDirectedGraphTarjan.java