-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kosaraju_Algorithm.java
86 lines (78 loc) · 2.35 KB
/
Kosaraju_Algorithm.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
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.*;
public class Kosaraju_Algorithm {
public static class edge{
int src;
int dest;
edge(int s,int d){
this.src = s;
this.dest = d;
}
}
public static void createGraph(ArrayList<edge>graph[]){
for(int i = 0;i<graph.length;i++){
graph[i] = new ArrayList<>();
}
graph[0].add(new edge(0,2));
graph[0].add(new edge(0,3));
graph[1].add(new edge(1,0));
graph[2].add(new edge(2,1));
graph[3].add(new edge(3,4));
}
public static void topoSort(ArrayList<edge>graph[],int curr,boolean vis[],Stack<Integer>s){
vis[curr] = true;
for(int i = 0;i<graph[curr].size();i++){
edge e = graph[curr].get(i);
if(!vis[e.dest]){
topoSort(graph, e.dest, vis, s);
}
}
s.push(curr);
}
public static void dfs(ArrayList<edge>graph[],boolean vis[],int curr){
vis[curr] = true;
System.out.print(curr+" ");
for(int i = 0;i<graph[curr].size();i++){
edge e = graph[curr].get(i);
if(!vis[e.dest]){
dfs(graph,vis,e.dest);
}
}
}
public static void KosarajuAlgo(ArrayList<edge>graph[],int v){
//step 1;
Stack<Integer>s = new Stack<>();
boolean vis[] = new boolean[v];
for(int i = 0;i<graph.length;i++){
if(!vis[i]){
topoSort(graph, i, vis, s);
}
}
//step 2;
ArrayList<edge>graphTrav[] = new ArrayList[v];
for(int i = 0;i<graph.length;i++){
graphTrav[i] = new ArrayList<>();
vis[i] = false;
}
for(int i =0;i<graph.length;i++){
for(int j = 0;j<graph[i].size();i++){
edge e = graph[i].get(j);
graphTrav[e.dest].add(new edge(e.dest, e.src));
}
}
// step 3;
while(!s.isEmpty()){
int curr = s.pop();
if(!vis[curr]){
System.out.print("SCC -> ");
dfs(graphTrav, vis, curr);
System.out.println();
}
}
}
public static void main(String arg[]){
int v = 5;
ArrayList<edge>graph[] = new ArrayList[v];
createGraph(graph);
KosarajuAlgo(graph,v);
}
}