-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDepthFirstOrder.java
More file actions
31 lines (27 loc) · 1016 Bytes
/
Copy pathDepthFirstOrder.java
File metadata and controls
31 lines (27 loc) · 1016 Bytes
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
public class DepthFirstOrder {
/* Topological sorting a Directed Acyclic Graph */
private boolean[] marked; // Keep track of which vertices have been visited
private Stack<Integer> reversePost; // store reverse post order
/* Constructor */
public DepthFirstOrder(Digraph G) {
this.marked = new boolean[G.V()];
this.reversePost = new Stack<Integer>();
// perform DFS over all vertices
for (int v = 0; v < G.V(); v++)
if (!marked[v]) dfs(G, v);
}
/* Recursive Depth First Search Method */
private void dfs(Digraph G, int v) {
// vertex v has now been visited
marked[v] = true;
// visit all adjacent unmarked vertices
for (int w : G.adj(v))
if (!marked[w]) dfs(G, w);
// once done with this vertex push onto reversePost
reversePost.push(v);
}
/* API: Return the topological sort order */
public Iterable<Integer> reversePost() {
return reversePost;
}
}