-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS_findpath.java
More file actions
34 lines (33 loc) · 1.14 KB
/
Copy pathBFS_findpath.java
File metadata and controls
34 lines (33 loc) · 1.14 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
import java.util.LinkedList;
public class BFS_findpath {
/////BFS find the path from v to w and print it
public static String findPath(int v, int w,Graph G) {
LinkedList<Integer> q = new LinkedList<Integer>();
boolean[] visited = new boolean[G.getNumVertices()];
String[] pathTo = new String[G.getNumVertices()];/// pathto means the path
q.add(v);
pathTo[v] = v+" ";
while(q.peek() != null) {
if(runBFS(q.poll(),w,visited,G,q,pathTo))
break;
}
return pathTo[w];
}
////use BFS to find the path from v to w
private static boolean runBFS(int v, int w, boolean[] visited,Graph G, LinkedList<Integer> q, String[] pathTo) {
if(visited[v]) {
}
else if(v == w)
return true;
else {
visited[v] = true;
LinkedList<node> vi = G.getList(v);
for(int i =0;i<vi.size();i++) {
int nextVertex = vi.get(i).destination;
pathTo[nextVertex] = pathTo[v] + nextVertex + " ";
q.add(nextVertex);
}
}
return false;
}
}