---
tag: 33P1SJW98J
kind: example
name: The backward search for 5 liters with jugs of 4 and 6 liters
msc: ["00A35", "00A08"]
topics: [X1NSTVGDNQ]
audience: school
not_instance_of:
  - target: 3QTB9S1THS
    note: working backwards from 5 liters produces only states with an odd amount and never reaches the start, but it cannot show by itself that no plan exists; an invariant proves the impossibility
uses: [3QTB9S1THS, 8NQ190278Y, H9JT1XNDKQ]
sources:
  - url: https://en.wikipedia.org/wiki/Water_pouring_puzzle
    locator: lead section
---

## Example

With a jug of exactly $4$ liters, a jug of exactly $6$ liters and unlimited water, can we get exactly $5$ liters in one jug?
The moves are as in [[measuring 6 liters with jugs of 4 and 9 liters|CJJZX90P2M]]: fill a jug, empty a jug, or pour one jug into the other until the first is empty or the second is full.
A state is a pair $(s, \ell)$ of the amounts in the small and the large jug, and the start is $(0, 0)$.
We try [[working backwards|3QTB9S1THS]] and see where it misleads.

1. The goal is $\ell = 5$.
   A full large jug holds $6 = 5 + 1$ liters, so we could pour $1$ liter out of a full large jug.
2. A pour moves exactly $1$ liter into the small jug when the small jug holds $3$ liters, so the new goal is $s = 3$.
3. A pour from the large jug into an empty small jug stops at $4$ liters or when the large jug is empty.
   So one way to get $s = 3$ is to pour a large jug that holds $3$ liters into the empty small jug, and the new goal is $\ell = 3$.
4. To get $\ell = 3$ we could pour $3$ liters out of a full large jug, so the small jug must hold $1$ liter, and the new goal is $s = 1$.
5. To get $s = 1$, either the large jug holds $1$ liter and is poured into the empty small jug, or a full small jug pours $3$ liters into a large jug that holds $3$.
   Each new goal asks again for an odd amount.

The backward chain moves among the goals "$1$ liter", "$3$ liters" and "$5$ liters", and it never reaches the start $(0, 0)$.
So working backwards gives no plan.
It also gives no proof that no plan exists, because a cleverer backward step might have been missed.

An [[invariant|8NQ190278Y]] settles the question.

1. We claim that in every state that can be reached from $(0, 0)$, both amounts are even.
2. The start $(0, 0)$ has both amounts even.
3. Filling a jug puts $4$ or $6$ liters in it, and emptying a jug leaves $0$, all even.
4. A pour moves either all the water of the first jug, $s$ or $\ell$, or the free space of the second jug, $4 - s$ or $6 - \ell$.
   If $s$ and $\ell$ are even, each of these amounts is even, so both amounts stay even.
5. By induction on the number of moves, the claim holds in every reachable state.
6. The goal needs $5$ liters in a jug, and $5$ is odd, so no sequence of moves reaches the goal.

## Remarks

The example shows a limit of working backwards: it can find a plan, but a failure to find one proves nothing.
To show that a goal cannot be reached, we need an argument about all states at once, such as a [[parity argument|H9JT1XNDKQ]].

The invariant also explains why the backward chain kept asking for odd amounts.
A computer search over all $5 \cdot 7 = 35$ pairs $(s, \ell)$ confirms that only $10$ of them can be reached, and each has both amounts even.
In general, with jugs of $p$ and $q$ liters, the large jug can hold exactly the multiples of $\gcd(p, q)$ from $0$ up to its size.
For jugs of $4$ and $6$ liters, $\gcd(4, 6) = 2$, so the large jug can hold $0$, $2$, $4$ or $6$ liters.

## Explanations

### L1 human

Both jugs hold an even number of liters when full.
If we only fill, empty and pour, every amount stays even.
So we can never see $5$ liters, which is odd.
Working backwards from $5$ kept asking for $1$ or $3$ liters, and it never got anywhere.
But that alone did not prove anything; the even-number argument did.

### L2 human

We compare two ways to treat a goal that may be impossible.
A backward search lists the states that lead to the goal, and if it is complete and runs out of new states, it proves impossibility, but a search by hand is seldom complete.
An invariant is one property, here "both amounts are even", that holds at the start, survives each move, and fails at the goal.
For jugs of $6$ and $9$ liters and the goal of $4$ liters, the invariant "both amounts are multiples of $3$" works in the same way, since $4$ is not a multiple of $3$.

This is the typical use: when working backwards keeps returning to the same kind of goal, look for a quantity that every move preserves.
Watch out: a short backward search that fails is not a proof, because a missed move could open a path.
