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 ;
2
6
3
- public class sccOptimized {
7
+ public class stronglyConnectedComponent_Optimized {
4
8
5
9
public void btrack (HashMap <Integer , List <Integer >> adjList , int [] visited , Stack <Integer > dfsCallsNodes , int currentNode ) {
6
10
visited [currentNode ] = 1 ;
@@ -10,12 +14,12 @@ public void btrack(HashMap<Integer, List<Integer>> adjList, int[] visited, Stack
10
14
btrack (adjList , visited , dfsCallsNodes , neighbor );
11
15
}
12
16
}
13
- dfsCallsNodes .add (currentNode ); // Add node after finishing DFS on all neighbors
17
+ dfsCallsNodes .add (currentNode );
14
18
}
15
19
16
20
public void btrack2 (HashMap <Integer , List <Integer >> adjRevList , int [] visited , int currentNode , List <Integer > newScc ) {
17
21
visited [currentNode ] = 1 ;
18
- newScc .add (currentNode ); // Add node to the current SCC
22
+ newScc .add (currentNode );
19
23
for (int i = 0 ; i < adjRevList .get (currentNode ).size (); i ++) {
20
24
int neighbor = adjRevList .get (currentNode ).get (i );
21
25
if (visited [neighbor ] == -1 ) {
@@ -29,28 +33,28 @@ public int getOutput(HashMap<Integer, List<Integer>> adjList, int N) {
29
33
Arrays .fill (visited , -1 );
30
34
Stack <Integer > dfsCallsNodes = new Stack <>();
31
35
32
- // First DFS pass to fill the stack with the finishing times of nodes
36
+
33
37
for (int i = 0 ; i < N ; i ++) {
34
38
if (visited [i ] == -1 ) {
35
39
btrack (adjList , visited , dfsCallsNodes , i );
36
40
}
37
41
}
38
42
39
- System .out .println ("Stack of nodes by finish time: " + dfsCallsNodes );
40
43
41
- // Reverse the graph
44
+
45
+
42
46
HashMap <Integer , List <Integer >> adjRevList = new HashMap <>();
43
47
for (int i = 0 ; i < N ; i ++) {
44
48
adjRevList .put (i , new ArrayList <>());
45
49
}
46
50
47
51
for (int i = 0 ; i < N ; i ++) {
48
52
for (int neighbor : adjList .get (i )) {
49
- adjRevList .get (neighbor ).add (i ); // Reverse edge
53
+ adjRevList .get (neighbor ).add (i );
50
54
}
51
55
}
52
56
53
- // Second DFS on the reversed graph
57
+
54
58
Arrays .fill (visited , -1 );
55
59
int stronglyConnectedComponents = 0 ;
56
60
List <List <Integer >> sccs = new ArrayList <>();
@@ -65,35 +69,8 @@ public int getOutput(HashMap<Integer, List<Integer>> adjList, int N) {
65
69
}
66
70
}
67
71
68
- // Print the found SCCs
69
- System .out .println ("Strongly Connected Components: " + sccs );
72
+
70
73
return stronglyConnectedComponents ;
71
74
}
72
75
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
- }
99
76
}
0 commit comments