-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBFSSearch
90 lines (74 loc) · 2.45 KB
/
BFSSearch
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
78
79
80
81
82
83
84
85
86
87
88
89
90
package org.example;
import java.util.*;
public class BFSSearch {
public static void main(String[] args) {
BFSGraph graph = new BFSGraph();
graph.addVertex("A");
graph.addVertex("B");
graph.addVertex("C");
graph.addVertex("D");
graph.addVertex("E");
graph.addVertex("F");
graph.addVertex("G");
graph.addEdge(new BFSGraph.BFSEdge("A", "B"));
graph.addEdge(new BFSGraph.BFSEdge("A", "C"));
graph.addEdge(new BFSGraph.BFSEdge("B", "D"));
graph.addEdge(new BFSGraph.BFSEdge("B", "E"));
graph.addEdge(new BFSGraph.BFSEdge("C", "F"));
graph.addEdge(new BFSGraph.BFSEdge("E", "F"));
graph.addEdge(new BFSGraph.BFSEdge("E", "G"));
graph.addEdge(new BFSGraph.BFSEdge("F", "G"));
System.out.println("BFS starting from vertex A:");
graph.bfsSearch("A");
}
}
class BFSGraph {
private Map<String, List<BFSEdge>> adjVertices = new HashMap<>();
void addVertex(String label) {
adjVertices.putIfAbsent(label, new ArrayList<>());
}
void addEdge(BFSEdge edge) {
adjVertices.get(edge.getFrom()).add(edge);
adjVertices.get(edge.getTo()).add(new BFSEdge(edge.getTo(), edge.getFrom()));
}
Set<String> getVertices() {
return adjVertices.keySet();
}
List<BFSEdge> getEdges(String label) {
return adjVertices.get(label);
}
void bfsSearch(String start) {
Set<String> visited = new HashSet<>();
Queue<String> list = new LinkedList<>();
list.add(start);
while (!list.isEmpty()) {
String vertex = list.poll();
if (!visited.contains(vertex)) {
visited.add(vertex);
System.out.print(vertex + " ");
List<BFSEdge> edgeList = adjVertices.get(vertex);
if (edgeList != null) {
for (BFSEdge edge : edgeList) {
if (!visited.contains(edge.getTo())) {
list.add(edge.getTo());
}
}
}
}
}
}
static class BFSEdge {
private String from;
private String to;
public BFSEdge(String from, String to) {
this.from = from;
this.to = to;
}
public String getFrom() {
return from;
}
public String getTo() {
return to;
}
}
}