Java Tarjan Solutions w/ Comments - Clean Code
\nclass Solution {\n    \n    public List<List<Integer>> criticalConnections(int n, List<List<Integer>> connections) {\n        // Results sets\n        List<Li
```\nclass Solution {\n    \n    public List<List<Integer>> criticalConnections(int n, List<List<Integer>> connections) {\n        // Results sets\n        List<List<Integer>> criticalConnections = new ArrayList<>();\n        \n        // Graph\n        Map<Integer, List<Integer>> map = new HashMap<>();\n        \n        // Keep track of visited vertices for dfs\n        boolean[] visited = new boolean[n];\n        \n        // Keep track of Strongly Conected Compoments low-link value\n        int[] lowLinkValues = new int[n];\n        \n        // Keep track of cycles, in dfs we increment and then set time to current lowLinkValues[] & discoverd[] values\n        int time = 0;\n        \n        // Load graph with vertices and connections\n        for (List<Integer> connection : connections) {\n            // Get [a,b] vertices from connections\n            int a = connection.get(0); \n            int b = connection.get(1);\n            \n            // Intialize [a ,b] in graph if not already\n            map.putIfAbsent(a, new ArrayList<>());\n            map.putIfAbsent(b, new ArrayList<>());\n            \n            // Add vertices to each respective List\n            map.get(a).add(b);\n            map.get(b).add(a);\n        }\n        \n        // Iterate through vertices in map and call dfs on unvisited vertices\n        for (int i = 0; i < map.size(); i++) {\n            if (!visited[i]) {\n                dfs(i, i, visited, lowLinkValues, map, time, criticalConnections);\n            }\n        }\n        \n        return criticalConnections;\n    }\n    \n    private void dfs(int currentVertice, int parentVertice, boolean[] visited, int[] lowLinkValues, Map<Integer, List<Integer>> map, int time, List<List<Integer>> criticalConnections) {\n        // Increment time per cycle\n        time++;\n        \n        // set current vertice\'s low link value to cycle time\n        lowLinkValues[currentVertice] = time;\n        \n        // Set discoveredValue to cycle time to avoid comparing \n        int discoverdValue = time;\n        \n        // Avoid infinite loop\n        visited[currentVertice] = true;\n        \n        // Iterate through edges\n        for (int vertice : map.get(currentVertice)) {\n            \n            // Skip look to avoid comparing in reverse. Ex: if A -> B don\'t compare B -> A\n            if (vertice == parentVertice) continue;\n            \n            // Process only unique vertices to avoid leaving a strongly connected component\n            if (!visited[vertice]) {\n                \n                dfs(vertice, currentVertice, visited, lowLinkValues, map, time, criticalConnections);\n                \n                // This means there is an edge that connects strongly connected components\n                if (discoverdValue < lowLinkValues[vertice]) {\n                    criticalConnections.add(Arrays.asList(currentVertice, vertice));\n                }\n            }\n            \n            // Update lowlinkValues\n            lowLinkValues[currentVertice] = Math.min(lowLinkValues[currentVertice], lowLinkValues[vertice]);\n        }\n    }\n}\n```\n\nDid this all in like 15 mins, question should be marked as EASY.... JK this was fricken hard and took me three study sessions for it to finally click.\n\nThis video was helpful for the general Tarjan SCC theory: https://www.youtube.com/watch?v=TyWtx7q2D7Y
['Java']