{
  "aliases": null,
  "backlinks": null,
  "body": "\n## Example\n\nWith a jug of exactly $4$ liters, a jug of exactly $6$ liters and unlimited water, can we get exactly $5$ liters in one jug?\nThe 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.\nA state is a pair $(s, \\ell)$ of the amounts in the small and the large jug, and the start is $(0, 0)$.\nWe try [[working backwards|3QTB9S1THS]] and see where it misleads.\n\n1. The goal is $\\ell = 5$.\n   A full large jug holds $6 = 5 + 1$ liters, so we could pour $1$ liter out of a full large jug.\n2. A pour moves exactly $1$ liter into the small jug when the small jug holds $3$ liters, so the new goal is $s = 3$.\n3. A pour from the large jug into an empty small jug stops at $4$ liters or when the large jug is empty.\n   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$.\n4. 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$.\n5. 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$.\n   Each new goal asks again for an odd amount.\n\nThe backward chain moves among the goals \"$1$ liter\", \"$3$ liters\" and \"$5$ liters\", and it never reaches the start $(0, 0)$.\nSo working backwards gives no plan.\nIt also gives no proof that no plan exists, because a cleverer backward step might have been missed.\n\nAn [[invariant|8NQ190278Y]] settles the question.\n\n1. We claim that in every state that can be reached from $(0, 0)$, both amounts are even.\n2. The start $(0, 0)$ has both amounts even.\n3. Filling a jug puts $4$ or $6$ liters in it, and emptying a jug leaves $0$, all even.\n4. 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$.\n   If $s$ and $\\ell$ are even, each of these amounts is even, so both amounts stay even.\n5. By induction on the number of moves, the claim holds in every reachable state.\n6. The goal needs $5$ liters in a jug, and $5$ is odd, so no sequence of moves reaches the goal.\n\n## Remarks\n\nThe example shows a limit of working backwards: it can find a plan, but a failure to find one proves nothing.\nTo show that a goal cannot be reached, we need an argument about all states at once, such as a [[parity argument|H9JT1XNDKQ]].\n\nThe invariant also explains why the backward chain kept asking for odd amounts.\nA 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.\nIn 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.\nFor jugs of $4$ and $6$ liters, $\\gcd(4, 6) = 2$, so the large jug can hold $0$, $2$, $4$ or $6$ liters.\n\n## Explanations\n\n### L1 human\n\nBoth jugs hold an even number of liters when full.\nIf we only fill, empty and pour, every amount stays even.\nSo we can never see $5$ liters, which is odd.\nWorking backwards from $5$ kept asking for $1$ or $3$ liters, and it never got anywhere.\nBut that alone did not prove anything; the even-number argument did.\n\n### L2 human\n\nWe compare two ways to treat a goal that may be impossible.\nA 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.\nAn invariant is one property, here \"both amounts are even\", that holds at the start, survives each move, and fails at the goal.\nFor 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$.\n\nThis is the typical use: when working backwards keeps returning to the same kind of goal, look for a quantity that every move preserves.\nWatch out: a short backward search that fails is not a proof, because a missed move could open a path.\n",
  "commit": "35d5da104f2b90e930cd762570f4d67e50027674",
  "front": {
    "audience": "school",
    "kind": "example",
    "msc": [
      "00A35",
      "00A08"
    ],
    "name": "The backward search for 5 liters with jugs of 4 and 6 liters",
    "not_instance_of": [
      {
        "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",
        "target": "3QTB9S1THS"
      }
    ],
    "sources": [
      {
        "locator": "lead section",
        "url": "https://en.wikipedia.org/wiki/Water_pouring_puzzle"
      }
    ],
    "tag": "33P1SJW98J",
    "topics": [
      "X1NSTVGDNQ"
    ],
    "uses": [
      "3QTB9S1THS",
      "8NQ190278Y",
      "H9JT1XNDKQ"
    ]
  },
  "kind": "example",
  "link": "https://math2.tamnd.com/node/33P1SJW98J/",
  "links": [
    {
      "field": "not_instance_of",
      "target": "3QTB9S1THS",
      "note": "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."
    },
    {
      "field": "topics",
      "target": "X1NSTVGDNQ"
    },
    {
      "field": "uses",
      "target": "3QTB9S1THS"
    },
    {
      "field": "uses",
      "target": "8NQ190278Y"
    },
    {
      "field": "uses",
      "target": "H9JT1XNDKQ"
    }
  ],
  "markdown": "https://math2.tamnd.com/node/33P1SJW98J.md",
  "msc": [
    "00A35",
    "00A08"
  ],
  "name": "The backward search for 5 liters with jugs of 4 and 6 liters",
  "prov": {
    "log": "https://github.com/tamnd/math2/blob/main/prov/33P/33P1SJW98J.jsonl",
    "quarantine": null,
    "unchecked": null
  },
  "role": "",
  "source": "https://github.com/tamnd/math2/blob/main/nodes/33P/33P1SJW98J.md",
  "status": null,
  "tag": "33P1SJW98J",
  "uri": "m2://33P1SJW98J",
  "url": "https://math2.tamnd.com/wiki/the-backward-search-for-5-liters-with-jugs-of-4-and-6-liters/",
  "writing": {
    "tag": "33P1SJW98J",
    "kind": "example",
    "level": "W0",
    "next": [
      {
        "what": "an inventory row",
        "note": "the row of the inventory that the node accounts for",
        "have": 0,
        "want": 1
      }
    ]
  }
}
