forked from lee2018jian/airbnb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStronglyConnectedComponents
59 lines (50 loc) · 1.66 KB
/
StronglyConnectedComponents
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
import static java.util.Arrays.asList;
import java.util.*;
public class StronglyConnectedComponents {
private void search(Set<Integer> res, Map<Integer,Set<Integer>> nodes, int cur, int start, Set<Integer> visited, Set<Integer> currVisited) {
currVisited.add(cur);
visited.add(cur);
for(int next:nodes.get(cur)) {
if(res.contains(next)&&next!=start) {
res.remove(next);
}
if(!currVisited.contains(next)) {
search(res,nodes,next,start,visited,currVisited);
}
}
}
public List<Integer> getMin(int[][] edges, int n){
Map<Integer,Set<Integer>> nodes = new HashMap<>();
for(int i = 0 ; i < edges.length ; i++) {
nodes.put(i, new HashSet<>());
}
for(int[] edge:edges) {
nodes.get(edge[0]).add(edge[1]);
}
Set<Integer> visited = new HashSet<>();
Set<Integer> res = new HashSet<>();
for(int i = 0 ; i < n ; i++) {
if(!visited.contains(i)) {
res.add(i);
visited.add(i);
search(res,nodes,i,i,visited,new HashSet<>());
}
}
return new ArrayList<>(res);
}
public static void main(String[] args) {
int[][] edges = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0 ,0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 1, 0, 1, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0 ,0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 0 ,0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 1, 0, 0, 1, 0, 0, 0}};
edges = new int[][]{{2, 9}, {3, 3}, {3, 5}, {3, 7}, {4, 8}, {5, 8}, {6, 6}, {7, 4}, {8, 7}, {9, 3}, {9, 6}};
System.out.println(new StronglyConnectedComponents().getMin(edges, 10));
}
}