2
$\begingroup$

“If you have a five-liter and a three-liter bottle and plenty of water, how can you get four liters of water in the five-liter bottle?”

We (all) know this problem, and its natural generalization,

“Given a p-liter and q-liter jug what amounts of water can be obtained?”

My question is different. I would like to know if there's a way to obtain the minimum number of operation to fill one between p and p jug with c liters. Only allowed operation are:

  1. Overfill a n-liter jug with n-liter
  2. Empty a jug
  3. Pour from a jug to another while the first one is empty or the other is full.

Thanks for your attention.

$\endgroup$
0

2 Answers 2

3
$\begingroup$

I have been thinking about this problem a lot lately. Given a $p$-liter and a $q$-liter with $p < q$, there is an algorithm involving $p$ and $q$ which outputs a $c$ for each set of operations inside of the algorithm.

For an example, consider a $4$-liter jug and a $9$-liter jug, where we need $6$ liters.

  1. Fill up the smaller jug. (In this case, the $4$-liter jug.)
  2. Slowly pour the smaller jug into the larger jug until either the larger jug is full or the smaller jug is empty.
  3. If the larger jug is full, empty it out and put the remaining contents of the smaller jug into the larger jug.
  4. Measure and record $c$, the number of liters in the larger jug. (In this case: $4$ liters in the $9$-liter jug.)
  5. Repeat until the desired $c$ is acquired.

Running this algorithm for this example outputs the following values for $c$: $4, 8, 3, 7, 2, 6, 1, 5, 0$. Those with a keen eye will see that these are the elements of $Z9$, the group of integers modulo 9.

In general, this algorithm will eventually produce all elements of $<p>$ inside $Zq$, the group of integers modulo $q$.

For the layperson (who knows about modular arithmetic), the possible amounts of water which can be obtained are $1 * p$ $mod$ $q$, $2 * p$ $mod$ $q$, $3 * p$ $mod$ $q$, ..., $q * p$ $mod$ $q$.

The number of operations required to find $c$ is always less than $4*q$. (This is because we have four steps to check each integer up to q, maximally).

$\endgroup$
1
$\begingroup$

By hand, this is most likely not possible. However, if you use a computer, you can use depth first search to look at all the possible first moves, possible second moves, possible third moves. At some point, you would have found the minimum number of operations required. The downfall to this method is that it has an exponential run-time.

$\endgroup$
4
  • $\begingroup$ Thank you. I'm sorry byt I can not upvote on this board. However that's exactly my goal: do it with an algorithm, and I was searching for some formal, mathematical trick in order to avoid the exponential cost. By the way do you suggest me to use a Tree of all the possible moves and then find a minimum path that leads to a successful result? $\endgroup$ Dec 2, 2013 at 20:42
  • $\begingroup$ Off the top of my head, I would use a tree. However, I would keep track of all possible combinations that have already been achieved, this way, it would reduce the number of possible combinations by a lot. $\endgroup$
    – user44322
    Dec 2, 2013 at 21:55
  • $\begingroup$ Already achieved combination is a strictly related issue to another problem. How can I build a non-indefinite tree, in principle a path from the root can never end. $\endgroup$ Dec 2, 2013 at 22:28
  • $\begingroup$ You can has at most $p$ liters in one jug and $q$ liters in another. Therefore, it will take at most $(p+1)(q+1)$ number of moves. Therefore, if you compute all possible first moves, then all possible second moves, etc. You will always terminate in a finite number of moves. Keeping track of already achieved results is just to minimize the number of branches you have to compute. $\endgroup$
    – user44322
    Dec 2, 2013 at 22:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .