'Breadth-First and Depth-First Search Are Generating the Same Statistics
I need to make a program that will automatically solve and display search statistics for an 8-puzzle problem using 4 different search algorithms: Breadth-First, Depth-First, Best-First and A star.
I am able to generate the correct statistics for BF search, Best-First search and A star search, but for some reason, my DF search algorithm is yielding the same statistics as the BF search. I am really stumped as to why this only happens with DF search.
I am using a ChoiceBox to allow the user to select which search algorithm to use, and the way I created the ChoiceBox and the ActionListener is shown here:
private VBox makeSearchTypeDropdown()
{
VBox searchTypeDropdown = new VBox();
searchTypeLabel = new Label("Search Type");
searchTypeLabel.setFont(new Font(14));
String searchTypeNames[] = {"BF State Space Search", "DF State Space Search", "Best-First Search", "A* Search"};
searchTypes = new ChoiceBox<>(FXCollections.observableArrayList(searchTypeNames));
searchTypeDropdown.setPadding(new Insets(10, 10, 10, 10));
searchTypeDropdown.setAlignment(Pos.CENTER);
searchTypeDropdown.getChildren().addAll(searchTypeLabel, searchTypes);
return searchTypeDropdown;
}
private void addSearchSelectListener()
{
searchTypes.setOnAction(e -> {
switch((String) searchTypes.getValue())
{
case "BF State Space Search":
curSolver = new BFSStateSpaceSolver(prob); //curSolver is of an abstract solver type
break;
case "DF State Space Search":
curSolver = new DFSStateSpaceSolver(prob);
break;
case "Best-First Search":
curSolver = new BestFirstSolver(prob);
break;
case "A* Search":
curSolver = new AStarSolver(prob);
break;
}
});
}
I'm not too sure if this will be useful to include or not, but here is the code that was provided to use depth-first search:
public class DFSStateSpaceSolver extends StateSpaceSolver {
public DFSStateSpaceSolver(Problem problem) {
super(problem);
super.getStatistics().setHeader("Depth-First Search");
}
@Override
public void add(Vertex v) {
((LinkedList<Vertex>)getQueue()).addFirst(v); // DFS
}
}
Thank you for the help!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
