4
4
import java .util .Arrays ;
5
5
import java .util .HashMap ;
6
6
7
- public class stronglyConnectedComponent_Optimized {
7
+ public class StronglyConnectedComponentOptimized {
8
8
9
9
public void btrack (HashMap <Integer , List <Integer >> adjList , int [] visited , Stack <Integer > dfsCallsNodes , int currentNode ) {
10
10
visited [currentNode ] = 1 ;
@@ -19,7 +19,7 @@ public void btrack(HashMap<Integer, List<Integer>> adjList, int[] visited, Stack
19
19
20
20
public void btrack2 (HashMap <Integer , List <Integer >> adjRevList , int [] visited , int currentNode , List <Integer > newScc ) {
21
21
visited [currentNode ] = 1 ;
22
- newScc .add (currentNode );
22
+ newScc .add (currentNode );
23
23
for (int i = 0 ; i < adjRevList .get (currentNode ).size (); i ++) {
24
24
int neighbor = adjRevList .get (currentNode ).get (i );
25
25
if (visited [neighbor ] == -1 ) {
@@ -28,33 +28,28 @@ public void btrack2(HashMap<Integer, List<Integer>> adjRevList, int[] visited, i
28
28
}
29
29
}
30
30
31
- public int getOutput (HashMap <Integer , List <Integer >> adjList , int N ) {
32
- int [] visited = new int [N ];
31
+ public int getOutput (HashMap <Integer , List <Integer >> adjList , int n ) {
32
+ int [] visited = new int [n ];
33
33
Arrays .fill (visited , -1 );
34
34
Stack <Integer > dfsCallsNodes = new Stack <>();
35
35
36
-
37
- for (int i = 0 ; i < N ; i ++) {
36
+ for (int i = 0 ; i < n ; i ++) {
38
37
if (visited [i ] == -1 ) {
39
38
btrack (adjList , visited , dfsCallsNodes , i );
40
39
}
41
40
}
42
41
43
-
44
-
45
-
46
42
HashMap <Integer , List <Integer >> adjRevList = new HashMap <>();
47
- for (int i = 0 ; i < N ; i ++) {
43
+ for (int i = 0 ; i < n ; i ++) {
48
44
adjRevList .put (i , new ArrayList <>());
49
45
}
50
46
51
- for (int i = 0 ; i < N ; i ++) {
47
+ for (int i = 0 ; i < n ; i ++) {
52
48
for (int neighbor : adjList .get (i )) {
53
- adjRevList .get (neighbor ).add (i );
49
+ adjRevList .get (neighbor ).add (i );
54
50
}
55
51
}
56
52
57
-
58
53
Arrays .fill (visited , -1 );
59
54
int stronglyConnectedComponents = 0 ;
60
55
List <List <Integer >> sccs = new ArrayList <>();
@@ -69,7 +64,6 @@ public int getOutput(HashMap<Integer, List<Integer>> adjList, int N) {
69
64
}
70
65
}
71
66
72
-
73
67
return stronglyConnectedComponents ;
74
68
}
75
69
0 commit comments