-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestConnectedComponent.java
More file actions
76 lines (75 loc) · 1.91 KB
/
ShortestConnectedComponent.java
File metadata and controls
76 lines (75 loc) · 1.91 KB
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
import java.util.*;
public class Scc {
static void dfs(boolean vis[],ArrayList<ArrayList<Integer>> a,int i,Stack<Integer> s)
{
vis[i]=true;
for(int j:a.get(i))
{
if(vis[j]==false) {
dfs(vis, a, j, s);
}
}
s.push(i);
}
static void reverseDfa(ArrayList<ArrayList<Integer>> transpose,int top, boolean vis[])
{
vis[top]=true;
System.out.print(top+" ");
for(int i:transpose.get(top))
{
if(vis[i]==false)
{
reverseDfa(transpose,i,vis);
}
}
}
static void kosaraju(ArrayList<ArrayList<Integer>> a,boolean vis[], int node )
{
Stack<Integer> s = new Stack<>();
for(int i=0;i<5;i++)
{
if(vis[i]==false)
{
dfs(vis,a,i,s);
}
}
ArrayList<ArrayList<Integer>> transpose = new ArrayList<ArrayList<Integer>>();
for(int i=0;i<5;i++)
{
transpose.add(new ArrayList<>());
}
for(int i=0;i<5;i++)
{
vis[i]=false;
for(int it:a.get(i)){
transpose.get(it).add(i);
}
}
while(s.size()>0)
{
int top = s.peek();
s.pop();
if(vis[top]==false)
{
System.out.print("SCC ");
reverseDfa(transpose,top,vis);
System.out.println();
}
}
}
public static void main(String arg[])
{
ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
for(int i=0;i<5;i++)
{
a.add(new ArrayList<>());
}
a.get(0).add(1);
a.get(1).add(2);
a.get(1).add(3);
a.get(2).add(0);
a.get(3).add(4);
boolean[] vis = new boolean[5];
kosaraju(a,vis,5);
}
}