-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFSPaths.java
More file actions
63 lines (50 loc) · 1.59 KB
/
DFSPaths.java
File metadata and controls
63 lines (50 loc) · 1.59 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
51
52
53
54
55
56
57
58
59
60
61
62
63
public class DFSPaths {
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
public DFSPaths(Graph G, int s) {
this.s = s;
edgeTo = new int[G.V()];
marked = new boolean[G.V()];
dfs(G, s);
}
// depth first search from v. Solves single source connectivity/reachability
private void dfs(Graph G, int v){
marked[v]=true;
for(int w : G.adj(v)) {
if (!marked[w]) {
edgeTo[w] = v;
dfs(G, w);
}
}
}
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;
}
public static void main(String[] args) {
Graph G = new Graph(); // from stdin
int s = 0;// Integer.parseInt(args[1]);
DFSPaths dfs = new DFSPaths(G, s);
for (int v = 0; v < G.V(); v++) {
if (dfs.hasPathTo(v)) {
System.out.print(s + " to " + v + ": ");
for (int x : dfs.pathTo(v)) {
if (x == s) System.out.print(x);
else System.out.print("-" + x);
}
System.out.println();
}
else {
System.out.println(s + " to " + v + ": not connected");
}
}
}
}