-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Kosaraju.java
248 lines (211 loc) · 6.05 KB
/
Kosaraju.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* Implementation of Kosaraju's SCC algorithm
*
* <p>Verified against:
*
* <ul>
* <li>https://open.kattis.com/problems/equivalences
* <li>https://open.kattis.com/problems/runningmom
* </ul>
*
* <p>./gradlew run -Palgorithm=graphtheory.Kosaraju
*/
package com.williamfiset.algorithms.graphtheory;
import java.util.*;
public class Kosaraju {
private int n;
private int sccCount;
private boolean solved;
private int[] sccs;
private boolean[] visited;
// The post order forest traversal of the original graph resulting from the first DFS.
private List<Integer> postOrderTraversal;
private List<List<Integer>> graph;
private List<List<Integer>> transposeGraph;
public Kosaraju(List<List<Integer>> graph) {
if (graph == null) throw new IllegalArgumentException("Graph cannot be null.");
this.graph = graph;
n = graph.size();
}
// Returns the number of strongly connected components in the graph.
public int sccCount() {
if (!solved) solve();
return sccCount;
}
// Get the connected components of this graph. If two indexes
// have the same value then they're in the same SCC.
public int[] getSccs() {
if (!solved) solve();
return sccs;
}
private void solve() {
sccCount = 0;
sccs = new int[n];
visited = new boolean[n];
postOrderTraversal = new ArrayList<>();
for (int i = 0; i < n; i++) {
dfs1(i);
}
Arrays.fill(visited, false);
createTransposeGraph();
// Reverse the post order traversal to make iterating through it
// in the next step more intuitive.
Collections.reverse(postOrderTraversal);
for (int node : postOrderTraversal) {
if (!visited[node]) {
dfs2(node);
sccCount++;
}
}
solved = true;
}
// Traverse the original graph and add nodes to the post order traversal on the callback.
private void dfs1(int from) {
if (visited[from]) {
return;
}
visited[from] = true;
for (int to : graph.get(from)) {
dfs1(to);
}
postOrderTraversal.add(from);
}
// Traverse the transverse graph and label all the encountered nodes as part of the sane SCC.
private void dfs2(int from) {
if (visited[from]) {
return;
}
visited[from] = true;
for (int to : transposeGraph.get(from)) {
dfs2(to);
}
sccs[from] = sccCount;
}
private void createTransposeGraph() {
transposeGraph = createGraph(n);
for (int u = 0; u < n; u++) {
for (int v : graph.get(u)) {
addEdge(transposeGraph, v, u);
}
}
}
// Initializes adjacency list with n nodes.
public static List<List<Integer>> createGraph(int n) {
List<List<Integer>> graph = new ArrayList<>(n);
for (int i = 0; i < n; i++) graph.add(new ArrayList<>());
return graph;
}
// Adds a directed edge from node 'from' to node 'to'
public static void addEdge(List<List<Integer>> graph, int from, int to) {
graph.get(from).add(to);
}
public static void main(String[] args) {
example1();
// example2();
// example3();
// example4();
// exampleFromCp4();
}
private static void exampleFromCp4() {
int n = 8;
List<List<Integer>> graph = createGraph(n);
addEdge(graph, 0, 1);
addEdge(graph, 1, 3);
addEdge(graph, 2, 1);
addEdge(graph, 3, 2);
addEdge(graph, 3, 4);
addEdge(graph, 4, 5);
addEdge(graph, 5, 7);
addEdge(graph, 6, 4);
addEdge(graph, 7, 6);
runKosaraju(graph);
}
private static void example4() {
int n = 8;
List<List<Integer>> graph = createGraph(n);
// [0, 3, 2, 1, 7, 6, 5, 4]
addEdge(graph, 0, 2);
addEdge(graph, 0, 3);
addEdge(graph, 0, 5);
addEdge(graph, 1, 4);
addEdge(graph, 1, 7);
addEdge(graph, 2, 1);
addEdge(graph, 3, 0);
addEdge(graph, 3, 4);
addEdge(graph, 4, 2);
addEdge(graph, 5, 7);
addEdge(graph, 6, 5);
addEdge(graph, 7, 6);
runKosaraju(graph);
}
private static void example3() {
int n = 6;
List<List<Integer>> graph = createGraph(n);
// [4, 2, 5, 0, 3, 1]
addEdge(graph, 0, 2);
addEdge(graph, 0, 5);
addEdge(graph, 1, 0);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
addEdge(graph, 3, 1);
addEdge(graph, 3, 5);
addEdge(graph, 4, 0);
runKosaraju(graph);
}
private static void example2() {
// [8, 9, 5, 4, 7, 3, 2, 6, 1, 0]
// [5, 4, 8, 9, 3, 2, 7, 1, 6, 0]
int n = 10;
List<List<Integer>> graph = createGraph(n);
addEdge(graph, 0, 1);
addEdge(graph, 1, 2);
addEdge(graph, 1, 6);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
addEdge(graph, 3, 7);
addEdge(graph, 4, 5);
addEdge(graph, 5, 9);
addEdge(graph, 6, 1);
addEdge(graph, 7, 2);
addEdge(graph, 8, 4);
addEdge(graph, 9, 8);
runKosaraju(graph);
}
private static void example1() {
int n = 8;
List<List<Integer>> graph = createGraph(n);
addEdge(graph, 6, 0);
addEdge(graph, 6, 2);
addEdge(graph, 3, 4);
addEdge(graph, 6, 4);
addEdge(graph, 2, 0);
addEdge(graph, 0, 1);
addEdge(graph, 4, 5);
addEdge(graph, 5, 6);
addEdge(graph, 3, 7);
addEdge(graph, 7, 5);
addEdge(graph, 1, 2);
addEdge(graph, 7, 3);
addEdge(graph, 5, 0);
// Prints:
// Number of Strongly Connected Components: 3
// Nodes: [3, 7] form a Strongly Connected Component.
// Nodes: [4, 5, 6] form a Strongly Connected Component.
// Nodes: [0, 1, 2] form a Strongly Connected Component.
runKosaraju(graph);
}
private static void runKosaraju(List<List<Integer>> graph) {
int n = graph.size();
Kosaraju solver = new Kosaraju(graph);
int[] sccs = solver.getSccs();
Map<Integer, List<Integer>> multimap = new HashMap<>();
for (int i = 0; i < n; i++) {
if (!multimap.containsKey(sccs[i])) multimap.put(sccs[i], new ArrayList<>());
multimap.get(sccs[i]).add(i);
}
System.out.printf("Number of Strongly Connected Components: %d\n", solver.sccCount());
for (List<Integer> scc : multimap.values()) {
System.out.println("Nodes: " + scc + " form a Strongly Connected Component.");
}
}
}