-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreadthFirstDirectedPaths.java
More file actions
67 lines (58 loc) · 2.16 KB
/
Copy pathBreadthFirstDirectedPaths.java
File metadata and controls
67 lines (58 loc) · 2.16 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
/*
* Sample usage: java BreadthFirstDirectedPaths tinyDG.txt 3
* Sample usage: java BreadthFirstDirectedPaths mediumDG.txt 3
*/
public class BreadthFirstDirectedPaths {
private boolean[] marked; // marked[v] = is there a shortest s-v path?
private int[] edgeTo; // last vertex on known path to this vertex
private final int s; // the source
public BreadthFirstDirectedPaths(Digraph G, int s){
marked = new boolean[G.V()];
edgeTo = new int[G.V()];
this.s = s;
bfs(G, s);
}
private void bfs(Digraph G, int s){
LinkedQueue<Integer> queue = new LinkedQueue<>();
marked[s] = true; // mark the source
queue.enqueue(s); // put source in the queue
while(!queue.isEmpty()){
int v = queue.dequeue(); // Remove the next vertex from the queue
for (int w : G.adj(v))
if (!marked[w]){ // For every unmarked adjacent vertex
edgeTo[w] = v; // save last edge on a shortest path
marked[w] = true;// mark it because path is known
queue.enqueue(w);// and add it to the queue
}
}
}
public boolean marked(int w){
return marked[w];
}
public boolean hasPathTo(int v){
return marked[v];
}
public Iterable<Integer> pathTo(int v){
if (!hasPathTo(v)) return null;
LinkedStack<Integer> path = new LinkedStack<>();
for (int x=v; x!=s; x=edgeTo[x])
path.push(x);
path.push(s);
return path;
}
public static void main(String[] args){
Digraph G = new Digraph(new In(args[0]));
int s = Integer.parseInt(args[1]);
BreadthFirstDirectedPaths bfs = new BreadthFirstDirectedPaths(G, s);
for (int v=0; v<G.V(); v++){
StdOut.print(s + " to " + v + ": ");
if (bfs.hasPathTo(v)){
for (int x: bfs.pathTo(v))
if (x==s) StdOut.print(x);
else StdOut.print("->"+x);
StdOut.println();
}
else StdOut.print("not connected\n");
}
}
}