-
Notifications
You must be signed in to change notification settings - Fork 0
/
Topsort.java
62 lines (56 loc) · 1.5 KB
/
Topsort.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.List;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Map;
import java.util.HashMap;
import java.util.Queue;
import java.util.stream.IntStream;
class Topsort {
public static int[] topsort(int elements, int[][] preqs) {
/* model the Graph: [[a,b]]
* where a,b is the dest,src respectively
*/
Map<Integer, List<Integer>> adjacentMap = new HashMap<>();
/*
* in-degree of each node
*/
int[] inDegree = new int[elements];
/*
* output correct sequence
*/
int[] topOrder = new int[elements];
for (int i=0; i<preqs.length; i++) {
int src = preqs[i][1];
int dest = preqs[i][0];
List<Integer> list = adjacentMap.getOrDefault(src,
new ArrayList<Integer>());
list.add(dest);
adjacentMap.put(src, list);
inDegree[dest] += 1;
}
/*
* add all nodes whose in-degree is 0
* use a queue for that
*/
Queue<Integer> queue = new LinkedList<Integer>();
IntStream.range(0, elements)
.filter(i -> inDegree[i] == 0)
.forEach(i -> queue.add(i)); // although this is concise, this side-effect equals bad code smell
int i = 0;
while (!queue.isEmpty()) {
int node = queue.remove();
topOrder[i++] = node;
if (adjacentMap.containsKey(node)) {
for (Integer neighbour : adjacentMap.get(node)) {
inDegree[neighbour]--;
if (inDegree[neighbour] == 0) {
queue.add(neighbour);
}
}
}
}
if (i == elements)
return topOrder;
return new int[0];
}
}