Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
262 views

Experiment - 6: Aim: WAP To Implement Water Jug Problem Using BFS. Theory

This document describes an experiment to implement the water jug problem using breadth-first search (BFS). The problem involves using jugs of sizes m and n liters to measure a target amount d liters. BFS is used to find a path from the initial empty state to a goal state where one jug contains d liters. The code implements BFS by maintaining a queue of states and exploring all possible next states by emptying, filling, or pouring between jugs until a goal is reached.

Uploaded by

akshat sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
262 views

Experiment - 6: Aim: WAP To Implement Water Jug Problem Using BFS. Theory

This document describes an experiment to implement the water jug problem using breadth-first search (BFS). The problem involves using jugs of sizes m and n liters to measure a target amount d liters. BFS is used to find a path from the initial empty state to a goal state where one jug contains d liters. The code implements BFS by maintaining a queue of states and exploring all possible next states by emptying, filling, or pouring between jugs until a goal is reached.

Uploaded by

akshat sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

EXPERIMENT -6

Aim: WAP to implement water jug problem using BFS.


Theory:
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) the path from the 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).
The operations you can perform are: 
1. Empty a Jug, (X, Y)->(0, Y) Empty Jug 1
2. Fill a Jug, (0, 0)->(X, 0) Fill Jug 1
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)

CODING:
from collections import deque
def BFS(a, b, target):
m = {}
isSolvable = False
path = []
q = deque()
q.append((0, 0))
while (len(q) > 0):
u = q.popleft()
if ((u[0], u[1]) in m):
continue
if ((u[0] > a or u[1] > b or
u[0] < 0 or u[1] < 0)):
continue
path.append([u[0], u[1]])
m[(u[0], u[1])] = 1
if (u[0] == target or u[1] == target):
isSolvable = True
if (u[0] == target):
if (u[1] != 0):
path.append([u[0], 0])
else:
if (u[0] != 0):
path.append([0, u[1]])
sz = len(path)
for i in range(sz):
print("(", path[i][0], ",",
path[i][1], ")")
break
q.append([u[0], b])
q.append([a, u[1]])
for ap in range(max(a, b) + 1):
c = u[0] + ap
d = u[1] - ap
if (c == a or (d == 0 and d >= 0)):
q.append([c, d])
c = u[0] - ap
d = u[1] + ap
if ((c == 0 and c >= 0) or d == b):
q.append([c, d])
q.append([a, 0])
q.append([0, b])
if (not isSolvable):
print ("No solution")
if __name__ == '__main__':
Jug1, Jug2, target = 4, 3, 2
print("Path from initial state "
"to solution state ::")
BFS(Jug1, Jug2, target)

OUTPUT:

RESULT:
The program is successfully done and compiled in python.

You might also like