-
Notifications
You must be signed in to change notification settings - Fork 0
/
TopologicalSort.java
62 lines (57 loc) · 1.66 KB
/
TopologicalSort.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
import java.util.*;
class Program {
// O(j + d) time | O(j + d) space
public static List<Integer> topologicalSort(List<Integer> jobs, List<Integer[]> deps) {
// Write your code here.
Map<Integer, List<Integer>> graph = buildGraph(deps);
Map<Integer, State> statesMap = buildStatesMap(jobs);
List<Integer> order = new ArrayList<Integer>();
for (int vertex : jobs) {
if (statesMap.get(vertex).equals(State.TO_VISIT) && !dfs(order, vertex, graph, statesMap)) {
return new ArrayList<>();
}
}
return order;
}
private static Map<Integer, List<Integer>> buildGraph(List<Integer[]> edges) {
Map<Integer, List<Integer>> graph = new HashMap<>();
for (Integer[] edge: edges) {
int start = edge[0];
int end = edge[1];
graph.putIfAbsent(start, new ArrayList<>());
graph.get(start).add(end);
}
return graph;
}
private enum State {
TO_VISIT,
VISITING,
VISITED;
}
private static Map<Integer, State> buildStatesMap(List<Integer> jobs) {
Map<Integer, State> statesMap = new HashMap<>();
for (int vertex : jobs) {
statesMap.put(vertex, State.TO_VISIT);
}
return statesMap;
}
private static boolean dfs(List<Integer> order, int vertex, Map<Integer, List<Integer>> graph, Map<Integer, State> statesMap) {
statesMap.put(vertex, State.VISITING);
if (graph.containsKey(vertex)) {
for (int nei : graph.get(vertex)) {
if (statesMap.get(nei).equals(State.VISITED)) {
continue;
}
if (statesMap.get(nei).equals(State.VISITING)) {
return false;
}
if (!dfs(order, nei, graph, statesMap)) {
return false;
}
}
}
statesMap.put(vertex, State.VISITED);
order.add(0, vertex);
return true;
}
}