-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path02-Kosaraju.java
62 lines (52 loc) · 1.85 KB
/
02-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
package graphs.advanced;
import graphs.DirectedGraph;
import graphs.Graph;
import java.util.Arrays;
import java.util.Stack;
public class Kosaraju {
/**
* Find the strongly connected components of a directed graph using the Kosaraju alori
* @param graph
* @return
*/
public static int[] getStronglyConnectedComponents(DirectedGraph graph) {
Stack<Integer> s = new Stack<>();
Arrays.fill(graph.visited, false);
for (int node = 0; node < graph.vertices; node++) {
if (!graph.visited[node]) {
dfs(graph, node, s);
}
}
Graph reverseGraph = new Graph(graph.vertices);
graph.edgesList.forEach(edge -> reverseGraph.addEdge(edge.to, edge.from, edge.cost));
int[] components = new int[graph.vertices];
Arrays.fill(graph.visited, false);
int componentNumber = 0;
while (!s.empty()) {
int node = s.pop();
if (!reverseGraph.visited[node]) {
dfsReverse(reverseGraph, node, components, componentNumber);
componentNumber++;
}
}
return components;
}
private static void dfs(Graph graph, int node, Stack<Integer> s) {
graph.visited[node] = true;
graph.adjacencyList.get(node).forEach(neighbour -> {
if (!graph.visited[neighbour.to]) {
dfs(graph, neighbour.to, s);
}
});
s.push(node);
}
private static void dfsReverse(Graph graph, int node, int[] components, int componentNumber) {
graph.visited[node] = true;
components[node] = componentNumber;
graph.adjacencyList.get(node).forEach(neighbour -> {
if (!graph.visited[neighbour.to]) {
dfsReverse(graph, neighbour.to, components, componentNumber);
}
});
}
}