forked from OneCodeMonkey/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepthFirstPaths.java
More file actions
77 lines (71 loc) · 1.85 KB
/
DepthFirstPaths.java
File metadata and controls
77 lines (71 loc) · 1.85 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Run depth first search on an undirected graph.
* Runs in O(E + V) time.
*
*/
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 `s` and every other vertex in graph `G`
public DepthFirstPaths(Graph G, int s) {
this.s = s;
edgeTo = new int[G.V()];
marked = new boolean[G.V()];
validateVertex(s);
dfs(G, s);
}
// depth first search from v
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);
}
}
}
// is there a path between the source vertex `s` and vertex `v`
public boolean hasPathTo(int v) {
validateVertex(v);
return marked[v];
}
// Returns a path between the source vertex `s` and the vertex `v`
// return null if no such path.
public Iterable<Integer> pathTo(int v) {
validateVertex(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;
}
// throw an IllegalArgumentException unless `0 <= v < V`
private void validateVertex(int v) {
int V = marked.length;
if(v < 0 || v >= V)
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V - 1));
}
// tests
public static void main(String[] args) {
In in = new In(args[0]);
Graph G = new Graph(in);
int s = Integer.parseInt(args[1]);
DepthFirstPaths dfs = new DepthFirstPaths(G, s);
for(int v = 0; v < G.(); v++) {
if(dfs.hasPathTo(v)) {
StdOut.printf("%d to %d: ", s, v);
for(int x:dfs.pathTo(v)) {
if(x == s)
StdOut.print(x);
else
StdOut.print("-" + x);
}
} else {
StdOut.printf("%d to %d: not connected\n", s, v);
}
}
}
}