State-Space Search for the 4-Gallon / 3-Gallon Water Jug Problem (Reach Exactly 2 Gallons)

State-Space Search for the 4-Gallon / 3-Gallon Water Jug Problem (Reach Exactly 2 Gallons)

Verified Sources
Nov 4, 2026

We model the classic Water Jug problem as a State Space Search task. Here, the capacities are 4 gallons and 3 gallons, with the goal: reach a state where the 4-gallon jug contains exactly 2 gallons.

Let a State be an ordered pair:

  • (x,y)(x, y) where
    • xx = water in the 4-gallon jug (0≤x≤40 \le x \le 4)
    • yy = water in the 3-gallon jug (0≤y≤30 \le y \le 3)

We consider legal actions as Production Rules that generate successor states. The transition dynamics are based on filling, emptying, and pouring without measuring markings.

Key state-goal:

  • Goal test: x=2x = 2 (i.e., (2,y)(2, y) for any yy satisfying 0≤y≤30 \le y \le 3)

Note: The above path is one valid solution sequence; the detailed production-rule application is given below.

Water Jug Problem (Typical Solution Walkthrough)

We define a Search Problem as:

  • State space: S={(x,y)∣x∈{0,1,2,3,4}, y∈{0,1,2,3}}S = \{(x,y)\mid x\in\{0,1,2,3,4\},\ y\in\{0,1,2,3\}\}
  • Start state: s0=(0,0)s_0 = (0,0)
  • Goal set: G={(2,y)∣y∈{0,1,2,3}}G = \{(2,y)\mid y\in\{0,1,2,3\}\}
  • Actions: fill a jug, empty a jug, or pour from one jug to the other until either the source is empty or the destination is full
  • Transition function: defined by the production rules below

Action set (conceptually):

  1. Fill 4-gallon jug
  2. Fill 3-gallon jug
  3. Empty 4-gallon jug
  4. Empty 3-gallon jug
  5. Pour 4 → 3
  6. Pour 3 → 4

Because capacities are small, this is a finite graph search. Algorithms like BFS/DFS/A* are applicable; here we mainly need the representation and at least one solution path.

Production Rules (Actions) for Successor Generation

  1. 1
    Step 1

    From any state (x,y)(x,y), if you choose to fill the 4-gallon jug, the successor is (4,y)(4,y). (Condition: always applicable.)

  2. 2
    Step 2

    From any state (x,y)(x,y), filling the 3-gallon jug yields successor (x,3)(x,3). (Condition: always applicable.)

  3. 3
    Step 3

    Emptying the 4-gallon jug yields successor (0,y)(0,y). (Condition: always applicable.)

  4. 4
    Step 4

    Emptying the 3-gallon jug yields successor (x,0)(x,0). (Condition: always applicable.)

  5. 5
    Step 5

    If x>0x>0 and y<3y<3, pour until either the 4-gallon jug is empty or the 3-gallon jug is full. Successor: x′=x−min⁡(x, 3−y)x' = x - \min(x,\,3-y) and y′=y+min⁡(x, 3−y)y' = y + \min(x,\,3-y). If x=0x=0 or y=3y=3, pouring yields no change (disallowed or treated as inapplicable).

  6. 6
    Step 6

    If y>0y>0 and x<4x<4, pour until either the 3-gallon jug is empty or the 4-gallon jug is full. Successor: y′=y−min⁡(y, 4−x)y' = y - \min(y,\,4-x) and x′=x+min⁡(y, 4−x)x' = x + \min(y,\,4-x). If y=0y=0 or x=4x=4, pouring yields no change (disallowed or treated as inapplicable).

Production Rules written explicitly (formal “if state then successor”)

We write each rule as a Production Rule of the form:

Rule name:
If (x,y)(x,y) satisfies the condition, then generate successor (x′,y′)(x',y') as specified.

Let Δ4→3=min⁡(x,3−y)\Delta_{4\to3}=\min(x,3-y) and Δ3→4=min⁡(y,4−x)\Delta_{3\to4}=\min(y,4-x).

  1. Fill-4

    • Condition: (always)
    • (x,y)→(4,y)(x,y)\rightarrow(4,y)
  2. Fill-3

    • Condition: (always)
    • (x,y)→(x,3)(x,y)\rightarrow(x,3)
  3. Empty-4

    • Condition: (always)
    • (x,y)→(0,y)(x,y)\rightarrow(0,y)
  4. Empty-3

    • Condition: (always)
    • (x,y)→(x,0)(x,y)\rightarrow(x,0)
  5. Pour-4-to-3

    • Condition: x>0x>0 and y<3y<3
    • (x,y)→(x−Δ4→3, y+Δ4→3)(x,y)\rightarrow(x-\Delta_{4\to3},\ y+\Delta_{4\to3})
  6. Pour-3-to-4

    • Condition: y>0y>0 and x<4x<4
    • (x,y)→(x+Δ3→4, y−Δ3→4)(x,y)\rightarrow(x+\Delta_{3\to4},\ y-\Delta_{3\to4})

Example solution (one correct action sequence)

We show a concrete path from the start (0,0)(0,0) to a goal state where the 4-gallon jug holds exactly 2 gallons.

Solution sequence (states + actions)

Target: reach (2,y)(2,y) for some yy

  1. Start: (0,0)(0,0)
  2. Fill-3: (0,0)→(0,3)(0,0)\rightarrow(0,3)
  3. Pour-3-to-4: pour from the 3-gallon into the 4-gallon
    (0,3)→(3,0)(0,3)\rightarrow(3,0)
  4. Fill-3: (3,0)→(3,3)(3,0)\rightarrow(3,3)
  5. Pour-3-to-4: (3,3)→(4,2)(3,3)\rightarrow(4,2)
    (Because 4−x=14-x=1, so only 1 gallon fits in the 4-gallon; 3−1=2 remains in 3-gallon.)
  6. Pour-4-to-3: (4,2)→(2,2)(4,2)\rightarrow(2,2)
    (Because 3−y=13-y=1, so 1 gallon is needed to fill the 3-gallon; 4−1=34-1=3? Let’s compute carefully:)
    • Here x=4x=4, y=2y=2
    • Δ4→3=min⁡(4,3−2)=min⁡(4,1)=1\Delta_{4\to3}=\min(4,3-2)=\min(4,1)=1
    • New state: (x−Δ, y+Δ)=(4−1, 2+1)=(3,3)(x-\Delta,\ y+\Delta)=(4-1,\ 2+1)=(3,3)

That reveals a correction: the state after pouring 4 → 3 from (4,2)(4,2) should be (3,3)(3,3), not (2,2)(2,2). We therefore adjust the solution to a correct one.

Corrected solution path (fully consistent arithmetic)

We use a known-valid sequence:

  1. (0,0)(0,0)
  2. Fill-4: (0,0)→(4,0)(0,0)\rightarrow(4,0)
  3. Pour-4-to-3: (4,0)→(1,3)(4,0)\rightarrow(1,3)
    • Δ4→3=min⁡(4,3−0)=3\Delta_{4\to3}=\min(4,3-0)=3 → (4−3, 0+3)=(1,3)(4-3,\ 0+3)=(1,3)
  4. Empty-3: (1,3)→(1,0)(1,3)\rightarrow(1,0)
  5. Pour-4-to-3: (not possible because y<3y<3 but x=1>0x=1>0 yes pour)
    (1,0)→(0,1)(1,0)\rightarrow(0,1)
    • Δ4→3=min⁡(1,3−0)=1\Delta_{4\to3}=\min(1,3-0)=1 → (1−1, 0+1)=(0,1)(1-1,\ 0+1)=(0,1)
  6. Fill-4: (0,1)→(4,1)(0,1)\rightarrow(4,1)
  7. Pour-4-to-3: (4,1)→(2,3)(4,1)\rightarrow(2,3)
    • Δ4→3=min⁡(4,3−1)=min⁡(4,2)=2\Delta_{4\to3}=\min(4,3-1)=\min(4,2)=2
    • (4−2, 1+2)=(2,3)(4-2,\ 1+2)=(2,3)

Now the 4-gallon jug contains exactly 2 gallons in state (2,3)(2,3), which satisfies the goal x=2x=2.

The production-rule math above explicitly shows why each successor state is correct.

Tip: Use the min() transfer amount

When pouring A→B, compute transfer as min⁡(amount in source, free capacity in destination)\min(\text{amount in source},\ \text{free capacity in destination}). Then subtract/add that amount to get (x′,y′)(x',y').

Warning: Pouring may be inapplicable

If the source jug is empty or the destination jug is already full, pouring generates no change. In state-space search, treat such actions as inapplicable to avoid self-loops.

Roadmap: From Informal Story to State-Space Search

Choose state variables

1) Identify quantities

Let state (x,y)(x,y) represent water in the 4- and 3-gallon jugs."

List actions

2) Define allowed moves

Fill, empty, and pour until a constraint stops the transfer."

Write production rules

3) Formalize transitions

Express each action as a conditional mapping from (x,y)(x,y) to (x′,y′)(x',y')."

Run BFS/DFS

4) Search for a goal

Stop when you reach any state with x=2x=2."

Visual check: state graph snippet

Below is a small portion of the graph around the solution path.

Key Concepts & FAQs

Knowledge Check

Question 1 of 4
Q1Single choice

In the state representation (x,y)(x,y) for this problem, what does xx represent?