-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopologicalSort.java
More file actions
50 lines (45 loc) · 1.54 KB
/
TopologicalSort.java
File metadata and controls
50 lines (45 loc) · 1.54 KB
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
package graph_algorithms;
import data_structures.LinkedList;
import data_structures.Stack;
public class TopologicalSort {
public static void main(String[] args) {
// Example:
Graph graph = new AdjacencyMatrixGraph(7, true);
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 2);
graph.addEdge(3, 0);
graph.addEdge(4, 5);
graph.addEdge(5, 4);
graph.addEdge(5, 6);
System.out.println(topologicalSort(graph)); // [4, 5, 6, 3, 0, 1, 2]
}
public static LinkedList<Integer> topologicalSort(Graph graph) {
Stack<Integer> stack = new LinkedList<>();
LinkedList<Integer> visited = new LinkedList<>(); // TODO: Use Dictionary once AVLTree is finished
LinkedList<Integer> topo = new LinkedList<>();
int vertexCount = graph.getVertexCount();
for (int s = 0; s < vertexCount; ++s) {
if (visited.contains(s)) {
continue;
}
stack.push(s);
while (stack.top() != null) {
int u = stack.top();
if (visited.contains(u)) {
stack.pop();
topo.addFirst(u);
} else {
visited.addLast(u);
for (int v : graph.outEdges(u)) {
if (visited.contains(v)) {
continue;
}
stack.push(v);
}
}
}
}
return topo;
}
}