Added a maximum node count to DFS.

This commit is contained in:
Harrison Deng 2020-04-22 18:40:17 -05:00
parent 39b5d0e778
commit 44c6880e07

View File

@ -9,23 +9,34 @@ import org.apache.commons.lang.NullArgumentException;
public class DepthFirstSearch {
private Queue<Node> queue;
private CoordinateValidatable validatable;
private CoordinateValidatable coordValidatable;
private Node startNode;
private Node endNode;
private HashSet<Node> checkedNodes;
private int maxNodesSearched = -1;
public DepthFirstSearch() {
checkedNodes = new HashSet<>();
queue = new PriorityQueue<>();
}
public DepthFirstSearch(int maxNodesSearched) {
this();
this.maxNodesSearched = maxNodesSearched;
}
public DepthFirstSearch(int searchRadius, CoordinateValidatable validatable) {
this(searchRadius);
setValidatable(validatable);
}
public DepthFirstSearch(CoordinateValidatable validatable) {
this();
this.validatable = validatable;
setValidatable(validatable);
}
public void setValidatable(CoordinateValidatable validatable) {
this.validatable = validatable;
this.coordValidatable = validatable;
}
public void setStartPosition(int x, int y) {
@ -50,7 +61,7 @@ public class DepthFirstSearch {
}
public boolean buildPathToEndNode() {
if (validatable == null) throw new IllegalStateException("Need to set a validator.");
if (coordValidatable == null) throw new IllegalStateException("Need to set a validator.");
if (startNode == null) throw new IllegalStateException("Need to set the starting position.");
queue.clear();
@ -58,6 +69,7 @@ public class DepthFirstSearch {
Node begin = startNode;
queue.add(begin);
while (!queue.isEmpty()) {
if (maxNodesSearched != -1 && checkedNodes.size() >= maxNodesSearched) return false;
Node n = queue.poll();
if (!n.equals(endNode)) {
n.child[0] = new Node(n, n.x + 1, n.y);
@ -71,7 +83,7 @@ public class DepthFirstSearch {
if (checkedNodes.add(n)) {
for (int i = 0; i < n.child.length; i++) {
Node child = n.child[i];
if (validatable.validate(child.x, child.y)) {
if (coordValidatable.validate(child.x, child.y)) {
queue.add(child);
}
}
@ -89,7 +101,7 @@ public class DepthFirstSearch {
* @return
*/
public boolean findTarget(CoordinateTargetValidatable targetValidator) {
if (validatable == null) throw new IllegalStateException("Need to set a validator.");
if (coordValidatable == null) throw new IllegalStateException("Need to set a validator.");
if (targetValidator == null) throw new NullArgumentException("targetValidator");
if (startNode == null) throw new IllegalStateException("Need to set the starting position.");
@ -99,6 +111,7 @@ public class DepthFirstSearch {
queue.add(begin);
while (!queue.isEmpty()) {
Node n = queue.poll();
if (maxNodesSearched != -1 && checkedNodes.size() >= maxNodesSearched) return false;
if (!targetValidator.isCoordinateTarget(n.x, n.y)) {
n.child[0] = new Node(n, n.x + 1, n.y);
n.child[1] = new Node(n, n.x - 1, n.y);
@ -111,7 +124,7 @@ public class DepthFirstSearch {
if (checkedNodes.add(n)) {
for (int i = 0; i < n.child.length; i++) {
Node child = n.child[i];
if (validatable.validate(child.x, child.y)) {
if (coordValidatable.validate(child.x, child.y)) {
queue.add(child);
}
}