Question
Implement the Water Jug problem in Python.
Answer :
Word Count : 1447
The Water Jug Problem is a classical problem-solving problem in Artificial Intelligence that demonstrates how a computer can find a sequence of actions to reach a desired state from an initial state. The problem consists of two jugs with known capacities and no measurement markings. The objective is to obtain a specified quantity of water by filling, emptying, and transferring water between the jugs. In a Programming and AI Lab, this problem is useful for understanding state-space representation, search algorithms, problem formulation, and implementation using Python. Consider two jugs, one having a capacity of 4 litres and another having a capacity of 3 litres. Initially, both jugs are empty. The objective is to measure exactly 2 litres of water. A state can be represented as a pair `(x, y)`, where `x` represents the amount of water in the 4-litre jug and `y` represents the amount of water in the 3-litre jug. Therefore, the initial state is `(0, 0)`. The goal is any state in which either jug contains exactly 2 litres, such as `(2, 0)`, `(2, 3)`, `(4, 2)`, or `(0, 2)`. The possible operations in the Water Jug Problem are filling either jug completely, emptying either jug completely, pouring water from one jug into the other until the source jug becomes empty or the destination jug becomes full. Each operation generates a new state. These states can be explored systematically using a search technique. A Breadth-First Search (BFS) approach is suitable because it explores states level by level and can find the shortest sequence of operations when every operation has equal cost. The following Python program implements the Water Jug Problem using BFS: ```python from collections import deque def water_jug_problem(capacity_a, capacity_b, target): initial_state = (0, 0) queue = deque() queue.append(initial_state) visited = set() visited.add(initial_state) parent = {} action = {} while queue: current = queue.popleft() a, b = current if a == target or b == target: path = [] while current != initial_state: path.append((action[current], current)) current = parent[current] path.reverse() return path next_states = [] # Fill Jug A next_states.append(("Fill Jug A", (capacity_a, b))) # Fill Jug B next_states.append(("Fill Jug B", (a, capacity_b))) # Empty Jug A next_states.append(("Empty Jug A", _____ ____ _____ ________ _____ ___ ____ ________ _____ ____ _______ _______.
________ _____ ___ __________ ________ ____.
_________ ___ ________ ________ __________.
_______ ________ _________ _____ _______.
_________ _______ __________ _______ ______ ________ _________ __________ ____ _______ ____ ____.
_________ __________ ______ __________ ___ ______ __________ _________ ___ __________ _______.
_____ ___ _____ ______ _______ _______ __________ ____ _______.
__________ _________ _________ ______ ___ ___ ___ _________ ___ _____.
_____ ________ ____ ________ ___ _______ ____.
_________ ____ _________ ______ ______ ______ ________.
__________ _________ _________ ________ ____ _________ ______ __________ ______ ________ _______.
__________ ___ ________ ________ __________ __________ ___ _____ ______.
__________ ________ ___ _________ _____ __________ _______ __________ ______.
___ ___ ________ _____ ________.
_________ _____ _______ __________ __________.
___ ________ ___ ________ ____ ______ _________ _________ _________.
___ ________ _________ _____ ____ _________ ______ ______ __________ _________ ________ _________.
______ ____ ___ __________ ___ ____ _______ _________ ______ _____ __________ _________.
_______ ________ ________ __________ ___ __________ __________ ________.
______ __________ _______ _________ ___ ________ _____ ____ _____.
________ ______ _____ _________ _________ _____.
___ ______ __________ _______ ______ ______ ____ ______ __________ ______.
___ _______ ______ ________ ____.
______ _________ ____ _______ _________ _______ __________ ______ ________ ________ ______.
____ _______ ________ __________ ___ ___ _______ ___ ______ ___ __________ ________.
_________ ______ ____ _______ _______ _________ _________ ________.
___ ________ _______ _____ ___ __________ ________ __________.
_______ _________ _______ __________ _________ ____ __________ ______.
_____ ______ ______ ______ ____ ____ __________ ____ ______ _____.
___ _______ __________ ______ _________.
_______ ___ ________ _______ _________.
_______ ____ __________ ___ _______ ________ __________ _____ ________ _________ ______ _________.
________ _______ __________ ___ __________.
__________ __________ ______ ______ _____ ______ ___ ________ ________ ________ _____.
_______ _________ _______ ______ _________ __________ ____ _____ ________ ___.
____ _______ ____ ____ ________ _____ _______.
____ ___ ____ _________ ________ _________.
_________ ______ ___ ___ ______ _____ ________ __________ _______.
_________ ________ ____ _________ _______ _________ ________ ______ ___ ___.
_________ _____ _________ _________ _______ ___ _____ ______ _____ _____ ____.
__________ ___ _____ ___ ______ _____ ___ ____ ____ ________ _________.
_______ ____ ___ _________ ____.
______ ____ _______ ______ __________ ___ _____ __________.
_____ __________ ____ _____ ______ __________ ___ _________ ______.
___ _________ ____ ____ _____ ______.
______ ____ _________ ____ ________ _______ ____ _________ ________ ___ ___ ________.
___ ___ _________ ___ ______ ______ _____ ______ ______ ________ _________ __________.
____ _______ __________ ______ _______ _____ ________ __________ _________.
_________ ___ __________ ___ __________ ___ ______ _________ ___.
________ ___ ___ ____ ______ _______ ____ ___ ____ ___ ___.
___ ____ ________ _____ _____ _______ __________.
___ _________ _________ ___ _______ _____ ___ ________ ________.
______ _____ ___ _________ _____.
_____ _________ ________ ____ ____ ______ __________ _________ _________ __________ ______.
_________ ___ _________ _________ _____.
______ ______ _______ _________ ___ ___ ______ ________ ___.
______ ____ ______ _____ ____.
______ _____ ___ __________ __________ ________.
____ __________ ________ _______ ______.
_____ _____ __________ ________ ______ _______.
_______ __________ _____ ____ _______ ______ ___ _____ _______ ________ ______ _______.
__________ _______ ______ ___ __________ _____ _______ ___ _________.
____ _____ _________ _________ ______.
_________ __________ _________ ______ _____ ___ ____ ________ ___ ________ _____.
_______ ______ ____ _______ ________.
__________ _______ ______ ______ ______ __________ _________ _____ _________ _________ ________.
_________ ____ _____ ___ ___ _________ ________ _________ __________ ___.
____ ___ __________ __________ ______ ________ _____ __________ ___.
____ ___ _____ _______ _____ ___ _____.
________ ______ ___ _______ _________ ____ __________ ______ ________.
_________ _______ ___ ____ ____ ___.
____ ________ ____ _________ __________ ______ ________ _____ _________.
______ ____ ___ _____ ____ ________ __________ _____ _____ ______ _________ _________.
__________ ___ _______ _______ _______ ___ ________ _____.
_____ _____ __________ _______ _______ _____ _______ _________ ____.
__________ _________ _______ _____ _____ _____ ____ _________ ____ ____ ____.
_______ _____ _________ _____ _______ _____ ____ _____ _________.
________ ____ ____ __________ ____ ______ ___ _______ _________ ______.
_____ ___ ____ ______ _________ ________ ________ ________ __________ _________.
_____ ______ _______ ____ __________ ________ __________ ______ ______ _____ __________.
_________ ____ _______ _______ _________ _________ ______.
________ ______ _______ ______ ______ _________ ________ _______ ____ ____.
__________ ____ _________ ____ _______ __________.
___ _____ __________ ________ _____ ___ ____ __________.
______ __________ _________ __________ _______ ______ ___ _________ _________.
________ _____ ___ _________ _____ ____ _________ _____ _____.
_____ _____ _____ __________ __________ _______.
______ ______ _____ _______ __________ __________.
____ __________ _____ ____ ______ ____ _____.
___ ___ ___ ______ ______ ______ _____.
____ _____ _____ _________ _________.
_____ _________ _________ _____ ____ __________ ___ _______ _________ _________ ___.
____ __________ ______ _________ _______ ___ _______ ________.
____ _________ ________ _________ ______ _______.
_________ __________ _______ _____ ___ ______ ________ ___ ____ ______.
_______ _______ _________ __________ _______ ____ _________.
_______ ____ ______ ________ ___ __________ ______ ___.
______ ________ ___ _______ __________ ________ _________ ____ _________ ____ ____ ________.
_______ ___ ____ ________ _______ _____ _________ _____ _______ ________.
_________ ________ _______ _____ _________ ___ _________ ___ _________.
______ _________ ______ ________ ______ _______.
____ _____ ____ ______ _______ _______.
___ _____ _______ ____ __________ _______.
______ ________ ______ ______ ________ ______ __________.
_____ _____ _________ __________ ________ _________.
___ __________ _______ _________ ____.
_____ ___ _____ ___ __________ __________ ________.
___ _______ ______ ________ _____ _____ ___ ___ __________ _____.
____ _________ ______ _____ _____ _____ _____ ____ _______ _________ __________.
___ __________ _______ ___ _____ _________.
__________ __________ __________ ___ ________ __________ _____ _____.
___ _______ ______ ________ _____ _________ ______ _______ _____.
____ _________ ________ __________ ____ ____ __________ _____ ____ ___ ____.
_____ ___ _________ ______ _________ ___.
____ __________ ________ _________ _________ _____.
____ _____ ____ ________ ______ _____ _______ ____ _________ ______.
_____ ______ ____ _____ _______ ____ _____ ____ ____ ____.
___ ____ __________ ________ _______ _____ _______ _________ ____.
_____ ___ ___ _____ _______ ______ __________ ________ ____ _________ _____.
___ _____ _____ ______ ___ _____ ________.
__________ ______ _____ ___ ______ _____ _________ ___ _______ ________.
_____ _________ _______ ________ ________.
___ _________ __________ __________ ___ ______ ___.
___ ______ ____ ___ __________ __________ _____ ____ ______ __________ ____ __________.
___ _____ ______ ________ __________ _____ ______ ___ ______ _____ _________.
________ _____ _______ _____ _______ ______ ________ _______ _________ ___.
_______ _____ ______ _________ _________ ________ _________ ________ __________ _______ ___ _______.
___ _____ _________ ____ _______ ____ ___.
________ __________ ____ ______ ______ ______ _______ __________ __________.
Get Full Answer on WhatsApp
The Water Jug Problem is a classical problem-solving problem in Artificial Intelligence that demonstrates how a computer can find a sequence of actions to reach a desired state from an initial state. The problem consists of two jugs with known capacities and no measurement markings. The objective is to obtain a specified quantity of water by filling, emptying, and transferring water between the jugs. In a Programming and AI Lab, this problem is useful for understanding state-space representation, search algorithms, problem formulation, and implementation using Python. Consider two jugs, one having a capacity of 4 litres and another having a capacity of 3 litres. Initially, both jugs are empty. The objective is to measure exactly 2 litres of water. A state can be represented as a pair `(x, y)`, where `x` represents the amount of water in the 4-litre jug and `y` represents the amount of water in the 3-litre jug. Therefore, the initial state is `(0, 0)`. The goal is any state in which either jug contains exactly 2 litres, such as `(2, 0)`, `(2, 3)`, `(4, 2)`, or `(0, 2)`. The possible operations in the Water Jug Problem are filling either jug completely, emptying either jug completely, pouring water from one jug into the other until the source jug becomes empty or the destination jug becomes full. Each operation generates a new state. These states can be explored systematically using a search technique. A Breadth-First Search (BFS) approach is suitable because it explores states level by level and can find the shortest sequence of operations when every operation has equal cost. The following Python program implements the Water Jug Problem using BFS: ```python from collections import deque def water_jug_problem(capacity_a, capacity_b, target): initial_state = (0, 0) queue = deque() queue.append(initial_state) visited = set() visited.add(initial_state) parent = {} action = {} while queue: current = queue.popleft() a, b = current if a == target or b == target: path = [] while current != initial_state: path.append((action[current], current)) current = parent[current] path.reverse() return path next_states = [] # Fill Jug A next_states.append(("Fill Jug A", (capacity_a, b))) # Fill Jug B next_states.append(("Fill Jug B", (a, capacity_b))) # Empty Jug A next_states.append(("Empty Jug A", _____ ____ _____ ________ _____ ___ ____ ________ _____ ____ _______ _______.
________ _____ ___ __________ ________ ____.
_________ ___ ________ ________ __________.
_______ ________ _________ _____ _______.
_________ _______ __________ _______ ______ ________ _________ __________ ____ _______ ____ ____.
_________ __________ ______ __________ ___ ______ __________ _________ ___ __________ _______.
_____ ___ _____ ______ _______ _______ __________ ____ _______.
__________ _________ _________ ______ ___ ___ ___ _________ ___ _____.
_____ ________ ____ ________ ___ _______ ____.
_________ ____ _________ ______ ______ ______ ________.
__________ _________ _________ ________ ____ _________ ______ __________ ______ ________ _______.
__________ ___ ________ ________ __________ __________ ___ _____ ______.
__________ ________ ___ _________ _____ __________ _______ __________ ______.
___ ___ ________ _____ ________.
_________ _____ _______ __________ __________.
___ ________ ___ ________ ____ ______ _________ _________ _________.
___ ________ _________ _____ ____ _________ ______ ______ __________ _________ ________ _________.
______ ____ ___ __________ ___ ____ _______ _________ ______ _____ __________ _________.
_______ ________ ________ __________ ___ __________ __________ ________.
______ __________ _______ _________ ___ ________ _____ ____ _____.
________ ______ _____ _________ _________ _____.
___ ______ __________ _______ ______ ______ ____ ______ __________ ______.
___ _______ ______ ________ ____.
______ _________ ____ _______ _________ _______ __________ ______ ________ ________ ______.
____ _______ ________ __________ ___ ___ _______ ___ ______ ___ __________ ________.
_________ ______ ____ _______ _______ _________ _________ ________.
___ ________ _______ _____ ___ __________ ________ __________.
_______ _________ _______ __________ _________ ____ __________ ______.
_____ ______ ______ ______ ____ ____ __________ ____ ______ _____.
___ _______ __________ ______ _________.
_______ ___ ________ _______ _________.
_______ ____ __________ ___ _______ ________ __________ _____ ________ _________ ______ _________.
________ _______ __________ ___ __________.
__________ __________ ______ ______ _____ ______ ___ ________ ________ ________ _____.
_______ _________ _______ ______ _________ __________ ____ _____ ________ ___.
____ _______ ____ ____ ________ _____ _______.
____ ___ ____ _________ ________ _________.
_________ ______ ___ ___ ______ _____ ________ __________ _______.
_________ ________ ____ _________ _______ _________ ________ ______ ___ ___.
_________ _____ _________ _________ _______ ___ _____ ______ _____ _____ ____.
__________ ___ _____ ___ ______ _____ ___ ____ ____ ________ _________.
_______ ____ ___ _________ ____.
______ ____ _______ ______ __________ ___ _____ __________.
_____ __________ ____ _____ ______ __________ ___ _________ ______.
___ _________ ____ ____ _____ ______.
______ ____ _________ ____ ________ _______ ____ _________ ________ ___ ___ ________.
___ ___ _________ ___ ______ ______ _____ ______ ______ ________ _________ __________.
____ _______ __________ ______ _______ _____ ________ __________ _________.
_________ ___ __________ ___ __________ ___ ______ _________ ___.
________ ___ ___ ____ ______ _______ ____ ___ ____ ___ ___.
___ ____ ________ _____ _____ _______ __________.
___ _________ _________ ___ _______ _____ ___ ________ ________.
______ _____ ___ _________ _____.
_____ _________ ________ ____ ____ ______ __________ _________ _________ __________ ______.
_________ ___ _________ _________ _____.
______ ______ _______ _________ ___ ___ ______ ________ ___.
______ ____ ______ _____ ____.
______ _____ ___ __________ __________ ________.
____ __________ ________ _______ ______.
_____ _____ __________ ________ ______ _______.
_______ __________ _____ ____ _______ ______ ___ _____ _______ ________ ______ _______.
__________ _______ ______ ___ __________ _____ _______ ___ _________.
____ _____ _________ _________ ______.
_________ __________ _________ ______ _____ ___ ____ ________ ___ ________ _____.
_______ ______ ____ _______ ________.
__________ _______ ______ ______ ______ __________ _________ _____ _________ _________ ________.
_________ ____ _____ ___ ___ _________ ________ _________ __________ ___.
____ ___ __________ __________ ______ ________ _____ __________ ___.
____ ___ _____ _______ _____ ___ _____.
________ ______ ___ _______ _________ ____ __________ ______ ________.
_________ _______ ___ ____ ____ ___.
____ ________ ____ _________ __________ ______ ________ _____ _________.
______ ____ ___ _____ ____ ________ __________ _____ _____ ______ _________ _________.
__________ ___ _______ _______ _______ ___ ________ _____.
_____ _____ __________ _______ _______ _____ _______ _________ ____.
__________ _________ _______ _____ _____ _____ ____ _________ ____ ____ ____.
_______ _____ _________ _____ _______ _____ ____ _____ _________.
________ ____ ____ __________ ____ ______ ___ _______ _________ ______.
_____ ___ ____ ______ _________ ________ ________ ________ __________ _________.
_____ ______ _______ ____ __________ ________ __________ ______ ______ _____ __________.
_________ ____ _______ _______ _________ _________ ______.
________ ______ _______ ______ ______ _________ ________ _______ ____ ____.
__________ ____ _________ ____ _______ __________.
___ _____ __________ ________ _____ ___ ____ __________.
______ __________ _________ __________ _______ ______ ___ _________ _________.
________ _____ ___ _________ _____ ____ _________ _____ _____.
_____ _____ _____ __________ __________ _______.
______ ______ _____ _______ __________ __________.
____ __________ _____ ____ ______ ____ _____.
___ ___ ___ ______ ______ ______ _____.
____ _____ _____ _________ _________.
_____ _________ _________ _____ ____ __________ ___ _______ _________ _________ ___.
____ __________ ______ _________ _______ ___ _______ ________.
____ _________ ________ _________ ______ _______.
_________ __________ _______ _____ ___ ______ ________ ___ ____ ______.
_______ _______ _________ __________ _______ ____ _________.
_______ ____ ______ ________ ___ __________ ______ ___.
______ ________ ___ _______ __________ ________ _________ ____ _________ ____ ____ ________.
_______ ___ ____ ________ _______ _____ _________ _____ _______ ________.
_________ ________ _______ _____ _________ ___ _________ ___ _________.
______ _________ ______ ________ ______ _______.
____ _____ ____ ______ _______ _______.
___ _____ _______ ____ __________ _______.
______ ________ ______ ______ ________ ______ __________.
_____ _____ _________ __________ ________ _________.
___ __________ _______ _________ ____.
_____ ___ _____ ___ __________ __________ ________.
___ _______ ______ ________ _____ _____ ___ ___ __________ _____.
____ _________ ______ _____ _____ _____ _____ ____ _______ _________ __________.
___ __________ _______ ___ _____ _________.
__________ __________ __________ ___ ________ __________ _____ _____.
___ _______ ______ ________ _____ _________ ______ _______ _____.
____ _________ ________ __________ ____ ____ __________ _____ ____ ___ ____.
_____ ___ _________ ______ _________ ___.
____ __________ ________ _________ _________ _____.
____ _____ ____ ________ ______ _____ _______ ____ _________ ______.
_____ ______ ____ _____ _______ ____ _____ ____ ____ ____.
___ ____ __________ ________ _______ _____ _______ _________ ____.
_____ ___ ___ _____ _______ ______ __________ ________ ____ _________ _____.
___ _____ _____ ______ ___ _____ ________.
__________ ______ _____ ___ ______ _____ _________ ___ _______ ________.
_____ _________ _______ ________ ________.
___ _________ __________ __________ ___ ______ ___.
___ ______ ____ ___ __________ __________ _____ ____ ______ __________ ____ __________.
___ _____ ______ ________ __________ _____ ______ ___ ______ _____ _________.
________ _____ _______ _____ _______ ______ ________ _______ _________ ___.
_______ _____ ______ _________ _________ ________ _________ ________ __________ _______ ___ _______.
___ _____ _________ ____ _______ ____ ___.
________ __________ ____ ______ ______ ______ _______ __________ __________.
Get Full Answer on WhatsApp
IGNOU NEWS
Assignment Submission Last Date Extended Till 30 June 2026 Click Here★★★IGNOU June 2026 TEE Date Sheet Released Click Here★★★