-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFSearch.java
31 lines (27 loc) · 960 Bytes
/
BFSearch.java
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
//Dylan Wulf
//Artificial Intelligence Project 1
//Feb 12, 2017
import java.util.LinkedList;
import java.util.Queue;
//Breadth-First Search strategy. Implemented using a Queue.
public class BFSearch implements SearchStrategy {
private Queue<ProblemNode> frontierQueue;
//Constructor
public BFSearch(ProblemNode rootNode) {
frontierQueue = new LinkedList<ProblemNode>();
frontierQueue.add(rootNode);
}
//Search returns the first node to be added to the queue which is at the goal location,
//or null if no such node is found.
public ProblemNode search() {
while (frontierQueue.size() > 0) {
ProblemNode node = frontierQueue.remove();
LinkedList<ProblemNode> nodeChildren = node.getChildNodes();
for (ProblemNode p : nodeChildren) {
if (p.isGoalNode()) return p;
frontierQueue.add(p);
}
}
return null;
}
}