Download On Optimal Solution of the General Two Jugs Problem

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

History of algebra wikipedia , lookup

System of polynomial equations wikipedia , lookup

Polynomial greatest common divisor wikipedia , lookup

Equation wikipedia , lookup

System of linear equations wikipedia , lookup

Boolean satisfiability problem wikipedia , lookup

Factorization of polynomials over finite fields wikipedia , lookup

Transcript
Advances in Theoretical and Applied Mathematics
ISSN 0973-4554 Volume 11, Number 2 (2016), pp. 137-144
© Research India Publications
http://www.ripublication.com
On Optimal Solution of the General Two Jugs
Problem
Yiu-Kwong Man
Department of Mathematics and Information Technology,
The Education University of Hong Kong, Hong Kong.
E-mail address: [email protected]
Abstract
The two jugs problem is a classic problem in recreational mathematics,
computer sciences, artificial intelligence, problem solving, discrete
mathematics and cognitive psychology. Depending on which jug is chosen to
fill first, there exist two possible ways to solve the problem for each
measurable amount. In this paper, we describe decision theorems for
determining the optimal solution of the general two jugs problem. Some
examples on applying the results are provided.
AMS Classification: 11A07, 68W1, 97F60
Key Words: Jugs Problem, Diophantine Equations, Optimal Solution.
INTRODUCTION
The general two jugs problem can be described like this:
“Let m, n and d be positive integers. Given a m-liter jug and a n-liter jug, where
0<m<n. The jugs do not have markings to measure the smaller quantities. How can
you measure d (<n) liters of water by the jugs?”
It is a classic problem in recreational mathematics, computer sciences, artificial
intelligence, problem-solving, discrete mathematics and cognitive psychology, etc.
Various approaches were proposed to tackle this problem for specified values of m, n
and d, such as the working backwards approach [1], the geometric approach [2-4], the
diagraph approach [5], the search approach [6-9], the cognitive approach [10] and the
use of heuristics [11-14]. However, the solutions obtained may not be optimal, in the
sense that the number of pouring steps involved is minimal. Depending on which jug is
chosen to fill first, there exist two possible ways to solve the problem. In [15], two
methods were proposed to compute the sequence of water amounts (denoted by V)
138
Yiu-Kwong Man
inside the jugs by simple additions and subtractions only, and then the actual pouring
sequence can be determined by referring to the computed values of V. The application
of the methods to find the optimal solution of the problem by using the Euclidean norm
was introduced in [16]. Further extension and modifications of the methods to compute
the integer sequence of all measurable amounts were described in [17]. In this paper,
we describe decision theorems for determining the optimal solution of the general two
jugs problem. Some examples on applying this important result are provided.
TWO EXISTING ALGORITHMS AND RELATED RESULTS
The general two jugs problem can be modeled by the Diophantine equation mx+ny=d,
which is called the associated Diophantine equation of the problem. The solvability of
this equation is determined by the well-known theorem below (see [19], [20]).
Theorem 2.1
The Diophantine equation mx+ny=d is solvable if and only if gcd(m, n) divides d.
An algorithmic approach to solve the general two jugs problem was introduced in [17],
which can be used to compute all measurable amounts (d) by the given jugs.
Depending on which jug to fill first, there are two algorithmic approaches to compute
all the measurable amounts, which are described below:
Algorithm 2.2
Input: Integers m and n, where 0<m<n.
Output: An integer sequence (called S1) of all distinct measurable amounts (< m + n)
obtained by the jugs by filling the m-litre jug first.
Procedure:
Step 1. Initialize a dummy variable k = 0.
Step 2. If k < n, then repeat adding m to k and assign the result to k until k > n.
Step 3. If k n, then subtract n from k and assign the result to k.
Step 4. If k = 0, then stop and return the sequence of the values of k obtained.
Otherwise, repeat the steps 2-4.
The number of additions (say x1) and subtractions (say y1) involved provide a solution
to the Diophantine equation mx+ny=d, namely x = x1, y = -y1. The actual pouring
sequence can be easily determined by referring to the integer sequence S1 obtained.
Algorithm 2.3
Input: Integers m and n, where 0<m<n.
Output: An integer sequence (called S2) of all distinct measurable amounts (< m + n)
obtained by the jugs by filling the n-litre jug first.
Procedure:
Step 1. Initialize a dummy variable k = 0.
Step 2. If k 0, then add n to k and assign the result to k.
Step 3. If k m, then repeat subtracting m from k and assign the result to k until k =0
or k < m.
On Optimal Solution of the General Two Jugs Problem
139
Step 4. If k = 0, then stop and return the sequence of the values of k obtained.
Otherwise, repeat the steps 2-4.
The number of subtractions (say x2) and additions (say y2) involved in Algorithm 2.3
provide a solution to the Diophantine equation mx+ny=d, namely x = -x2, y = y2.
Again, the actual pouring sequence can be easily determined by referring to the
integer sequence S2.
There are some important results related to the two jugs problems, whose proofs can
be found in [17] and [18].
Theorem 2.4
The total number of distinct measurable amounts (< m+n) by the m- and n-litre jugs is
equal to (m + n)/k, where k =gcd (m, n).
Theorem 2.5
The integer sequence obtained by Algorithm 2.2 is the same as that obtained by
Algorithm 2.3 in reverse order. The additions and subtractions involved in Algorithm
2.2 can be reversed to become subtractions and additions involved in Algorithm 2.3.
Theorem 2.6
Let k =gcd(m, n). If (m+n)/2 is an integer and k divides (m+n)/2, then ((k-n)/2k,
(m+k)/2k) satisfies the Diophantine equation mx+ny=(m+n)/2, which is the optimal
solution of the corresponding two jugs problem obtained by applying Algorithm 2.3.
MAIN RESULTS
Here are the main results of this paper.
Theorem 3.1
For a measurable amount d (< n), if (x0, y0) is a solution of the Diophantine equation
mx+ny=d obtained by applying Algorithm 2.2, then (x0 – n/k, y0 + m/k) is the
corresponding solution of the same equation obtained by applying Algorithm 2.3,
where k = gcd (m, n).
Proof.
Since m(n/k) – n(m/k)=0, so the total number of additions and subtractions involved
before the termination of Algorithm 2.2 are n/k and m/k, respectively. As mx0+ny0=d,
so the additional number of additions (say x') and subtractions (say y') required to
reduce d to 0 satisfy the equations: x'= n/k –x0 and y' = -(m/k – (-y0)) = -(y0 + m/k). By
Theorem 2.5, (-x', - y') =(x0 – n/k, y0 + m/k) is the solution of mx+ny=d obtained by
applying Algorithm 2.3. It completes the proof.
Theorem 3.2
For a measurable amount d (< n), if (x0, y0) is a solution of the Diophantine equation
mx+ny=d obtained by applying Algorithm 2.3, then (x0 + n/k, y0 – m/k) is the
140
Yiu-Kwong Man
corresponding solution of the same equation obtained by applying Algorithm 2.2,
where k = gcd(m, n).
Proof.
Since m(-n/k) + n(m/k)=0, so the total number of additions and subtractions involved
before the termination of Algorithm 2.3 are m/k and n/k, respectively. As mx0+ny0=d,
so the additional number of additions (say y') and subtractions (say x') required to
reduce d to 0 satisfy the equations: y'= m/k –y0 and x' = -(n/k – (-x0)) = -(x0 + n/k). By
Theorem 2.5, (-x', - y') =(x0 + n/k, y0 – m/k) is the solution of mx+ny=d obtained by
applying Algorithm 2.2. It completes the proof.
Theorem 3.3
For a measurable amount d (< n), let (x0, y0) be a solution of the Diophantine equation
mx+ny=d obtained by applying Algorithm 2.2. If (x0 – y0) < (m+n)/2k, then (x0, y0) is
the optimal solution of the corresponding two jugs problem. Otherwise, (x0 – n/k, y0 +
m/k) is the optimal solution of the problem obtained by applying Algorithm 2.3,
where k = gcd(m, n).
Proof.
Since x0 and -y0 refer to the number of additions and subtractions involved for
obtaining the amount d by applying Algorithm 2.2, so x0 – y0 represents the total
number of arithmetic operations involved. Similarly, x0 – n/k and y0 + m/k refer to the
number of subtractions and additions involved for obtaining the amount d by applying
Algorithm 2.3, so (y0 + m/k) – (x0 – n/k) = (m+n)/k + y0 – x0 represents the total
number of arithmetic operations involved. Now, there are three cases to be considered.
Case (i): If x0 – y0 < (m+n)/2k, then it implies that (y0 + m/k) – (x0 – n/k) > x0 – y0, so
there is less arithmetic operations required to obtain (x0, y0) than (x0 – n/k, y0 + m/k),
so (x0, y0) is the optimal solution of the corresponding two jugs problem. Case (ii): If
x0 – y0 > (m+n)/2k, then it implies that (y0 + m/k) – (x0 – n/k) < x0 – y0, so there is more
arithmetic operations required to obtain (x0, y0) than (x0 – n/k, y0 + m/k), so (x0 – n/k,
y0 + m/k) is the optimal solution of the corresponding two jugs problem. Case (iii): If
x0 – y0 = (m+n)/2k, then y0 + m/k – (x0 – n/k) = (m+n)/2k. As x0 – y0 is an integer, so
(m+n)/2k is also an integer. Since k = gcd(m, n) divides m+n, so (m+n)/2 is an integer.
By Theorem 2.6, (x0 – n/k, y0 + m/k) is equal to ((k-n)/2k, (m+k)/2k), which satisfies
the Diophantine equation mx+ny=(m+n)/2 and is the optimal solution of the
corresponding two jugs problem. It completes the proof.
Theorem 3.4
For a measurable amount d (< n), let (x0, y0) be a solution of the Diophantine equation
mx+ny=d obtained by applying Algorithm 2.3. If (x0 – y0) (m+n)/2k, then (x0, y0) is
the optimal solution of the corresponding two jugs problem. Otherwise, (x0 + n/k, y0 –
m/k) is the optimal solution of the problem obtained by applying Algorithm 2.2,
where k = gcd(m, n).
On Optimal Solution of the General Two Jugs Problem
141
Proof.
By applying Theorem 3.2 and Theorem 3.3, this result can be proved easily. The
details are skipped here.
EXAMPLES
We now illustrate how to apply the new results to solve the two jugs problem below.
Example 4.1.
Given a 3-litre jug and a 5-litre jug. We can apply Algorithm 2.1 to determine all
measurable amounts less than 8-litre by the jugs. The integer sequence obtained is
shown below.
0
3
+3
6
+3
1
4
-5
+3
7
+3
2
-5
5
+3
0
-5
Thus, the set of all positive measurable amounts by the two jugs is {3, 6, 1, 4, 7, 2, 5}.
The number of additions and subtractions involved for each measurable amount d
provides a solution to the associated linear Diophantine equation 3x+5y=d. For
example, there are 2 additions and 1 subtraction involved in obtaining an amount of 1litre, so x = 2 y = -1 is a solution of 3x+5y=1. If we use (x, y) to represent the amounts
of water inside the 3-litre jug and the 5-litre jug at each pouring step, then the
successive pouring steps for obtaining 1-litre are described as follows:
(0,0)
(3,0)
(0,3)
(3,3)
(1,5)
So, the number of steps involved by filling the 3-litre jug first is 4. In this case,
(m+n)/2k = (3+5)/2 = 4 and (x0, y0) = (2, -1). Since x0 – y0 = 3 < 4, so the pouring
sequence provides an optimal solution to this jug problem according to Theorem 3.3.
In fact, if we start to fill the 5-litre jug first, then the integer sequence obtained will
be:
0
5
+5
2
-3
7
+5
4
-3
1
-3
The number of additions and subtractions involved are 2 and 3 respectively, so x = -3,
y = 2 is a solution of the equation 3x+5y=1. The corresponding pouring steps are:
(0,0)
(0,5)
(3,2)
(0,2)
(2,0)
(2,5)
(3,4)
(0,4)
(3,1)
Thus, the total number of pouring steps involved for filling the 5-litre jug first is 8,
which requires more steps than filling the 3-litre jug first. The following table
summarizes the algorithm to be chosen for obtaining the optimal solutions for the
required measurable amounts.
142
Yiu-Kwong Man
Amount (d)
Algorithm to be adopted for obtaining
the optimal solution
3-L 6-L 1-L 4-L 7-L 2-L 5-L
Algorithm 2.2
Algorithm 2.3
Example 4.2.
Given a 6-litre jug and a 8-litre jug. We can apply Algorithm 2.1 to determine all
measurable amounts less than 8-litre by the jugs. The integer sequence obtained is
shown below.
0
6
+6
12
+6
4
-8
10
+6
2
-8
8
+6
0
-8
So, the set of all positive measurable amounts by the two jugs is {6, 12, 4, 10, 2, 8}.
Suppose we want to obtain an amount of 2-litre by the jugs. From the above sequence,
there are 3 additions and 2 subtractions involved, so x = 3, y = -2, which is a solution
of 6x+8y=2. The successive pouring steps for obtaining 2-litre are shown below:
(0,0)
(6,0)
(0,6)
(6,6)
(4,8)
(4,0)
(0,4)
(6,4)
(2,8)
Thus, the number of steps involved by filling the 6-litre jug first is 8. In this case,
(m+n)/2k = (6+8)/4 = 3.5 and (x0, y0) = (3, -2). Since x0 – y0 = 5 > 3.5, so it is not an
optimal solution to the problem according to Theorem 3.3. In fact, if we start to fill
the 8-litre jug first, then the integer sequence obtained will be:
0
8
+8
2
-6
The number of addition and subtraction involved are 1 and 1 respectively, so x = -1, y
= 1 is a solution of the equation 6x+8y=2. The corresponding pouring steps are:
(0,0)
(0,8)
(6,2)
Thus, the total number of pouring steps involved for filling the 8-litre jug first is 2
only, which requires less steps than filling the 6-litre jug first. The following table
summarizes the algorithm to be chosen for obtaining the optimal solutions for the
required measurable amounts.
Amount (d)
Algorithm to be adopted for obtaining
the optimal solution
6-L 12-L 4-L 10-L 2-L 8-L
Algorithm 2.2
Algorithm 2.3
On Optimal Solution of the General Two Jugs Problem
143
FINAL REMARKS
In this paper, we have discussed the interrelationship between the integer sequences
obtained by Algorithm 2.2 and Algorithm 2.3, as well as the decision theorems for
finding the optimal solution of the general two jugs problem. No additional memory
cost is required for conducting searching and branching like common search methods
(e.g. BFS or DFS) do when finding the solution of the problem concerned. It is
suitable for hand calculations or implementation in common computer languages
since the integer sequences involved can be computed easily by additions and
subtractions only. We hope the algorithms and the results discussed in this paper will
be found useful to researchers in the areas of artificial intelligence, computer sciences,
discrete mathematics, problem-solving, cognitive psychology and recreational
mathematics.
ACKNOWLEDGMENT
This work was supported by the research grants of EdUHK in 2015/16.
REFERENCES
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
[11]
G. Polya, 1945, How to Solve It, Princeton University Press, NY.
M.C.K. Tweedie, A graphical method of solving Tartaglian measuring puzzles,
Mathematical Gazette, 23(1939), 278–282.
H.S.M.Coxeter and S.L.Greitzer, 1967, Geometry Revisited, The Mathematical
Association of America, Washington D.C.
H. E. Dudeney, 1970, Amusements in Mathematics, Dover, NY.
C. J. McDiarmid and J. R. Alfonsin, Sharing jugs of wine, Discrete
Mathematics, 125(1994), 279–287.
M. Rem, Y. Choo, A fixed-space program of linear output complexity for the
program of the three vessels, Science of Computer Programming, 2(1982),
133–141.
G. P. Thomas, The water jugs problem: Solutions from artificial intelligence
and mathematical viewpoint, Mathematics in School, 24(5)(1995), 34–37.
B. Harvey, 1997, Computer Science Logo Style: Symbolic Computing (Vol. I),
MIT, MA.
S. Abu Naser, Developing visualization tool for the teaching AI searching
algorithms, Information Technology Journal, 7(2)(2008), 350–355.
D. Saxena, N. K. Malik, V. R. Singh, A cognitive approach to solve water jugs
problem, International Journal of Computer Applications, 124(17)(2015), 45–
54.
M. K. Colvin, K. Dunbar and J. Grafman, The effects of frontal lobe lesions on
goal achievement in the water jug task, Journal of Cognitive Neuroscience,
13(8)(2001), 1129–1147.
144
Yiu-Kwong Man
[12]
S. L. Beilock and M.S. DeCaro, From poor performance to success under
stress: working memory, strategy selection and mathematical problem solving
under pressure, Journal of Experimental Psychology, 33(6)(2007), 983–998.
S. D. Marchi, Mathematics and wine, Applied Mathematics and Computation,
192(2007), 180–190.
H. P. Carder, S. J. Handley and T. J. Perfect, Counterintuitive and alternative
moves choice in the water jug tasks, Brain and Cognition, 66(2008), 11–20.
Y. K. Man, Solving the water jugs problem by an integer sequence approach,
International Journal of Mathematical Education in Science & Technology,
43(1)(2012), 109–113.
Y. K. Man, On algorithmic approach to the two jugs problem, Applied
Mathematical Sciences, 9(36)( 2015), 1787–1796.
Y. K. Man, On computing the measurable amounts of the two jugs problem,
Mathematics in Computer Sciences, 9(2015), 453–459.
Y. K. Man, On optimal solutions of a family of two jugs problem, Global
Journal of Pure and Applied Mathematics, 11(2015), 3559–3563.
D. M. Burdon, 2002, Elementary Number Theory, 5th edition, McGraw-Hill,
NY.
R. Crandall and C. Pomerance, 2001, Prime Numbers: A Computational
Perspective, Springer, NY.
[13]
[14]
[15]
[16]
[17]
[18]
[19]
[20]