While reading through some lecture notes on preliminary number theory, I came across the solution to
water jug problem (with two jugs) which is summed as thus:
Using the property of the G.C.D of two numbers that GCD(a,b) is the smallest possible linear combination of a and b, and hence a certain quantity Q is only measurable by the 2 jugs, iff Q is a n*GCD(a,b), since Q=sA + tB, where:
n = a positive integer
A = capacity of jug A
B= capacity of jug B
And, then the method to the solution is discussed
Another model of the solution is to model the various states as a state-space search problem as often resorted to in Artificial Intelligence.
My question is: What other known methods exist which models the solution, and how? Google didn’t throw up much.
5
Answers
This type of problem is often amenable to dynamic programming techniques. I’ve ofetn seen this specific problem used as an example in operations research courses. One nice step-by-step description is here.
An amazing and amusing approach (for 3 jugs) is through barycentric coordinates (really!), as described at the always brilliant website Cut-the-Knot: Barycentric coordinates: A Curious Application.
The search space method is what I would’ve suggested. I made a program to solve generic water jugs problems using a BFS. Could send it to you if you wish.
Strictly for 2 Jug Problem
Q = Gallons you need.
Note: The Q must be a multiple of Gcd(A,B) else there is no solution. If Gcd(A,B) == 1, There is a solution for Any Q.
1) Method 1 :
Extended Euclid’s Algorithm will solve it faster than any Graph Algorithm.
2) Method 2:
Here’s a Naive Approach. (note, this can throw 2 solutions, You’ll have to choose which is shorter)
The Problem in question can be simply solved by
repeatedly
Fill from one bucket A to another bucket B (order doesnt matter) until it fills up with the amount you want…ofcoz, when a bucket fillsup, you empty it and continue.Repeatedly Fill A->B
Lets try and observe what happens if we go the other way round,
Fill B->A
In this case filling B->A gives us the goal state faster than A->B
Generic N Jugs
Here’s an interesting paper
I encountered this problem during one of my studies. As you, and as st0le said here, I found as answer to the problem the Extended Euclide’s algorithm. But this answer do not satisfied me, because I think it’s a quantitative answer, not a qualitative one (that is, the algorithm does not say what step to take to reach the result).
I think I found a different solution to the problem that always reach the result with the minimum number of steps.
Here it is:
Choose the service jug (that is, the one you will refill with the pump). Supposing A > B (you can easily find which jug is the bigger one):
start the filling process:
fill with the pump the service jug (if empty)
fill the other jug using the service one
check fullness of the other jug and, in case, empty it
stop when the bigger jug contains Q
Below you find a very naive implementation of the algorithm in c++. Feel free to reuse it, or improve it as you need.
I don’t know if there is any mathematical concept behind the process I described to choose the right jug to minimize the number of needed steps, I use it as an heuristic.
I hope this can help you.