-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Kahns.java
140 lines (125 loc) · 3.98 KB
/
Kahns.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
/**
* Implementation of Kahn's algorithm to find a topological ordering
*
* <p>Kahn's algorithm finds a topological ordering by iteratively removing nodes in the graph which
* have no incoming edges. When a node is removed from the graph, it is added to the topological
* ordering and all its edges are removed allowing for the next set of nodes with no incoming edges
* to be selected.
*
* <p>Verified against: https://open.kattis.com/problems/builddeps
*
* <p>./gradlew run -Palgorithm=graphtheory.Kahns
*
* <p>Time complexity: O(V+E)
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.graphtheory;
import static com.williamfiset.algorithms.utils.graphutils.Utils.addDirectedEdge;
import static com.williamfiset.algorithms.utils.graphutils.Utils.createEmptyAdjacencyList;
import java.util.*;
public class Kahns {
// Given a an acyclic graph `g` represented as a adjacency list, return a
// topological ordering on the nodes of the graph.
public int[] kahns(List<List<Integer>> g) {
int n = g.size();
// Calculate the in-degree of each node.
int[] inDegree = new int[n];
for (List<Integer> edges : g) {
for (int to : edges) {
inDegree[to]++;
}
}
// q always contains the set nodes with no incoming edges.
Queue<Integer> q = new ArrayDeque<>();
// Find all start nodes.
for (int i = 0; i < n; i++) {
if (inDegree[i] == 0) {
q.offer(i);
}
}
int index = 0;
int[] order = new int[n];
while (!q.isEmpty()) {
int at = q.poll();
order[index++] = at;
for (int to : g.get(at)) {
inDegree[to]--;
if (inDegree[to] == 0) {
q.offer(to);
}
}
}
if (index != n) {
throw new IllegalArgumentException("Graph is not acyclic! Detected a cycle.");
}
return order;
}
// Example usage:
public static void main(String[] args) {
exampleFromSlides();
// test1();
// test2();
// cycleTest();
}
private static void exampleFromSlides() {
List<List<Integer>> g = createEmptyAdjacencyList(14);
addDirectedEdge(g, 0, 2);
addDirectedEdge(g, 0, 3);
addDirectedEdge(g, 0, 6);
addDirectedEdge(g, 1, 4);
addDirectedEdge(g, 2, 6);
addDirectedEdge(g, 3, 1);
addDirectedEdge(g, 3, 4);
addDirectedEdge(g, 4, 5);
addDirectedEdge(g, 4, 8);
addDirectedEdge(g, 6, 7);
addDirectedEdge(g, 6, 11);
addDirectedEdge(g, 7, 4);
addDirectedEdge(g, 7, 12);
addDirectedEdge(g, 9, 2);
addDirectedEdge(g, 9, 10);
addDirectedEdge(g, 10, 6);
addDirectedEdge(g, 11, 12);
addDirectedEdge(g, 12, 8);
Kahns solver = new Kahns();
int[] ordering = solver.kahns(g);
// Prints: [0, 9, 13, 3, 2, 10, 1, 6, 7, 11, 4, 12, 5, 8]
System.out.println(java.util.Arrays.toString(ordering));
}
private static void test1() {
List<List<Integer>> g = createEmptyAdjacencyList(6);
addDirectedEdge(g, 0, 1);
addDirectedEdge(g, 0, 2);
addDirectedEdge(g, 1, 2);
addDirectedEdge(g, 3, 1);
addDirectedEdge(g, 3, 2);
addDirectedEdge(g, 2, 4);
addDirectedEdge(g, 4, 5);
Kahns solver = new Kahns();
System.out.println(java.util.Arrays.toString(solver.kahns(g)));
}
private static void test2() {
List<List<Integer>> g = createEmptyAdjacencyList(6);
addDirectedEdge(g, 0, 1);
addDirectedEdge(g, 0, 2);
addDirectedEdge(g, 0, 5);
addDirectedEdge(g, 1, 2);
addDirectedEdge(g, 1, 3);
addDirectedEdge(g, 2, 3);
addDirectedEdge(g, 2, 4);
addDirectedEdge(g, 3, 4);
addDirectedEdge(g, 5, 4);
Kahns solver = new Kahns();
System.out.println(java.util.Arrays.toString(solver.kahns(g)));
}
private static void cycleTest() {
List<List<Integer>> g = createEmptyAdjacencyList(4);
addDirectedEdge(g, 0, 1);
addDirectedEdge(g, 1, 2);
addDirectedEdge(g, 2, 3);
addDirectedEdge(g, 3, 0);
Kahns solver = new Kahns();
System.out.println(java.util.Arrays.toString(solver.kahns(g)));
}
}