-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFSPaths.java
More file actions
74 lines (65 loc) · 2.28 KB
/
BFSPaths.java
File metadata and controls
74 lines (65 loc) · 2.28 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
import java.io.File;
public class BFSPaths
{
private static final int INFINITY = Integer.MAX_VALUE;
private boolean[] marked; // marked[v] = is a shortest path to this vertex known
private int[] edgeTo; // edgeTo[v] = previous edge on shortest s-v path
private int[] distTo; // distTo[v] = number of edges shortest s-v path
// bfs from one source to other vertices
public BFSPaths(Graph G, int s) {
marked = new boolean[G.V()];
distTo = new int[G.V()];
edgeTo = new int[G.V()];
bfs(G, s);
}
// Compute shortest path between source s and every other vertex in the graph
private void bfs(Graph G, int s) {
Queue<Integer> q = new Queue<Integer>();
for (int v = 0; v < G.V(); v++)
distTo[v] = INFINITY;
distTo[s] = 0;
marked[s] = true;
q.enqueue(s);
while (!q.isEmpty()) {
int v = q.dequeue();
for (int w : G.adj(v)) {
if (!marked[w]) {
edgeTo[w] = v;
distTo[w] = distTo[v] + 1;
marked[w] = true;
q.enqueue(w);
}
}
}
}
public boolean hasPathTo(int v) { return marked[v]; }
public int distTo(int v) { return distTo[v]; }
public Iterable<Integer> pathTo(int v) {
if (!hasPathTo(v)) return null;
Stack<Integer> path = new Stack<Integer>();
int x;
for (x = v; distTo[x] != 0; x = edgeTo[x])
path.push(x);
path.push(x);
return path;
}
public static void main(String[] args) {
File file = new File(args[0]);
Graph G = new Graph(file);
int s = Integer.parseInt(args[1]);
BFSPaths bfs = new BFSPaths(G, s);
for (int v = 0; v < G.V(); v++) {
if (bfs.hasPathTo(v)) {
System.out.printf("%d to %d (%d): ", s, v, bfs.distTo(v));
for (int x : bfs.pathTo(v)) {
if (x == s) System.out.print(x);
else System.out.print("-" + x);
}
System.out.println();
}
else {
System.out.printf("%d to %d (-): not connected\n", s, v);
}
}
}
}