-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathBreadthFirstSearch.java
More file actions
58 lines (51 loc) · 1.61 KB
/
BreadthFirstSearch.java
File metadata and controls
58 lines (51 loc) · 1.61 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
package graphalgorithms;
import java.util.List;
import java.util.LinkedList;
import java.util.Queue;
public class BreadthFirstSearch {
public void bfs(Graph g, Vertex s) {
if (g == null || s == null) {
throw new IllegalArgumentException("Graph object or Vertex object null");
}
List<Vertex> vertices = g.getVertices();
for (Vertex u : vertices) {
u.color = Color.WHITE;
u.d = -1;
u.p = null;
}
s.color = Color.GRAY;
s.d = 0;
s.p = null;
Queue<Vertex> q = new LinkedList<>();
q.add(s);
System.out.printf("BFS: ");
while (!q.isEmpty()) {
Vertex u = q.poll();
System.out.printf("%s ", u);
List<Vertex> adjVertices = u.neighbors;
for (Vertex v : adjVertices) {
if (v.color == Color.WHITE) {
v.color = Color.GRAY;
v.d = u.d + 1;
v.p = u;
q.add(v);
}
}
u.color = Color.BLACK;
}
System.out.printf("%n");
}
public void printPath(Graph g, Vertex s, Vertex v) {
if (g == null || s == null || v == null) {
throw new IllegalArgumentException("Graph object or Vertex object(s) null");
}
if (v == s) {
System.out.printf("%s%n", s);
} else if (v.p == null) {
System.out.printf("no path from %s to %s exists.%n", s, v);
} else {
printPath(g, s, v.p);
System.out.printf("%s%n", v);
}
}
}