forked from striver79/StriversGraphSeries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kosaRajuJava
87 lines (72 loc) · 1.65 KB
/
kosaRajuJava
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.util.*;
class Main
{
private void dfs(int node, Stack<Integer> st, ArrayList<ArrayList<Integer>> adj, int vis[]) {
vis[node] = 1;
for(Integer it : adj.get(node)) {
if(vis[it] == 0) {
dfs(it, st, adj, vis);
}
}
st.push(node);
}
private void revDfs(int node, ArrayList<ArrayList<Integer>> transpose, int vis[]) {
vis[node] = 1;
System.out.print(node + " ");
for(Integer it : transpose.get(node)) {
if(vis[it] == 0) {
revDfs(it, transpose, vis);
}
}
}
void kosaRaju(ArrayList<ArrayList<Integer>> adj, int n)
{
int vis[] = new int[n];
Stack<Integer> st = new Stack<Integer>();
for(int i = 0;i<n;i++) {
if(vis[i] == 0) {
dfs(i, st, adj, vis);
}
}
ArrayList<ArrayList<Integer> > transpose = new ArrayList<ArrayList<Integer> >();
for (int i = 0; i < n; i++)
transpose.add(new ArrayList<Integer>());
for(int i = 0;i<n;i++) {
vis[i] = 0;
for(Integer it: adj.get(i)) {
transpose.get(it).add(i);
}
}
while(st.size() > 0) {
int node = st.peek();
st.pop();
if(vis[node] == 0) {
System.out.print("SCC: ");
revDfs(node, transpose, vis);
System.out.println();
}
}
}
public static void main(String args[])
{
int n = 5;
ArrayList<ArrayList<Integer> > adj = new ArrayList<ArrayList<Integer> >();
for (int i = 0; i < n; i++)
adj.add(new ArrayList<Integer>());
adj.get(0).add(1);
adj.get(1).add(2);
adj.get(2).add(0);
adj.get(1).add(3);
adj.get(3).add(4);
Main obj = new Main();
obj.kosaRaju(adj, n);
}
}
/*
5 5
0 1
1 2
2 0
1 3
3 4
*/