Javatpoint Logo
Javatpoint Logo

Water Jug Problem in Python

The water jug problem is a classic puzzle that involves using two jugs to measure a certain amount of water. The main objective of the water jug problem is to use the jugs to measure out a specific amount of water by filling and emptying the jugs in a particular order. Here's an algorithm and Python code to solve the problem:

Algorithm:

  • Initialize two variables j1 and j2 to represent the current amount of water in each jug.
  • Set a target amount of water to measure out.
  • Create a list of possible actions, including filling a jug, emptying a jug, and pouring water from one jug to the other.
  • Create an empty set to keep track of visited states.
  • Create a stack to keep track of states to visit, and add the initial state to the stack.
  • While the stack is not empty:
    • Pop the top state from the stack.
    • If this state has not been visited before, add it to the visited set.
    • Check if the state matches the target amount of water. If so, return the seq actions taken to get to this state.
    • If the state does not match the target amount, generate all possible next states from this state by applying each of the possible actions in turn.
    • If it has not been visited before, add it to the stack for each next state.
  • If the stack becomes empty without finding a solution, return None.

Python Code:

Output:

[('fill', 1), ('pour', 1, 2), ('empty', 2), ('pour', 1, 2), ('fill', 1), ('pour', 1, 2)]

Explanation:

In this example, we are using a jug with a capacity of 4 and another with a capacity of 3 to measure out 2 units of water. The function will return a sequence of actions that can be taken to achieve this, such as filling the first jug, pouring it into the second jug,

Space Complexity

Let us consider the space complexity. We use a set to store the visited states and a queue to store the states to visit, so the space required is proportional to the number of states visited. In the worst-case scenario, where there is no solution, we would need to visit every possible state, which is bounded by the product of the capacities of the two jugs. Therefore, the space complexity is O(jug1_cap * jug2_cap).

Time Complexity

Next, let us consider the time complexity. In the worst-case scenario, we would need to visit every possible state to find a solution. Each state has six possible next states (filling, emptying, or pouring each jug), so the branching factor is 6. Therefore, the time complexity is exponential, O(6^d), where d is the depth of the search tree. The search tree's depth is the sum of the capacities of the two jugs since we cannot have more water in the jugs than their capacities. Therefore, the time complexity is O(6^(jug1_cap + jug2_cap)).

In practice, the search space is much smaller than the worst-case scenario, so the algorithm is typically much faster than its theoretical time complexity might suggest.

Bézout's identity Approach

There is another approach to solve the water jug problem which is known as "Bézout's identity" approach. This approach uses the concept of Bézout's identity, which is a theorem from number theory. This theory states that for any two integers a and b, there exist integers x and y such that ax + by = gcd(a, b), where gcd is the greatest common divisor of a and b.

In the case of the water jug problem, we can apply Bézout's identity to find if measuring a certain amount of water using jugs with given capacities is possible. Specifically, if the most common divisor of the jug capacities divides the desired amount of water, then it is possible to measure that amount using the jugs.

To find the actual steps and measure the desired amount of water, we can use the extended Euclidean algorithm to find the coefficients x and y such that ax + by = gcd(a, b). These coefficients can be used to determine how much water to pour from each jug in each step.

Python code:

Output:

[('fill', 1), ('pour', 1, 2), ('empty', 2), ('pour', 1, 2), ('fill', 1), ('pour', 1, 2), ('fill', 1), ('pour', 1, 2), ('empty', 2), ('pour', 1, 2), ('fill', 1), ('pour', 1, 2)]

Explanation:

The "can_measure_water" function checks if it is possible to measure out the desired amount of water using the jugs, and the "measure_water" function returns the sequence of steps needed to measure out the desired amount of water.

The Bézout's identity approach has a time complexity of O(log(min(a,b))), where a and b are the capacities of the two jugs. It is much faster than the brute-force approach, especially for large capacities, but it requires some knowledge of number theory to understand and implement.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA