-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstronglyconnectedKOSARAJU.java
68 lines (58 loc) · 1.46 KB
/
stronglyconnectedKOSARAJU.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package graphs;
import java.util.Arrays;
import java.util.LinkedList;
public class stronglyconnectedKOSARAJU {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] adj = { { 0, 1, 1 }, { 0, 0, 1 }, { 0, 0, 0 }, };
kosaraju(adj);
}
public static void kosaraju(int[][] adj) {
// dfs and store all values in stack
int[] vis = new int[adj.length];
LinkedList<Integer> st = new LinkedList<>();
for (int i = 0; i < adj.length; i++) {
if (vis[i] == 0) {
dfs(vis, adj, i, st);
}
}
// now reverse every edge of adj list
for (int i = 0; i < adj.length; i++) {
for (int j = i + 1; j < adj[0].length; j++) {
int temp = adj[i][j];
adj[i][j] = adj[j][i];
adj[j][i] = temp;
}
}
Arrays.fill(vis, 0);
// now again dfs with stack elements
int c = 0;
while (!st.isEmpty()) {
int src = st.removeFirst();
if (vis[src] == 0) {
c++;
dfs2(vis, adj, src);
}
}
System.out.println(c);
}
public static void dfs(int[] visited, int[][] adj, int src, LinkedList<Integer> st) {
visited[src] = 1;
int[] nbr = adj[src];
for (int i = 0; i < nbr.length; i++) {
if (nbr[i] == 1 && visited[i] == 0) {
dfs(visited, adj, i, st);
}
}
st.addFirst(src);
}
public static void dfs2(int[] visited, int[][] adj, int src) {
visited[src] = 1;
int[] nbr = adj[src];
for (int i = 0; i < nbr.length; i++) {
if (nbr[i] == 1 && visited[i] == 0) {
dfs2(visited, adj, i);
}
}
}
}