Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Practical-3

3. Write a program to implement DFS (for 8 puzzle problem or Water Jug


problem or any AI search problem.
Water Jug Problem:
You are given 2 jugs with the capacity 'm' and 'n' respectively.
Initially, they are given empty. There is an unlimited supply of water. You can either fill
the whole jug or a quantity that is less than the given capacity of jugs. Now, you are also
given a third positive integer 'd'. Using the 2 given jugs, you need to come up with a
solution to have 'd' amount of water in them and return the number of steps you took to
reach that capacity.

I. Define the State Representation:


 Define a data structure to represent the state of the jugs. Each state should
include the current amount of water in both jugs.

II. Create a Stack:


 Initialize an empty stack to keep track of the states to explore. You can use a
list as a stack.

III. Define the Goal:


 Specify the goal you want to achieve with the jugs, such as a specific amount
of water that you want to measure.

IV. Define Valid Operations:


 Determine the valid operations you can perform on the jugs: filling, emptying,
and pouring water from one jug to another.

V. Implement the DFS Algorithm:


 Start with an initial state where both jugs are empty.
 Push the initial state onto the stack.
 Create a set to keep track of visited states to avoid revisiting the same state.
 Enter a loop until the stack is empty:
 Pop the top state from the stack.
 Check if it satisfies the goal condition. If yes, return the path to the
solution.
 Generate all possible states reachable from the current state by applying
valid operations.
 Filter out states that have been visited before.
 Push the unvisited next states onto the stack.
VI. Return the Solution Path:
 If the DFS algorithm finds a solution, return the path from the initial state to
the goal state, which represents the sequence of operations needed to
achieve the goal.

VII. Implement the Water Jug Problem using BFS in Python:


class JugState:
def __init__(self, x, y):
self.x = x
self.y = y

def __eq__(self, other):


return self.x == other.x and self.y == other.y

def __hash__(self):
return hash((self.x, self.y))

def pour_jugs_dfs(x, y, target):


stack = [(JugState(0, 0), [])] # (state, path)
visited = set()

while stack:
current_state, path = stack.pop()
visited.add(current_state)

if current_state.x == target or current_state.y == target:


path.append(current_state)
return path

next_states = []

# Fill jug x
if current_state.x < x:
new_x = x
new_y = current_state.y
new_state = JugState(new_x, new_y)
if new_state not in visited:
next_states.append((new_state, path + [current_state]))

# Fill jug y
if current_state.y < y:
new_x = current_state.x
new_y = y
new_state = JugState(new_x, new_y)
if new_state not in visited:
next_states.append((new_state, path + [current_state]))

# Empty jug x
if current_state.x > 0:
new_x = 0
new_y = current_state.y
new_state = JugState(new_x, new_y)
if new_state not in visited:
next_states.append((new_state, path + [current_state]))

# Empty jug y
if current_state.y > 0:
new_x = current_state.x
new_y = 0
new_state = JugState(new_x, new_y)
if new_state not in visited:
next_states.append((new_state, path + [current_state]))

# Pour from x to y
if current_state.x > 0 and current_state.y < y:
pour_amount = min(current_state.x, y - current_state.y)
new_x = current_state.x - pour_amount
new_y = current_state.y + pour_amount
new_state = JugState(new_x, new_y)
if new_state not in visited:
next_states.append((new_state, path + [current_state]))

# Pour from y to x
if current_state.y > 0 and current_state.x < x:
pour_amount = min(current_state.y, x - current_state.x)
new_x = current_state.x + pour_amount
new_y = current_state.y - pour_amount
new_state = JugState(new_x, new_y)
if new_state not in visited:
next_states.append((new_state, path + [current_state]))

stack.extend(next_states)

return None

if __name__ == "__main__":
x_capacity = 4 # Capacity of jug x
y_capacity = 3 # Capacity of jug y
target_amount = 2 # Desired amount to measure

solution_path = pour_jugs_dfs(x_capacity, y_capacity,


target_amount)

if solution_path:
print(f"Solution found to measure {target_amount} liters:")
for state in solution_path:
print(f"Jug X: {state.x} liters, Jug Y: {state.y} liters")
else:
print("No solution found.")

Output:

You might also like