0% found this document useful (0 votes)
242 views36 pages

Water Jag Problem

Uploaded by

siddartha0331
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
242 views36 pages

Water Jag Problem

Uploaded by

siddartha0331
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Water Jug 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

Water Jug Problem


PRESENTATION TITLE

Recap

Definition of State space Search

8 Puzzle problem

Problem Characteristics

(1)Initial state(3) Action

(2)Goal State(4)Transisition Model(5) State Space(6) Cost


Function

4
Application of State Space

• Routing and Navigation


• Game Playing
• Natural Language
• Planning and Scheduling
• Robotics
• Data Mining and Machine Learning

PRESENTATION TITLE 5
Water Jug Problem

Conclusion of State space search is a powerful concept used


to solve problems by systematically exploring
State Space possible states and transitions to achieve a
desired goal
Seach

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

Goal- Backtracking Problem-


Defining Exploring the
Directed and Specific
Rules State Space
Behavior Optimization Adaptation

PRESENTATION TITLE 9
Application of State Space

• Routing and Navigation


• Game Playing
• Natural Language
• Planning and Scheduling
• Robotics
• Data Mining and Machine Learning

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

we have two jugs, A and B, with capacities of 4


liters and 7 liters respectively. Our task is to
measure exactly 2 liters of water.

PRESENTATION TITLE 16
Production Rules for this Example 2

Rule 1: Fill a jug

Rule 2: Empty a jug

Rule 3: Pour water

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

Step 2: Pour from B to A Step 6: Pour from B to A until A is full

• Jug A: 4L, Jug B: 3L • Jug A: 4L, Jug B: 6L


Step 7: Empty jug A
Step 3: Empty jug A
• Jug A: 0L, Jug B: 6L
• Jug A: 0L, Jug B: 3L
Step 8: Pour from B to A
Step 4: Pour from B to A
• Jug A: 4L, Jug B: 2L
• Jug A: 3L, Jug B: 0L

PRESENTATION TITLE 18
Example 3

Now using the example of 4 liters jug and 3 liter jug


and goal is to measure exactly 2 liter water in 4
liters jug

PRESENTATION TITLE 19
Production Rules for this Example 3

Rule 1: Fill a jug

Rule 2: Empty a jug

Rule 3: Pour water

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

Rule 1: Fill a jug

Rule 2: Empty a jug

Rule 3: Pour water

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.

2. When the m liter jug becomes empty refill it.

3. When the n-liter jug becomes full, empty it.

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.

• When the n liter jug becomes empty refill it.

• When the m liter jug becomes full, empty it.

• 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

• Time Complexity for the solution will be


O(n*m)
• Space Complexity for it will be O(n*m)

PRESENTATION TITLE 31
The different ways of approaching the Water Jug Problem
1. Algebraic Method

The steps are as follows:


Step 1: Define the variables: Let x represent the volume of water in the first jug, y represent the
volume in the second jug, and z represent the amount of water that is wanted.
Step 2: Equations to be written down: Using the restrictions of the problem, write out two
equations.
Step 3: Find values for x and y that satisfy both equations by solving the system of
equations. Systems of equations can be solved using a variety of techniques, such as substitution
or elimination.
Step 4: Verify the answer: Make sure the answer fulfils the requirements of the issue, such as
making sure the values of x and y are non-negative and less than or equal to the respective jugs'
capacity.

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

You might also like