UHG
Search
Close this search box.

What is the Water Jug Problem in AI?

The water jug problem in AI is great choice for those who wants to learn problem solving and here, I have used BFS algorithm to solve water jug problem.

Share

Table of Content

The water jug problem is often used to explain algorithms in computer science and AI. The idea is that you have two jugs without any marks and an unlimited water supply, and you have to fill a specific amount of water in one of the available jugs. Furthermore, there’s no limit on the iterations you make. 

Defining the Water Jug Problem with an example

A common example involves a 4-litre jug and a 3-litre jug, with the goal of measuring exactly 2 litres of water. To identify jugs, I gave them names and quantities of water:

  • Jug A with a capacity of 4 litres.
  • Jug B with a capacity of 3 litres.

Now, here comes the tricky part. While you have to measure exactly 2 litres of water using these jugs, it can only be achieved through the given three moves: 

  1. Fill the entire jug.
  2. Empty the entire jug.
  3. Pour water from one jug to another until one of the jugs is either full or empty.

State-Space Representation

To simplify the explanation, I’m going to use the state-space representation. With the state-space representation, we can represent each state as a pair of integers (x, y), where:

  • x is the amount of water in Jug A.
  • y is the amount of water in Jug B.

The thing to note here is the initial state is (0, 0), meaning both jugs are empty and our goal state is (2, y) or (x, 2), where either jug contains exactly 2 litres of water.

Steps to Solve The Water Jug Problem in AI using BFS

Let’s solve the problem step-by-step using the Breadth-First Search (BFS) algorithm, which explores all possible states level by level.

  1. Initial State: (0, 0) – Both jugs are empty.
  2. Fill Jug A: (4, 0) – Fill Jug A to its full capacity so Jug A has 4 litres of water.
  3. Pour from Jug A to Jug B: (1, 3) – Pour water from Jug A to Jug B until Jug B is full. Jug A now has 1 litre left.
  4. Empty Jug B: (1, 0) – Empty Jug B completely and we have 2 litre of water left in jug A.
  5. Pour from Jug A to Jug B: (0, 1) – Pour the remaining 1 litre from Jug A to Jug B. Now, Jug A is empty, and Jug B has 1 litre.
  6. Fill Jug A: (4, 1) – Fill Jug A to its full capacity again
  7. Pour from Jug A to Jug B: (2, 3) – Pour water from Jug A to Jug B until Jug B is full. Jug A now has exactly 2 litres left, which is our goal.

At this point, we have reached the goal state (2, 3). Jug A contains exactly 2 litres of water.

Solving The Water Jug Problem using Python Code

Here is a Python program which replicates the approach that was mentioned earlier. It follows all the steps and gets you the output of 6 lines suggesting what processes it went through to achieve the result:

Here’s the Output:

Conclusion:

The Water Jug Problem is a fascinating example of how AI can be used to solve real-world problems through systematic exploration of possible states and actions. 

By representing the problem in a state-space format and using search algorithms like BFS, we can find a solution efficiently. This problem not only helps in understanding basic AI concepts but also enhances problem-solving skills.

📣 Want to advertise in AIM? Book here

Related Posts
19th - 23rd Aug 2024
Generative AI Crash Course for Non-Techies
Upcoming Large format Conference
Sep 25-27, 2024 | 📍 Bangalore, India
Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.
Flagship Events
Rising 2024 | DE&I in Tech Summit
April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore
Data Engineering Summit 2024
May 30 and 31, 2024 | 📍 Bangalore, India
MachineCon USA 2024
26 July 2024 | 583 Park Avenue, New York
MachineCon GCC Summit 2024
June 28 2024 | 📍Bangalore, India
Cypher USA 2024
Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA
Cypher India 2024
September 25-27, 2024 | 📍Bangalore, India
discord icon
AI Forum for India
Our Discord Community for AI Ecosystem, In collaboration with NVIDIA.