0% found this document useful (0 votes)
9 views4 pages

Steps for Water jug problem

The document outlines the steps to solve the Water Jug Problem using a breadth-first search (BFS) approach. It includes initialization of jug capacities, a function to validate water amounts, and a method to generate possible moves between the jugs. The solution path is printed if found, or a message indicating no solution exists is displayed.

Uploaded by

guthayaswanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Steps for Water jug problem

The document outlines the steps to solve the Water Jug Problem using a breadth-first search (BFS) approach. It includes initialization of jug capacities, a function to validate water amounts, and a method to generate possible moves between the jugs. The solution path is printed if found, or a message indicating no solution exists is displayed.

Uploaded by

guthayaswanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Steps for Water jug problem

1. Initialization:

jug1_capacity = 4
jug2_capacity = 3
goal_state = (2, 0) # The target water amounts in the jugs
start_state = (0, 0) # Both jugs are empty initially

 jug1_capacity and jug2_capacity: Define the capacities of the two jugs.


 goal_state: The desired end state (2 liters in jug1 and 0 liters in jug2).
 start_state: The initial state of the jugs (both empty).

2. is_valid Function:

def is_valid(jug1_amount, jug2_amount):


return 0 <= jug1_amount <= jug1_capacity and 0 <= jug2_amount <= jug2_capacity

 Ensures that the amounts of water in the jugs are within their respective capacities.

3. Breadth-First Search (BFS) Implementation:

def solve_water_jug_problem(start_state):
visited = set()
queue = deque([(start_state, [])]) # Queue stores (state, path to state)

 visited: Keeps track of visited states to avoid revisiting them.


 queue: A deque (double-ended queue) used to perform BFS. Each element is a tuple
containing the current state and the path taken to reach it.

4. Processing Each State:

while queue:
current_state, path = queue.popleft()
if current_state == goal_state:
return path + [current_state]

 Dequeues the first element from queue.


 If the current state matches the goal_state, the solution path is returned.
5. Generating Possible Moves:

moves = [
(jug1_capacity, jug2_amount), # Fill Jug 1 to its capacity
(jug1_amount, jug2_capacity), # Fill Jug 2 to its capacity
(0, jug2_amount), # Empty Jug 1 completely
(jug1_amount, 0), # Empty Jug 2 completely
(max(0, jug1_amount - (jug2_capacity - jug2_amount)),
min(jug2_capacity, jug1_amount + jug2_amount)), # Pour Jug 1 into Jug 2
(min(jug1_capacity, jug1_amount + jug2_amount),
max(0, jug2_amount - (jug1_capacity - jug1_amount))) # Pour Jug 2 into Jug 1
]

 Lists all possible valid moves:


o Filling a jug to its capacity.
o Emptying a jug completely.
o Pouring water from one jug to another without exceeding the recipient's
capacity.

6. Adding New States to the Queue

for move in moves:


new_jug1_amount, new_jug2_amount = move
if is_valid(new_jug1_amount, new_jug2_amount) and move not in visited:
queue.append((move, path + [current_state]))

 Checks if the new state is valid and has not been visited.
 Adds the new state and the updated path to the queue.

7. Returning the Solution:

solution_path = solve_water_jug_problem(start_state)
if solution_path:
print("Solution found:")
for step in solution_path:
print(step)
else:
print("No solution exists!")

Prints the sequence of states leading to the goal state, or reports that no solution exists.
//// code for water jug problem
from collections import deque

jug1_capacity = 4
jug2_capacity = 3
goal_state = (2, 0)
start_state = (0, 0)

def is_valid(jug1_amount, jug2_amount):


return 0 <= jug1_amount <= jug1_capacity and 0 <= jug2_amount <=
jug2_capacity

def solve_water_jug_problem(start_state):
visited = set()
queue = deque([(start_state, [])])

while queue:
current_state, path = queue.popleft()
if current_state == goal_state:
return path + [current_state]

jug1_amount, jug2_amount = current_state


moves = [
(jug1_capacity, jug2_amount),
(jug1_amount, jug2_capacity),
(0, jug2_amount),
(jug1_amount, 0),
(max(0, jug1_amount - (jug2_capacity - jug2_amount)),
min(jug2_capacity, jug1_amount + jug2_amount)),
(min(jug1_capacity, jug1_amount + jug2_amount), max(0,
jug2_amount - (jug1_capacity - jug1_amount)))
]

for move in moves:


new_jug1_amount, new_jug2_amount = move
if is_valid(new_jug1_amount, new_jug2_amount) and move not
in visited:
visited.add(move)
queue.append((move, path + [current_state]))

return None

solution_path = solve_water_jug_problem(start_state)
if solution_path:
print("Solution found:")
for step in solution_path:
print(step)
else:
print("No solution exists!")

You might also like