State-Space Search for the 4-Gallon / 3-Gallon Water Jug Problem (Reach Exactly 2 Gallons)
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:
- where
- = water in the 4-gallon jug ()
- = water in the 3-gallon jug ()
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: (i.e., for any satisfying )
Note: The above path is one valid solution sequence; the detailed production-rule application is given below.
Water Jug Problem (Typical Solution Walkthrough)
Problem formulation in State Space Search
We define a Search Problem as:
- State space:
- Start state:
- Goal set:
- 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):
- Fill 4-gallon jug
- Fill 3-gallon jug
- Empty 4-gallon jug
- Empty 3-gallon jug
- Pour 4 → 3
- 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
- 1Step 1
From any state , if you choose to fill the 4-gallon jug, the successor is . (Condition: always applicable.)
- 2Step 2
From any state , filling the 3-gallon jug yields successor . (Condition: always applicable.)
- 3Step 3
Emptying the 4-gallon jug yields successor . (Condition: always applicable.)
- 4Step 4
Emptying the 3-gallon jug yields successor . (Condition: always applicable.)
- 5Step 5
If and , pour until either the 4-gallon jug is empty or the 3-gallon jug is full. Successor: and . If or , pouring yields no change (disallowed or treated as inapplicable).
- 6Step 6
If and , pour until either the 3-gallon jug is empty or the 4-gallon jug is full. Successor: and . If or , 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 satisfies the condition, then generate successor as specified.
Let and .
-
Fill-4
- Condition: (always)
-
Fill-3
- Condition: (always)
-
Empty-4
- Condition: (always)
-
Empty-3
- Condition: (always)
-
Pour-4-to-3
- Condition: and
-
Pour-3-to-4
- Condition: and
Example solution (one correct action sequence)
We show a concrete path from the start to a goal state where the 4-gallon jug holds exactly 2 gallons.
Solution sequence (states + actions)
Target: reach for some
- Start:
- Fill-3:
- Pour-3-to-4: pour from the 3-gallon into the 4-gallon
- Fill-3:
- Pour-3-to-4:
(Because , so only 1 gallon fits in the 4-gallon; 3−1=2 remains in 3-gallon.) - Pour-4-to-3:
(Because , so 1 gallon is needed to fill the 3-gallon; ? Let’s compute carefully:)- Here ,
- New state:
That reveals a correction: the state after pouring 4 → 3 from should be , not . We therefore adjust the solution to a correct one.
Corrected solution path (fully consistent arithmetic)
We use a known-valid sequence:
- Fill-4:
- Pour-4-to-3:
- →
- Empty-3:
- Pour-4-to-3: (not possible because but yes pour)
- →
- Fill-4:
- Pour-4-to-3:
Now the 4-gallon jug contains exactly 2 gallons in state , which satisfies the goal .
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 . Then subtract/add that amount to get .
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 quantitiesLet state represent water in the 4- and 3-gallon jugs."
List actions
2) Define allowed movesFill, empty, and pour until a constraint stops the transfer."
Write production rules
3) Formalize transitionsExpress each action as a conditional mapping from to ."
Run BFS/DFS
4) Search for a goalStop when you reach any state with ."
Visual check: state graph snippet
Below is a small portion of the graph around the solution path.
Key Concepts & FAQs
Knowledge Check
In the state representation for this problem, what does represent?