-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDirectedDFS.java
More file actions
50 lines (45 loc) · 1.3 KB
/
Copy pathDirectedDFS.java
File metadata and controls
50 lines (45 loc) · 1.3 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
public class DirectedDFS {
// mark all vertices that have been visited
private boolean[] marked;
// maintain how we 1st reached a vertex
private int[] edgeTo;
// source vertex
private int s;
/* Constructor */
public DirectedDFS(Digraph G, int s) {
this.s = s;
this.marked = new boolean[G.V()];
this.edgeTo = new int[G.V()];
dfs(G, s);
}
/* Recursive depth-first search method */
private void dfs(Digraph G, int v) {
marked[v] = true; // has been visited
for (int w : G.adj(v)) {
// visit every unmarked vertex adjacent to v
if (!marked[w]) {
dfs(G, w);
edgeTo[w] = v;
}
}
}
/* API: Is vertex v connected to s? */
public boolean hasPathTo(int v) {
// O(1)
return marked[v];
}
/* API: Return an iterable having the path from s to v */
public Iterable<Integer> pathTo(int v) {
// O(N)
// if no path return null
if (!hasPathTo(v)) return null;
// Stack is an iterable!
Stack<Integer> path = new Stack<Integer>();
while (v != s) {
path.push(v);
v = edgeTo[v];
}
path.push(s); // push source vertex
return path;
}
}