0% found this document useful (0 votes)
45 views12 pages

Lab Program 1

The document outlines the implementation of the Depth First Search (DFS) algorithm to solve the Water Jug Problem. It provides a detailed algorithm, pseudocode, and Python code for generating possible states and transitions until the desired water quantity is achieved. The conclusion emphasizes the relevance of the Water Jug Problem as a practical example of DFS application in real-world scenarios.

Uploaded by

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

Lab Program 1

The document outlines the implementation of the Depth First Search (DFS) algorithm to solve the Water Jug Problem. It provides a detailed algorithm, pseudocode, and Python code for generating possible states and transitions until the desired water quantity is achieved. The conclusion emphasizes the relevance of the Water Jug Problem as a practical example of DFS application in real-world scenarios.

Uploaded by

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

Artificial Intelligence (BAD402)

Dr. Asha S Manek


Professor & HOD– AIML,
TJIT, Bangalore
[email protected]

BAD402 Artificial Intelligence


1
Implement and Demonstrate Depth First Search Algorithm on Water Jug Problem

BAD402 Artificial Intelligence


2
Implement and Demonstrate Depth First Search Algorithm on Water Jug Problem

BAD402 Artificial Intelligence


3
Implement and Demonstrate Depth First Search Algorithm on Water Jug Problem

BAD402 Artificial Intelligence


4
Implement and Demonstrate Depth First Search Algorithm on Water Jug Problem

Algorithm
The algorithm for solving the Water Jug Problem using DFS involves
exploring all possible states and transitions between them until the
desired quantity of water is achieved. Here are the high-level steps of the
algorithm:
1.Create a stack to store the states of the jugs.
2.Initialize the stack with the initial state (both jugs empty).
3.While the stack is not empty, do the following:
1. Pop a state from the stack.
2. If the state represents the desired quantity, stop and return the
solution.
3. Generate all possible next states from the current state.
4. Push the next states onto the stack.
4.If the stack becomes empty and no solution is found, the problem is
unsolvable.

BAD402 Artificial Intelligence


5
Implement and Demonstrate Depth First Search Algorithm on Water Jug Problem

Pseudocode
function solveWaterJugProblem(capacity_jug1, capacity_jug2,
desired_quantity):
stack = empty stack
push initial state (0, 0) onto stack

while stack is not empty:


current_state = pop from stack

if current_state represents desired_quantity:


return current_state

generate next states from current_state

push next states onto stack

return "No solution found"

BAD402 Artificial Intelligence


6
Implement and Demonstrate Depth First Search Algorithm on Water Jug Problem

The Python implementation of the Water Jug Problem using the DFS
algorithm:
def solveWaterJugProblem(capacity_jug1, capacity_jug2, desired_quantity):
stack = []
stack.append((0, 0)) # Initial state: both jugs empty

while stack:
current_state = stack.pop()

if current_state[0] == desired_quantity or current_state[1] == desired_quantity:


return current_state

next_states = generateNextStates(current_state, capacity_jug1, capacity_jug2)


stack.extend(next_states)

return "No solution found"

BAD402 Artificial Intelligence


7
Implement and Demonstrate Depth First Search Algorithm on Water Jug Problem

The Python implementation of the Water Jug Problem using the DFS
algorithm:
def generateNextStates(state, capacity_jug1, capacity_jug2):
next_states = []

# Fill Jug 1
next_states.append((capacity_jug1, state[1]))

# Fill Jug 2
next_states.append((state[0], capacity_jug2))

# Empty Jug 1
next_states.append((0, state[1]))

# Empty Jug 2
next_states.append((state[0], 0))

BAD402 Artificial Intelligence


8
Implement and Demonstrate Depth First Search Algorithm on Water Jug Problem

The Python implementation of the Water Jug Problem using the DFS
algorithm:
# Pour water from Jug 1 to Jug 2
pour_amount = min(state[0], capacity_jug2 - state[1])
next_states.append((state[0] - pour_amount, state[1] + pour_amount))

# Pour water from Jug 2 to Jug 1


pour_amount = min(state[1], capacity_jug1 - state[0])
next_states.append((state[0] + pour_amount, state[1] - pour_amount))

return next_states

solution = solveWaterJugProblem(4, 3, 2)
print("Solution:", solution)

Solution: (4, 2)

BAD402 Artificial Intelligence


9
Implement and Demonstrate Depth First Search Algorithm on Water Jug Problem

Explanation
• The algorithm starts with an empty stack and pushes the initial state (both
jugs empty) onto the stack.
• While the stack is not empty, it pops a state from the stack, checks if the state
represents the desired quantity, generates all possible next states from the
current state, and pushes them onto the stack.
• This process continues until the stack becomes empty or the desired quantity
is found.
• The solveWaterJugProblem function takes the capacities of the two jugs and the
desired quantity as input.
• It initializes an empty stack and pushes the initial state onto the stack.
• In each iteration, it checks if the current state satisfies the desired quantity.
• If not, it generates the next states based on the available operations (filling,
emptying, and pouring) and pushes them onto the stack.
• Once the desired quantity is found, the function returns the current state.
• If the stack becomes empty without finding a solution, it returns a message
indicating that no solution was found.
• The generateNextStates function generates all possible next states from a given
state by considering the available operations for each jug.

BAD402 Artificial Intelligence


10
Implement and Demonstrate Depth First Search Algorithm on Water Jug Problem

Conclusion

• The Water Jug Problem serves as an interesting example to understand


the application of DFS in solving real-world problems.

BAD402 Artificial Intelligence


11
BAD402 Artificial Intelligence
12

You might also like