package com.thealgorithms.graph; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Stack; /** * Finds the strongly connected components in a directed graph. * * @param adjList The adjacency list representation of the graph. * @param n The number of nodes in the graph. * @return The number of strongly connected components. */ public class StronglyConnectedComponentOptimized { public void btrack(HashMap> adjList, int[] visited, Stack dfsCallsNodes, int currentNode) { visited[currentNode] = 1; List neighbors = adjList.get(currentNode); // Check for null before iterating if (neighbors != null) { for (int neighbor : neighbors) { if (visited[neighbor] == -1) { btrack(adjList, visited, dfsCallsNodes, neighbor); } } } dfsCallsNodes.add(currentNode); } public void btrack2(HashMap> adjRevList, int[] visited, int currentNode, List newScc) { visited[currentNode] = 1; newScc.add(currentNode); List neighbors = adjRevList.get(currentNode); // Check for null before iterating if (neighbors != null) { for (int neighbor : neighbors) { if (visited[neighbor] == -1) { btrack2(adjRevList, visited, neighbor, newScc); } } } } public int getOutput(HashMap> adjList, int n) { int[] visited = new int[n]; Arrays.fill(visited, -1); Stack dfsCallsNodes = new Stack<>(); for (int i = 0; i < n; i++) { if (visited[i] == -1) { btrack(adjList, visited, dfsCallsNodes, i); } } HashMap> adjRevList = new HashMap<>(); for (int i = 0; i < n; i++) { adjRevList.put(i, new ArrayList<>()); } for (int i = 0; i < n; i++) { List neighbors = adjList.get(i); // Check for null before iterating if (neighbors != null) { for (int neighbor : neighbors) { adjRevList.get(neighbor).add(i); } } } Arrays.fill(visited, -1); int stronglyConnectedComponents = 0; while (!dfsCallsNodes.isEmpty()) { int node = dfsCallsNodes.pop(); if (visited[node] == -1) { List newScc = new ArrayList<>(); btrack2(adjRevList, visited, node, newScc); stronglyConnectedComponents++; } } return stronglyConnectedComponents; } }