Water Jug problem using BFS
Water Jug problem using BFS
You are given an m liter jug and a n liter jug. Both the jugs are initially empty. The jugs don’t
have markings to allow measuring smaller quantities. You have to use the jugs to measure d
liters of water where d is less than n.
(X, Y) corresponds to a state where X refers to the amount of water in Jug1 and Y refers to the
amount of water in Jug2
Determine the path from e initial state (xi, yi) to the final state (xf, yf), where (xi, yi) is (0, 0)
which indicates both Jugs are initially empty and (xf, yf) indicates a state which could be (0, d)
or (d, 0).
3. Pour water from one jug to the other until one of the jugs is either empty or full, (X, Y) -
> (X-d, Y+d)
Examples:
Input : 4 3 2
Output : {( 0,0),(0,3),(3,0),(3,3),(4,2),(0,2)}
We have discussed the optimal solution in The Two Water Jug Puzzle. In this post, a BFS based
solution is discussed.
Here, we keep exploring all the different valid cases of the states of water in the jug
simultaneously until and unless we reach the required target water.
As provided in the problem statement, at any given state we can do either of the following
operations:
1. Fill a jug
2. Empty a jug
3. Transfer water from one jug to another until either of them gets completely filled or
empty.
Examples:
Input: X = 4, Y = 3, Z = 2
Output: {(0, 0), (0, 3), (3, 0), (3, 3), (4, 2), (0, 2)}
Explanation:
Step 1:- First we will fill the 4 litre jug completely with water.
Step 2:- Then optimal approach would be to empty water from 4-litre jug
into 3-litre (leaving 1L water in 4L jug and 3L completely full). Hence we
got 1L water.
Step 3:- Now, Empty water from 3L.
Step 4:- Pour the water from 4L jug into 3L jug Now 4L container is
completely empty and 1L water in present in 3L litre jug.
Step 5:- Fill the 4L jug with water completely again.
Step 6:- On transferring water from 4L jug to 3L jug, we will get 2L
water in 4L jug which was our required quantity.
Input: X = 3, Y = 5, Z = 4
Output: 6
Explanation:
Step 1:- First we will fill the 5-litres jug to its maximum capacity.
Step 2:- Then optimal approach would be to transfer 3-litres from 5-litres
jug to 3-litres jugs.
Step 3:- Now, Empty the 3-litres jug.
Step 4:- Transfer 2L from 5L jug to 3-L jug.
Step 5:- Now, Fill 5-litres jug to its maximum capacity.
Step 6:- On Pouring water from 5L jug to 3L jug until it’s full we will
get 4L water in 5-litre jug which was our required quantity.
We start at an initial state in the queue where both the jugs are empty. We then continue to
explore all the possible intermediate states derived from the current jug state using the operations
provided.
We also, maintain a visited matrix of states so that we avoid revisiting the same state of jugs
again and again.
From the table above, we can observe that the state where both the jugs are filled is redundant
as we won’t be able to continue ahead / do anything with this state in any possible way.
So, we proceed, keeping in mind all the valid state cases (as shown in the table above) and we
do a BFS on them.
In the BFS, we first skip the states which was already visited or if the amount of water in either
of the jugs exceeded the jug quantity.
If we continue further, then we first mark the current state as visited and check if in this state, if
we have obtained the target quantity of water in either of the jugs, we can empty the other jug
and return the current state’s entire path.
But, if we have not yet found the target quantity, we then derive the intermediate states from the
current state of jugs i.e. we derive the valid cases, mentioned in the table above (go through the
code once if you have some confusion).
We keep repeating all the above steps until we have found our target or there are no more states
left to proceed with.