25

Consider the following water-pouring puzzle. Given $n$ initially empty jugs with maximum volumes $V_1, V_2,\dots ,V_n$, and the following three actions on a jug:

  1. A jug is filled completely.
  2. A jug is emptied completely.
  3. The contents of a jug are poured into another jug until it is completely full, leaving any excess in the original jug.

Is the closed form for the total number of possible configurations of $(V_{\rm jug\,1},V_{\rm jug\,2},\dots,V_{{\rm jug}\,n})$ given by $$\prod_{i=1}^{n}\left(\frac{V_i}{\gcd(V_1,\dots,V_n)}+1\right)-\prod_{i=1}^{n}\left(\frac{V_i}{\gcd(V_1,\dots,V_n)}-1\right)?$$

Example: Suppose we have two jugs with specified volumes, $V_1=4, V_2=3$. Then the possible permutations of $(V_1, V_2)$ are $(0,0),(0,3),(3,0),(3,3),(4,0),(1,3),(1,0),(0,1),(4,1),(2,3)$, $(2,0),(0,2),(4,2),(4,3)$ i.e. $14$ tuples, agreeing with $(4+1)(3+1)-(4-1)(3-1)=14$, since $\gcd(4,3)=1$.


By considering the expansion of the monic polynomial $\prod\limits_{i=1}^n(\lambda-V_i)$ and substituting $\lambda=\pm1$, we can also express this in terms of the elementary symmetric polynomial as the following $$2\sum_{k=1}^{n/2}\frac{e_{2k-1}(V_1,\dots ,V_n)}{\gcd(V_1,\dots ,V_n)^{2k-1}}\quad\text{for $n$ even and }2+2\sum_{k=1}^{(n-1)/2}\frac{e_{2k}(V_1,\dots ,V_n)}{\gcd(V_1,\dots ,V_n)^{2k}}\quad\text{for $n$ odd}, $$ for which I credit @TheSimpliFire for finding all the above general expressions.

For example:

  • $n=2$ (the $2$ jug case): the number of possible permutations for $2$ jugs of maximum volume $V_1,V_2$ is $2(V_1+V_2)/\gcd(V_1,V_2)$; or
  • $n=3$ (the $3$ jug case): with maximum volumes $V_1,V_2,V_3$, this has a total number of possible permutations of $2+2(V_1V_2+V_1V_3+V_2V_3)/\gcd(V_1,V_2,V_3)^2$.

However, one can simplify the problem by assuming WLOG that $V_1,V_2,\dots ,V_n$ are all coprime to each other and asking whether the closed form for the total number of possible combinations is given by $$\prod_{i=1}^{n} (V_i+1) - \prod_{i=1}^{n}(V_i-1)$$ since suppose for example that $\gcd(V_1,\dots ,V_n)=3$, then we cannot make a jug have a volume that is not a multiple of $3$, giving the same number of possible combinations as if we began with an initial step of scaling all the maximum volumes down by a factor of $3$ such that all the maximum volumes would now be coprime.


Since writing this question, I have uploaded a Python script of mine, which is able to generate a table of the number of possible states for up to $4$ jugs, to GitHub and which was used to greatly assist in determining the formula in the question. I have linked it here in the hope of aiding anyone attempting to determine the number of states without the need for tedious by-hand computations. The code can likely be optimised massively, and I would be interested if anyone knows how to make it better.

The context for this problem is due to a computer science coursework question of a friend of mine which asked to write a Java program using the DFS algorithm to find the number of possible states for the $V_1=8,V_2=5,V_3=3$ case, however, I wanted to think of the problem more mathematically and abstractly and out of my own interest tried to generalise it and find a general formula. Note that there are $160$ possible states with $V_1=8,V_2=5,V_3=3$ for which the above formulae agree.

KStarGamer
  • 5,089
  • 1
  • 18
  • 50
  • 8
    [+1] Very interesting. – Jean Marie Dec 07 '22 at 16:52
  • 1
    Hints: 1) At any step, there must a jar empty or full, as a result of one of the three actions, therefore your formula is an upper bound of the number of configurations ($\prod_{i=1}^n (V_i+1-2)$ are the subtracted configurations because invalid). 2) It remains to show that any configuration with an empty or full jar can be obtained with a sequence of the given actions. Starting from $(V_1,V_2)$ you go to $(V_1-qV_2,V_2)$, $V_1-qV_2<V_2$, then $(V_1,V_1-qV_2)$ then $(V_1,sV_1-(q+s)V_2)$ then $(sV_1-(q+s)V_2,V_2)$ then $(sV_1-(q+s+t)V_2,V_2)$. – Fabius Wiesner Dec 08 '22 at 22:11
  • 1
    I think it should be possible to get the desired coefficients for $V_1$ and $V_2$ for the Bezout identity and thus get $1$ and any other value of a valid configuration. – Fabius Wiesner Dec 08 '22 at 22:15
  • 1
    More Generally this is Standard Chicken Nuggent Theorem or something kinda related to Stardard trick of that problem(I may be wrong in naming them but Something like that is there in Justin Steven look at it). –  Dec 10 '22 at 10:46
  • 1
    A relevant Wikipedia page dedicated to this problem, although there it is not possible to empty or fill a jar on its own. – Fabius Wiesner Dec 11 '22 at 16:14
  • 1
    @BillyJoe I am aware of this similar problem, but it’s not quite the same; it asks for the number of moves necessary to reach some target state from some initial state, whereas my problem counts all possible states not just the ones needed to reach some target state. – KStarGamer Dec 11 '22 at 16:24
  • 1
    Yes but maybe the methods there can be used to prove that all the configurations counted by your formula can be reached with a sequence of moves. To prove your formula we need to show that any state with one empty or full jar have a sequence of moves that reach it. – Fabius Wiesner Dec 11 '22 at 16:31
  • 2
    See also https://mathoverflow.net/questions/379098/what-is-known-in-general-about-the-liquid-transfer-problem and https://math.stackexchange.com/questions/1178368/worst-case-in-decanting-puzzles-pouring-water-from-one-jug-to-others and https://puzzling.stackexchange.com/questions/8252/a-general-solution-to-the-decanting-problem-aka-jug-pouring-water-pouring – Gerry Myerson Dec 13 '22 at 11:35

3 Answers3

5

I had an earlier answer which had a huge error. This new answer should be completely correct.


Let $g=\gcd(V_1,\dots, V_n)$. I will use $\newcommand\per{\,\%\,}\%$ to denote the modulo operation, so $x \per V$ is the unique integer in $\{0,1,\dots,V-1\}$ which is congruent to $x\pmod V$.

A configuration is a vector $(x_1,\dots,x_n)$, where $x_i$ is the amount of water in jug $i$, so $0\le x_i \le V_i$. Call a configuration valid if every jug has a multiple of $g$ units of water, and at least jug is empty or full. In order to prove that the number of reachable configurations is $\prod_{i=1}^n (V_i/g+1)-\prod_{i=1}^n (V_i/g-1)$, it suffices that every valid configuration is reachable.

Let $(y_1,\dots,y_n)$ be the valid target configuration. We can assume that each $y_i<V_i$, because it is trivial to convert between empty and full. Without loss of generality, we assume $y_n=0$. In this solution, jug $n$ will generally remain empty, to be available for "storage".

Here are the methods my solution uses. Below, $s$ and $t$ are indicies of jugs, where $s,t\in \{1,\dots,n-1\}$.

  • Storage: If jug $s$ is nonzero, then storing $s$ consists of pouring from $s$ into jug $n$ until $s$ is empty, possibly emptying $n$ to make space as many times as necessary. At the end, if jug $s$ started with $x_s$ liters, then jug $n$ will have $x_s\per V_n$ liters, while $s$ will be empty.

    As long as jug $s$ is empty later, and jug $n$ is never touched, then storage is reversible. By reversing the pourings, you can make it so jug $s$ has $x_s$ liters again, and jug $n$ is empty again.

    For example, if jug $s$ has $19$ liters of water, and $V_n=4$, then storing $s$ involves pouring from jug $s$ into jug $n$ five times, where the last pour puts $3$ liters in jug $n$. To undo, you pour those $3$ liters from $n$ back into $s$, then fill $n$ and dump into $s$ four times, so $s$ returns to $19$.

  • Addition: The addition operation takes two jugs, the source $s$ and the target $t$. This operation only affects the target jug. If jug $t$ starts with $x_t$ units, then at the end jug $t$ will have $$ (x_t + V_s)\per V_t $$ To accomplish this, there are two cases:

    1. If jug $s$ is initially empty, this is accomplished by filling jug $s$, and then pouring from jug $s$ into jug $t$ until jug $s$ is empty, emptying jug $t$ to make room if necessary.

    2. If jug $s$ is initially non-empty, you first store jug $s$, then perform the steps described in 1. Afterwards, reverse the storage procedure so jug $s$ returns to its initial value.

Once we have the addition operation, solving the puzzle is a piece of cake. For each jug, $i$, with target value $y_i\in \{0,1,\dots,V_i-1\}$, such that $y_i$ is a multiple of $\gcd(V_1,\dots,V_n)$, we know from Bézout's Lemma that we can write $$ y_i = c_1 V_1+\dots+c_iV_i+\dots+c_nV_n,\tag1 $$ for some integers $c_1,\dots,c_n$. Looking at this equation modulo $V_i$, we see $$ y_i\equiv \tilde c_1 V_1+\dots+\tilde c_{i-1} V_{i-1}+\tilde c_{i+1} V_{i+1}+\dots+\tilde c_n V_n\pmod {V_i},\tag2 $$ where each $\tilde c_j=c_j\per V_i$.

Solution: For each $i\in \{1,\dots,n-1\}$, and for each $j\in \{1,\dots,n\}$ such that $j\neq i$, perform the addition operation with source $j$ and target $i$, a total of $\tilde c_j$ times.

If we let $w_i$ be the final contents of jug $i$ after performing the claimed solution, then $(2)$ implies that $w_i\equiv y_i\pmod{V_i}$. Since both $w_i,y_i\in \{0,\dots,V_i-1\}$, this further implies $w_i=y_i$, so we have reached the target state.

Mike Earnest
  • 75,930
1

Note: I know the OP asks for a closed form formula. This answer doesn't provide it, but gives an algorithm to compute the value. So, this may be considered as a partial answer that others may build upon.

If the set $V = \{V_i\}$ form a perfect difference set modulo $q$, then every non-zero residue modulo $q$ can be constructed as a difference of elements $v_x, v_y \in V$. In the general case, $V$ is not a p.d. set. and may contain duplicates (i.e., multiple jugs with same volume).

To keep things simple, let us assume that the $V_i$ are distinct.

Note: The calculation becomes more complicated if the $V_i$ are not distinct.

Let

$$ \psi = \text{the power set of $V$}, \\ \Sigma = \bigg\{\sum r : r\in \psi \bigg\},\\ \Delta = \{\sigma_i - \sigma_j : \sigma_i \gt \sigma_j, \sigma_i, \sigma_j \in \Sigma \} \cup \{0\} \cup {V}. $$

Then, the possible volumes in the $i$-th jug is given by:

$$v_i =\{\delta_i : \delta_i \le V_i, \delta_i \in \Delta \}.$$

Then, the possible configurations are given by:

$$ \prod_{i=1}^n \#v_i, \text{ where $\#v_i$ is the cardinality of set $v_i$ } $$

Example: Let the jug volumes be $V = \{1, 3, 4, 5\}$.

We have

$$ \begin{align} \psi & = \{ \\ & \{1\}, \{3\}, \{4\}, \{5\}, \\ & \{1,3\}, \{1,4\}, \{1,5\}, \{3,4\}, \{3,5\}, \{4,5\}, \\ & \{1,3,4\}, \{1,3,5\}, \{1,4,5\}, \{3,4,5\}, \\ & \{1,3,4,5\}, \emptyset \\ \}, \\ \Sigma &= \{ \\ & 1, 3, 4, 5, \\ & \color{red}{4}, \color{red}{5}, 6, 7, 8, 9 \\ & \color{red}{8}, \color{red}{9}, 10, 12 \\ & 13, 0 \\ \} & = \{0,1,3,4,5,6,7,8,9,10,12,13\}, \\ \Delta &= \{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 \}, \\ v_i &= \{ \\ & \{0,1\}, \\ & \{0,1,2,3\}, \\ & \{0,1,2,3,4\}, \\ & \{0,1,2,3,4,5\}, \\ \} \end{align} $$

Therefore, $$\prod_{i=1}^4 \#v_i = 2 \cdot 4 \cdot 5 \cdot 6 = 240$$.

Note: The use of a powerset makes this an exponential time algorithm.

vvg
  • 3,311
0

This is only a partial answer / long comment.

I will consider the simplified version of the problem with $\gcd(V_1,\ldots,V_n)=1$.

Obviously, each jug $i$ can be in $V_i+1$ states: all integers from $0$ (empty jug) to $V_i$ (full jug). Therefore the maximum number of configurations for the set of all the jars is $\prod_{i=1}^n (V_i+1)$. However, due to the given rules, as a result of any step, including the initial condition, there must be at least one empty or full jar. Therefore we need to subtract the configurations without any empty or full jar, which are $\prod_{i=1}^n (V_i+1-2)$, where $-2$ is for the empty and full condition. Therefore the maximum number of configurations is $\prod_{i=1}^n (V_i+1)-\prod_{i=1}^n (V_i-1)$.

We need now to show that all the configuration counted by your formula, i.e. all the configurations with at least one empty or full jar, can be reached with some moves. You already saw that a necessary condition for a configuration to be reached is that the volumes be multiples of $\gcd(V_1,\ldots,V_n)$. This is also a sufficient condition, provided that there is at least one empty or full jar, and since $\gcd(V_1,\ldots,V_n)=1$, all integer volumes are possible.

[Note also that it is possible to work with two jars at a time, one of which is fixed and is the jar that will remain empty or full at the end. So if the empty or full jar is $V_j$, we will work on the $n$ couples $(V_j,V_i)$, $1 \le i \le n$, $i \not = j$ in sequence.] As pointed out by Mike Earnest, this is wrong and thus confutes all the proof. Maybe this can be used to fix the proof.

Regarding to a general proof that with two jugs any configuration, with one of them empty or full, can be obtained if volumes are a multiple of $\gcd(V_1,\ldots,V_n)$, you can find it in this interesting article by Chavdar Lalov, a fourteen or fifteen years old High School student at the time of writing: A New Approach For Solving The Generalized 2 Jugs Decanting Problem, Parabola Volume 52, Issue 1 (2016).

Other references for further reading on related (but not the same) problems can be found on the Wikipedia page Water pouring puzzle.

  • 1
    I am unclear how your solution works for $n\ge 3$. Say the volumes are $6,10,$ and $15$, and the goal is to get $0$ in the six jug and $1$ in the ten and fifteen jugs. You seem to be saying that you use the $6$ jug and $10$ jug alone to put one unit in the $10$ jug, but this is impossible, as $\gcd(6,10)=2$. – Mike Earnest Dec 11 '22 at 18:14
  • 1
    Oh well, right... I made it too simple. I turned my answer into a long comment. – Fabius Wiesner Dec 11 '22 at 18:46
  • 1
    I think I solved it! Mind checking it out? – Mike Earnest Dec 13 '22 at 03:05