-
Notifications
You must be signed in to change notification settings - Fork 0
/
StronglyConnectedComp.java
74 lines (64 loc) · 1.69 KB
/
StronglyConnectedComp.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
69
70
71
72
73
74
class Solution
{
//Function to find number of strongly connected components in the graph.4
public static void dfs(ArrayList<ArrayList<Integer>> adh , int i , boolean[]visited , Stack<Integer> st)
{
visited[i]=true;
for(int a : adh.get(i))
{
if(visited[a]==false)
{
dfs(adh , a , visited , st);
}
}
st.add(i);
}
public void df2(ArrayList<ArrayList<Integer>> al , boolean vis[], int t )
{
vis[t]=true;
for(int aa : al.get(t))
{
if(vis[aa]==false)
{
df2(al,vis,aa);
}
}
}
public int kosaraju(int V, ArrayList<ArrayList<Integer>> adj)
{
//code here
Stack<Integer> st = new Stack<>();
boolean[] visited= new boolean[V];
for(int i=0;i<V;i++)
{
if(visited[i]==false)
dfs(adj , i,visited,st);
}
ArrayList<ArrayList<Integer>> ad2 = new ArrayList<>();
for(int i=0;i<V;i++)
{
ad2.add(new ArrayList<>());
}
for(int i=0;i<adj.size();i++)
{
for(int j=0;j<adj.get(i).size();j++)
{
int u=i;
int vv= adj.get(i).get(j);
ad2.get(vv).add(i);
}
}
boolean vis[]= new boolean[V];
int ans=0;
while(st.size()>0)
{
int t=st.pop();
if(vis[t]==false)
{
ans++;
df2(ad2 , vis , t);
}
}
return ans;
}
}