Implemented functionality to record number of nodes used.
This commit is contained in:
parent
4753ba5aa9
commit
22ffd96380
@ -19,7 +19,7 @@ public interface Solver {
|
|||||||
*
|
*
|
||||||
* @return the number of nodes from the interation.
|
* @return the number of nodes from the interation.
|
||||||
*/
|
*/
|
||||||
int getNumberOfNodes();
|
int getNumOfNodes();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Solves for, finds and creates a spanning representing the solving pattern.
|
* Solves for, finds and creates a spanning representing the solving pattern.
|
||||||
|
@ -17,6 +17,7 @@ public class DFS implements Solver {
|
|||||||
/* Private Fields for Searching */
|
/* Private Fields for Searching */
|
||||||
private final Board solved = new Board();
|
private final Board solved = new Board();
|
||||||
private final List<String> tried = new ArrayList<>();
|
private final List<String> tried = new ArrayList<>();
|
||||||
|
private int numOfNodes = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Traverses through a Board using a Stack, allowing for DFS LIFO.
|
* Traverses through a Board using a Stack, allowing for DFS LIFO.
|
||||||
@ -33,7 +34,7 @@ public class DFS implements Solver {
|
|||||||
BoardNode rootNode = new BoardNode(root, null);
|
BoardNode rootNode = new BoardNode(root, null);
|
||||||
rootNode.depth = 0;
|
rootNode.depth = 0;
|
||||||
stack.push(rootNode);
|
stack.push(rootNode);
|
||||||
|
numOfNodes = 1;
|
||||||
while (!stack.isEmpty()) {
|
while (!stack.isEmpty()) {
|
||||||
BoardNode current = stack.pop();
|
BoardNode current = stack.pop();
|
||||||
|
|
||||||
@ -54,8 +55,21 @@ public class DFS implements Solver {
|
|||||||
childNode.depth = current.depth+1;
|
childNode.depth = current.depth+1;
|
||||||
stack.push(childNode);
|
stack.push(childNode);
|
||||||
current.addChild(childNode);
|
current.addChild(childNode);
|
||||||
|
numOfNodes++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of nodes from the previous iterations.
|
||||||
|
*
|
||||||
|
* @precondition none
|
||||||
|
* @postcondition none
|
||||||
|
*
|
||||||
|
* @return the number of nodes from the interations.
|
||||||
|
*/
|
||||||
|
public int getNumOfNodes() {
|
||||||
|
return numOfNodes;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,23 +10,39 @@ import java.util.HashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.PriorityQueue;
|
import java.util.PriorityQueue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to traverse a Spanning Tree of 8 Puzzle using a provided comparitor for a Priority Queue.
|
||||||
|
*
|
||||||
|
* @author Jonathan Turner
|
||||||
|
* @version Spring 2024
|
||||||
|
*/
|
||||||
public class PriorityTraversal implements Solver {
|
public class PriorityTraversal implements Solver {
|
||||||
|
|
||||||
private final Board solved = new Board();
|
private final Board solved = new Board();
|
||||||
private final HashSet<Board> visited = new HashSet<>();
|
private final HashSet<Board> visited = new HashSet<>();
|
||||||
|
|
||||||
private final Comparator<BoardNode> comparator;
|
private final Comparator<BoardNode> comparator;
|
||||||
|
private int numOfNodes = 1;
|
||||||
|
|
||||||
public PriorityTraversal(Comparator<BoardNode> type) {
|
public PriorityTraversal(Comparator<BoardNode> type) {
|
||||||
this.comparator = type;
|
this.comparator = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Traverses through the list using a priority queue which is specified by the comparitor at constructor.
|
||||||
|
*
|
||||||
|
* @precondition root != null
|
||||||
|
* @postcondition a spanning tree is created.
|
||||||
|
*
|
||||||
|
* @param root the first node/initial start.
|
||||||
|
* @return the solved leaf node.
|
||||||
|
*/
|
||||||
public BoardNode traverse(Board root) {
|
public BoardNode traverse(Board root) {
|
||||||
visited.clear();
|
visited.clear();
|
||||||
PriorityQueue<BoardNode> boards = new PriorityQueue<>(comparator);
|
PriorityQueue<BoardNode> boards = new PriorityQueue<>(comparator);
|
||||||
boards.add(new BoardNode(root, null));
|
boards.add(new BoardNode(root, null));
|
||||||
BoardNode node = null;
|
BoardNode node = null;
|
||||||
Board current = new Board(root);
|
Board current = new Board(root);
|
||||||
|
numOfNodes = 1;
|
||||||
while (!current.equals(solved) && !boards.isEmpty()) {
|
while (!current.equals(solved) && !boards.isEmpty()) {
|
||||||
node = boards.poll();
|
node = boards.poll();
|
||||||
current = node.board;
|
current = node.board;
|
||||||
@ -41,8 +57,22 @@ public class PriorityTraversal implements Solver {
|
|||||||
BoardNode childNode = new BoardNode(child, node);
|
BoardNode childNode = new BoardNode(child, node);
|
||||||
boards.add(childNode);
|
boards.add(childNode);
|
||||||
node.addChild(childNode);
|
node.addChild(childNode);
|
||||||
|
numOfNodes++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of nodes from the previous iterations.
|
||||||
|
*
|
||||||
|
* @precondition none
|
||||||
|
* @postcondition none
|
||||||
|
*
|
||||||
|
* @return the number of nodes from the interation.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getNumOfNodes() {
|
||||||
|
return this.numOfNodes;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user