Question
Write a Java program which implements Breadth-First Search and Depth-First Search algorithms to find a solution for the Water Jugs Problem. The start state is
Write a Java program which implements Breadth-First Search and Depth-First Search algorithms to find a solution for the Water Jugs Problem. The start state is (0, 0); goal states are (2, *) (either state (2, 0) or state (2, 3)). To keep things simple, a Node class is provided for you. (You can find the file Node.java on Blackboard). In this class, the method expand() generates all possible successors of one node and returns the list of all successors. You need to write a new class named WaterJugs to implement and test your algorithms.
Requirements:
1) Create a method BFS that applies the Breadth-Frist search.
2) Create a method DFS that applies the Depth-First search.
3) Output the path from the start state to the goal state.
4) Your program should be able to avoid repeated states in order to find the goal state.
5)Add a counter to your methods that keeps track of how many nodes were created during a computation, as a measure of the algorithms efficiency. Let the program output that number.
6) Your program should be well-documented. Variable names and function names should be self-descriptive. Major functions should be explained clearly in comments.
7) Test your program. Make sure your program compiles and runs correctly.
NODE CLASS
import java.util.ArrayList;
public class Node { public final int CAPACITY_X = 4; public final int CAPACITY_Y = 3; private int x; private int y; private Node parent; /** * constructor method * @param x - water volume in first jug * @param y - water volume in second jug */ public Node(int x, int y) { this.x = x; this.y = y; this.parent = null; } /** * * @return the current water volume in first jug */ public int getX() { return x; } /** * * @return the current water volume in second jug */ public int getY() { return y; } /** * * @return the parent state */ public Node getParent() { return parent; } /** * set the parent state of the current node * @param parent */ public void setParent(Node parent) { this.parent = parent; } /** * * @return the set of successors of current node */ public ArrayList
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started