-
Notifications
You must be signed in to change notification settings - Fork 0
/
DepthFirstSearch.java
46 lines (40 loc) · 1.03 KB
/
DepthFirstSearch.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
import java.util.Stack;
/**
*
* @author TheDarkLord a.k.a Gaurav Shrivastava
*/
public class DepthFirstPaths {
private boolean[] marked; // marked[v] = is there an s-v path?
private int[] edgeTo; // edgeTo[v] = last edge on s-v path
private final int s; // source vertex
/**
* Computes a path between <tt>s</tt> and every other vertex in graph <tt>G</tt>.
* @param G the graph
* @param s the source vertex
*/
private void DFS(Graph G, int v){
marked[v] = true;
for(int w : G.adj(v))
if(!marked[w]){
DFS(G,w);
edgeTo[w] = v;
}
}
public DepthFirstPaths(Graph G, int s){
this.s = s;
edgeTo = new int[G.V()];
marked = new boolean[G.V()];
DFS(G,s);
}
public boolean hasPathTo(int v){
return marked[v];
}
public Iterable<Integer> pathTo(int v){
if(!hasPathTo(v)) return null;
Stack<Integer> path = new Stack<Integer>();
for (int x = v; x != s; x = edgeTo[x])
path.push(x);
path.push(s);
return path;
}
}