Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java (Java)

Code before:
    |   /                        |   \   /
    |  v                         |    v v
    1                            5 <--- 6 

    We can observe that this graph has the same SCC as that of original graph.

 */

public class Kosaraju {

    //Store each component
    private List<Integer> scc = new ArrayList<>();

    //All the strongly connected components
    private List<List<Integer>> sccsList = new ArrayList<>();

    public List<List<Integer>> kosaraju(int v, List<List<Integer>> list){
        
        //Sort the edges according to lowest finish time
        var st = new Stack<Integer>();
        int vis[] = new int[v];
        for (int i = 0; i < v; i++) {
            if(vis[i] == 0){
                dfs(i, vis, list, st);
            }
        }

        //Create a transpose graph of the given graph
        var transposeGraph = new ArrayList<List<Integer>>(8);
        for (int i = 0; i < v; i++) {
            transposeGraph.add(new ArrayList<>());
        }
        for (int i = 0; i < vis.length; i++) {
            vis[i] = 0;
            for (Integer neigh : list.get(i)) {
                transposeGraph.get(neigh).add(i);
            }
        }

        //Run DFS on the transpose graph and get the Strongly Connected Components
        while (!st.isEmpty()) {
            var node = st.pop();
            if(vis[node] == 0){
                dfs2(node, vis, transposeGraph);
                System.out.println();
                sccsList.add(scc);
                scc = new ArrayList<>();
            }
        }
        return sccsList;
    }

Review comment (refactor):
Maybe you could extract it into a method called `sortEdgesByLowestFinishTime`?

Code after:
    ^     /                      ^ \       /
    |    /                       |  \     /
    |   /                        |   \   /
    |  v                         |    v v
    1                            5 <--- 6 

    We can observe that this graph has the same SCC as that of original graph.

 */

public class Kosaraju {

    // Sort edges according to lowest finish time
    Stack<Integer> stack = new Stack<Integer>();

    //Store each component
    private List<Integer> scc = new ArrayList<>();

    //All the strongly connected components
    private List<List<Integer>> sccsList = new ArrayList<>();

    /**
     * 
     * @param v Node count
     * @param list Adjacency list of graph
     * @return List of SCCs
     */
    public List<List<Integer>> kosaraju(int v, List<List<Integer>> list){
        
        sortEdgesByLowestFinishTime(v, list);
        
        List<List<Integer>> transposeGraph = createTransposeMatrix(v, list);

        findStronglyConnectedComponents(v, transposeGraph);
        
        return sccsList;
    }

    private void sortEdgesByLowestFinishTime(int v, List<List<Integer>> list){
        int vis[] = new int[v];
        for (int i = 0; i < v; i++) {
            if(vis[i] == 0){
                dfs(i, vis, list);
            }
        }
    }

    private List<List<Integer>> createTransposeMatrix(int v, List<List<Integer>> list) {
        var transposeGraph = new ArrayList<List<Integer>>(v);
        for (int i = 0; i < v; i++) {
            transposeGraph.add(new ArrayList<>());