1- import java .util .*;
1+ import java .util .List ;
2+ import java .util .Stack ;
3+ import java .util .ArrayList ;
4+ import java .util .Arrays ;
5+ import java .util .HashMap ;
26
3- public class sccOptimized {
7+ public class stronglyConnectedComponent_Optimized {
48
59 public void btrack (HashMap <Integer , List <Integer >> adjList , int [] visited , Stack <Integer > dfsCallsNodes , int currentNode ) {
610 visited [currentNode ] = 1 ;
@@ -10,12 +14,12 @@ public void btrack(HashMap<Integer, List<Integer>> adjList, int[] visited, Stack
1014 btrack (adjList , visited , dfsCallsNodes , neighbor );
1115 }
1216 }
13- dfsCallsNodes .add (currentNode ); // Add node after finishing DFS on all neighbors
17+ dfsCallsNodes .add (currentNode );
1418 }
1519
1620 public void btrack2 (HashMap <Integer , List <Integer >> adjRevList , int [] visited , int currentNode , List <Integer > newScc ) {
1721 visited [currentNode ] = 1 ;
18- newScc .add (currentNode ); // Add node to the current SCC
22+ newScc .add (currentNode );
1923 for (int i = 0 ; i < adjRevList .get (currentNode ).size (); i ++) {
2024 int neighbor = adjRevList .get (currentNode ).get (i );
2125 if (visited [neighbor ] == -1 ) {
@@ -29,28 +33,28 @@ public int getOutput(HashMap<Integer, List<Integer>> adjList, int N) {
2933 Arrays .fill (visited , -1 );
3034 Stack <Integer > dfsCallsNodes = new Stack <>();
3135
32- // First DFS pass to fill the stack with the finishing times of nodes
36+
3337 for (int i = 0 ; i < N ; i ++) {
3438 if (visited [i ] == -1 ) {
3539 btrack (adjList , visited , dfsCallsNodes , i );
3640 }
3741 }
3842
39- System .out .println ("Stack of nodes by finish time: " + dfsCallsNodes );
4043
41- // Reverse the graph
44+
45+
4246 HashMap <Integer , List <Integer >> adjRevList = new HashMap <>();
4347 for (int i = 0 ; i < N ; i ++) {
4448 adjRevList .put (i , new ArrayList <>());
4549 }
4650
4751 for (int i = 0 ; i < N ; i ++) {
4852 for (int neighbor : adjList .get (i )) {
49- adjRevList .get (neighbor ).add (i ); // Reverse edge
53+ adjRevList .get (neighbor ).add (i );
5054 }
5155 }
5256
53- // Second DFS on the reversed graph
57+
5458 Arrays .fill (visited , -1 );
5559 int stronglyConnectedComponents = 0 ;
5660 List <List <Integer >> sccs = new ArrayList <>();
@@ -65,35 +69,8 @@ public int getOutput(HashMap<Integer, List<Integer>> adjList, int N) {
6569 }
6670 }
6771
68- // Print the found SCCs
69- System .out .println ("Strongly Connected Components: " + sccs );
72+
7073 return stronglyConnectedComponents ;
7174 }
7275
73- public static void main (String [] args ) {
74- Scanner rs = new Scanner (System .in );
75- HashMap <Integer , List <Integer >> adjList = new HashMap <>();
76- int N = rs .nextInt (); // Number of nodes
77-
78- // Initialize adjacency list
79- for (int i = 0 ; i < N ; i ++) {
80- adjList .put (i , new ArrayList <>());
81- }
82-
83- // Input graph data
84- for (int i = 0 ; i < N ; i ++) {
85- System .out .println ("Number of neighbor vertices for node " + i + ": " );
86- int nVertices = rs .nextInt ();
87- for (int j = 0 ; j < nVertices ; j ++) {
88- int neighbor = rs .nextInt ();
89- adjList .get (i ).add (neighbor );
90- }
91- }
92-
93- System .out .println ("Adjacency list: " + adjList );
94-
95- sccOptimized obj = new sccOptimized ();
96- System .out .println ("Number of SCCs: " + obj .getOutput (adjList , N ));
97- rs .close ();
98- }
9976}
0 commit comments