TL;DR: The water jug problem illustrates how AI performs a state-space search to reach a goal state from an initial state by taking actions within the given constraints. Water distribution is considered a state, and filling, emptying, and pouring are considered transitions. BFS can be used to search these states and can obtain a valid one.

The water jug problem in AI is a well-known example used to illustrate a search-based problem-solving approach. Requires two empty jugs (typically 4 and 3 liters) and an unlimited amount of water. The goal is to find exactly 2 liters in the 4-liter jug. 

Although the puzzle can be solved manually, AI approaches it differently. The problem is represented as a collection of states and actions. A search algorithm then explores possible transitions until it reaches the required goal.

What Is the Water Jug Problem in AI?

In the standard version of the water jug problem, the available resources are:

  • One 4-liter jug
  • One 3-liter jug
  • An unlimited water supply
  • No measurement markings

The allowed actions are filling a jug, emptying a jug, or pouring water from one jug into the other. Pouring continues until the source jug is empty or the destination jug is full.

The initial state is:

(0, 0)

The first value represents the amount of water in the 4-liter jug, while the second represents the amount in the 3-liter jug.

The goal is to reach any state where the 4-liter jug contains exactly 2 liters:

x = 2

For example, both (2, 0) and (2, 3) satisfy the goal.

Learn 44+ in-demand AI and machine learning skills and tools, including generative AI, prompt engineering, LLMs, and NLP, with this Microsoft AI Engineer course.

State-Space Representation and Production Rules

A state-space representation describes every possible condition the problem can reach. For this example, a state is written as:

(x, y)

Here:

  • x can range from 0 to 4.
  • y can range from 0 to 3.

Each state becomes a node in a search graph. A valid action creates an edge connecting the current state to another state. The water jug problem in AI is commonly used to demonstrate this form of state-space search.

Water Jug State Space

Production Rules

The problem requires only six general production rules:

Rule

Action

Result

1

Fill the 4-liter jug

(x, y) → (4, y)

2

Fill the 3-liter jug

(x, y) → (x, 3)

3

Empty the 4-liter jug

(x, y) → (0, y)

4

Empty the 3-liter jug

(x, y) → (x, 0)

5

Pour from the 4-liter jug into the 3-liter jug

Continue until the first jug is empty or the second is full

6

Pour from the 3-liter jug into the 4-liter jug

Continue until the second jug is empty or the first is full

For either pouring action, the amount transferred can be calculated as:

Transfer amount = min(water in source jug, space in destination jug)

This approach is clearer than defining separate rules for every possible pouring condition.

Build expertise in leading AI tools including LangChain, CrewAI, AutoGen, and Claude Code through Simplilearn's Applied Agentic AI program. Through 40+ demos, 10+ guided practices, 7 hands-on projects, and a capstone, you'll gain practical exposure to the technologies shaping the AI-native workplace.

Step-by-Step Water Jug Problem Solution

One valid path starts by filling the 3-liter jug:

Step

Action

State

1

Start with both jugs empty

(0, 0)

2

Fill the 3-liter jug

(0, 3)

3

Pour the 3 liters into the 4-liter jug

(3, 0)

4

Fill the 3-liter jug again

(3, 3)

5

Pour into the 4-liter jug until it is full

(4, 2)

6

Empty the 4-liter jug

(0, 2)

7

Pour the remaining 2 liters into the 4-liter jug

(2, 0)

The final state is (2, 0), so the 4-liter jug contains exactly 2 liters.

Water Jug Solution Path

The problem is solvable because 2 is a multiple of the greatest common divisor of the two jug capacities:

gcd(4, 3) = 1

Under the standard water jug rules, a target can be measured when it is a multiple of the greatest common divisor of the jug capacities and does not exceed the larger jug.

Solving the Water Jug Problem in AI Using BFS

Breadth-First Search, or BFS, explores the state space one level at a time. It begins with the initial state, examines every state reachable in one action, and then moves to states that require two actions.

A queue stores the states that still need to be explored. A visited set prevents the algorithm from processing the same state repeatedly.

Because each filling, emptying, or pouring operation counts as one action, the state graph can be treated as an unweighted graph. BFS therefore finds a solution requiring the fewest number of actions.

BFS vs. DFS for the Water Jug Problem in AI

BFS

DFS

Explores states level by level

Follows one path as deeply as possible

Uses a queue

Uses a stack or recursion

Finds the shortest solution by number of actions

Does not guarantee the shortest solution

May require more memory

Often requires less memory

Well suited to this problem

Can still find a valid solution

DFS can solve the puzzle, but it must track visited states to avoid repeating the same transitions. BFS is generally more useful when the shortest sequence of actions is required.

AI Engineer has been ranked as the fastest-growing role as companies move from experimenting with AI to deploying it at scale. Explore the AI Engineer roadmap that covers everything from foundational skills to senior-level responsibilities in one place.

Python Implementation of the Water Jug Problem

The following Python program uses BFS to find a path from (0, 0) to a state where the 4-liter jug contains 2 liters.

from collections import deque


def get_next_states(x, y):
    """Return every valid state reachable in one action."""

    states = {
        (4, y),  # Fill the 4-liter jug
        (x, 3),  # Fill the 3-liter jug
        (0, y),  # Empty the 4-liter jug
        (x, 0),  # Empty the 3-liter jug
    }

    # Pour from the 4-liter jug into the 3-liter jug
    transfer = min(x, 3 - y)
    states.add((x - transfer, y + transfer))

    # Pour from the 3-liter jug into the 4-liter jug
    transfer = min(y, 4 - x)
    states.add((x + transfer, y - transfer))

    return states


def solve_water_jug():
    start_state = (0, 0)
    queue = deque([(start_state, [start_state])])
    visited = {start_state}

    while queue:
        current_state, path = queue.popleft()
        x, y = current_state

        if x == 2:
            return path

        for next_state in get_next_states(x, y):
            if next_state not in visited:
                visited.add(next_state)
                queue.append((next_state, path + [next_state]))

    return None


solution = solve_water_jug()

if solution:
    for state in solution:
        print(state)
else:
    print("No solution found.")

The program generates all valid states that can be reached from the current state. It adds unexplored states to the queue and continues until x equals 2.

One possible output is:

(0, 0)
(0, 3)
(3, 0)
(3, 3)
(4, 2)
(0, 2)
(2, 0)

Conclusion

The water jug problem is an example of how a basic puzzle problem can be transformed into a structured search problem by using AI. An algorithm can systematically explore solutions rather than trial and error if it defines the initial state, goal condition, available actions, and possible transitions. These principles can also be applied to more complex applications such as planning, pathfinding, and automated decision-making. 

To develop these foundations further, explore our Microsoft AI Engineer Course. The program covers Python, machine learning, deep learning, NLP, generative AI, agentic AI, and intelligent automation through live training and hands-on projects, helping learners progress from core AI concepts to building practical AI-powered solutions.

FAQs

1. What's the smallest number of steps required to solve the 4-liter and 3-liter water jug problem?

Here are the six operations that can be performed to achieve 2 liters: Fill the 3-liter jug, pour from the 3-liter into the 4-liter jug, refill the 3-liter jug, pour from the 3-liter into the 4-liter jug until full, empty the 4-liter jug, and pour the remaining 2 liters into the 4-liter jug. BFS can verify that this is the shortest path from the initial state to the goal state (2, 0).

2. What is the search tree like in the water jug problem?

The search tree indicates the sequence of the problem from one state to another. The initial state (0, 0) is the root, and branches indicate valid actions like filling, emptying, or pouring. Search algorithms try out these branches until they arrive at a state in which 2 liters are in the 4-liter jug.

3. What are some real-world applications of the water jug problem?

The water jug problem is used primarily to learn the concept of state space search and problem-solving in AI. The same approach is used in robot path planning, solving games, scheduling workflows, and allocating resources in robot navigation. In both instances, an artificial intelligence system is provided with a set of potential actions. Then it seeks a way to traverse from its current position to a specified end state. 

Our AI & Machine Learning Program Duration and Fees

AI & Machine Learning programs typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Professional Certificate in AI and Machine Learning

Cohort Starts: 14 Aug, 2026

6 months$4,300
Microsoft AI Engineer Program

Cohort Starts: 19 Aug, 2026

6 months$2,199
Applied Generative AI and Agentic AI Specialization

Cohort Starts: 27 Aug, 2026

12 weeks$3,390
Applied Generative AI Specialization

Cohort Starts: 31 Aug, 2026

16 weeks$2,995
Oxford Programme inStrategic Analysis and Decision Making with AI

Cohort Starts: 3 Sep, 2026

12 weeks$3,390