Water Jag Problem
Water Jag Problem
Madhuri Gupta
Assistant Professor
Department of Computer Science & Engineering
Agenda
Recap
What is water Jug Problem
Production Rules
Steps to follow for solution
Methods to Follow
Other Aspects of solving Water Jug Problem
Summary
PRESENTATION TITLE 2
A State Space problem
Recap
8 Puzzle problem
Problem Characteristics
4
Application of State Space
PRESENTATION TITLE 5
Water Jug Problem
6
State Space Search
Example Application of State Space
8 Puzzle Problem Routing and Navigation
Game Playing
Natural Language Processing
Planning and Scheduling
Initial State Robotics
Goal State Data Mining and Machine Learning
Actions
Transistion Model
State Space
Cost Function
Problem Characteristics
PRESENTATION TITLE 7
Production Rules
• Definition
They are used to model the behavior of intelligent
agents or systems by specifying a set of conditional
statements (conditions) and corresponding actions
to be taken if those conditions are satisfied.
Production rules provide a way to represent
knowledge and guide decision-making in a
structured manner
PRESENTATION TITLE 8
Application: Production Rules
1 2 3 4 5
PRESENTATION TITLE 9
Application of State Space
PRESENTATION TITLE 10
PRESENTATION TITLE 11
Water Jug Problem
The State Space of Water Jug
Problem Statement Problem
You are given 2 jugs with the
capacity 'm' and 'n' respectively. Initially,
they are given empty. There is an unlimited
supply of water. You can either fill the
whole jug or a quantity that is less than the
given capacity of jugs. Now, you are also
given a third positive integer 'd'. Using the
2 given jugs, you need to come up with a
solution to have 'd' amount of water in them
and return the number of steps you took to
reach that capacity.
PRESENTATION TITLE 12
Operations Performed on the Jugs
Operations: Example
• Empty a jug (a, b) -> (0,
b) Input: 3, 5, 4
• Fill a jug (0, b) -> (a, b) Output: 6
• Transfer water from one
jug to another.
PRESENTATION TITLE 13
Water Jug Problem 14
Explanation
1. Fill the 5-liter jug completely.
2. Transfer 3 liters from a 5-liter jug to a 3-liter jug.
3. Empty the 3-liter capacity jug.
4. Transfer the remaining 2 liters from a 5-liter jug to a 3-liter jug.
5. Now, fill the 5-liter jug fully.
6. Pour 1 liter from a 5-liter jug into a 3-liter jug.
7. There! We have 4 liters in the first jug, now empty the 2nd jug.
PRESENTATION TITLE 15
Example 2
PRESENTATION TITLE 16
Production Rules for this Example 2
PRESENTATION TITLE 17
Steps for the solution
Steps to follow Steps to follow
Step1: Fill jug B Step 5: Fill jug B
• Jug A: 0L, Jug B: 7L • Jug A: 3L, Jug B: 7L
PRESENTATION TITLE 18
Example 3
PRESENTATION TITLE 19
Production Rules for this Example 3
PRESENTATION TITLE 20
Steps for the solution
Steps to follow Steps to follow
Step 1: Fill the 3-liter jug Step 5: Fill the 3-liter jug
• Jug A: 0L, Jug B: 3L • Jug A: 3L, Jug B: 2L
Step 2: Pour from B to A Step 6: Pour water from the 3-liter jug to the 4-liter
jug until the 4-liter jug is full
Jug A: 3L, Jug B: 0L
• Jug A: 1L, Jug B: 4L
Step 3: Pour the water back from A to B
• Jug A: 0L, Jug B: 3L
Step 4: Pour the remaining 1 liter from the 3-liter jug to
the 4-liter jug
• Jug A: 0L, Jug B: 2L
PRESENTATION TITLE 21
Production Rules
PRESENTATION TITLE 22
03 Methods to Solve the Water Jug Problem
03 Methods to
Solve the Water
Jug Problem
PRESENTATION TITLE 23
Method 01) always pour from 1st jug to 2nd
jug
1. Fill the m liter jug and empty it into the n liter jug.
4. Repeat the above steps till any one of the jugs contains the required amount of water in
them.
PRESENTATION TITLE 24
Method 02) Always pour from 2nd jug to 1st jug
• Fill the n-liter jug and empty it into the m-liter jug.
• Repeat the above steps till any of the jugs contain the required amount of water in them.
PRESENTATION TITLE 25
Method 03) Using BFS(Breadth First Search)
• In this method, we use a queue and a map to find the solution. In the beginning, we
initialize it with zero as both the jugs are empty.
• We then keep applying the above operations that are permitted to us and find the possible
scenarios to find the targeted amount of water in the jugs.
• Also, we keep maintaining an empty matrix to keep a track of the positions or states that
we have encountered. In this way, the space complexity reduces manifolds and the
efficiency of the program continuously improves.
PRESENTATION TITLE 26
PRESENTATION TITLE 27
PRESENTATION TITLE 28
PRESENTATION TITLE 29
PRESENTATION TITLE 30
Complexity Analysis of water jug problem
PRESENTATION TITLE 31
The different ways of approaching the Water Jug Problem
1. Algebraic Method
PRESENTATION TITLE 32
Binary Method
Step 1: Establish variables: Let c be the required amount of water, and let
a and b represent the first and second jugs' respective capacities.
Step 2: Specify the quantity of water is in each of the jugs: The quantity of
water in each jug should be represented using binary values. For instance,
if a = 3, you may use the binary numbers 0, 1, 2, or 3 to express how much
water is in the first jug. (e.g., 00, 01, 10, 11). Similarly, if b = 5, you may use
the binary numbers 0, 1, 2, 3, or 5 to express how much water is in the
second jug. (e.g., 000, 001, 010, 011, 100, 101).
Step 3: Filling up the first jug: Set the binary integer that represents the
first jug to the highest possible value, such as 11 if a = 3.
PRESENTATION TITLE 33
Continue..
Step 4: Water should be moved between the jugs: Transfer water using
binary operations from the first jug to the second jug until the second jug
has the desired amount of water in it. (c). The binary operations are
binary OR, AND, and XOR, as well as bitwise shift (left or right). For
instance, to move one unit of water from one jug to another, shift the first
jug's binary digits by one (by eliminating the rightmost digit), and then
OR the resultant binary number with the digits of the second jug. (Adding
the rightmost digit).
Step 5: Verify the answer: Verify that the binary number that results
represents the second jug with the appropriate amount of water.
PRESENTATION TITLE 34
Conclusion
Conclusion
Conclusion
After watching this video , it is certain that you
must have understood the Water Jug problem and
the approaches with which it can be solved.
PRESENTATION TITLE 35
Thank you