-
Notifications
You must be signed in to change notification settings - Fork 0
/
TopologicalSortDfs.java
54 lines (45 loc) · 1.38 KB
/
TopologicalSortDfs.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
package graph;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class TopologicalSortDfs implements TopologicalSort {
private Graph graph;
public TopologicalSortDfs(Graph graph) {
this.graph = graph;
}
@Override
// Time Complexity O(V + E)
public List<Integer> sort() {
List<Integer> result = new ArrayList<>();
boolean[] visited = new boolean[graph.getVerticesCount()];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < graph.getVerticesCount(); i++) {
if (!visited[i]) {
dfs(i, visited, stack);
}
}
while (!stack.empty()) {
result.add(stack.pop());
}
return result;
}
private void dfs(int source, boolean[] visited, Stack<Integer> stack) {
visited[source] = true;
for (int adj: graph.adjacent(source)) {
if (!visited[adj]) {
dfs(adj, visited, stack);
}
}
stack.push(source);
}
public static void main(String[] args) {
Graph graph = new DirectedGraphAdjacencyList(5);
graph.addEdge(0,1);
graph.addEdge(1, 3);
graph.addEdge(3, 4);
graph.addEdge(2, 3);
graph.addEdge(2, 4);
TopologicalSort sort = new TopologicalSortDfs(graph);
System.out.println(sort.sort());
}
}