0% found this document useful (0 votes)
3 views

Ass2_10

Uploaded by

202100264
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)
3 views

Ass2_10

Uploaded by

202100264
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/ 6

Name Ojas Khawas

SRN No 202100264
Roll no 10
Division / Batch C-1
Assignment no 02
Subject AI LAB
topic Water jug problem using BFS and DFS
Problem Statement: Water jug problem using BFS and DFS

Water jug problem using BFS

Code:
def bfs(start, end, x_capacity, y_capacity):
path = []
front = []
front.append(start)
visited = []
#visited.append(start)
print(f"{front}\n{visited}\n{path}\n --------------- ")
cost = 0
while(front):
current = front.pop() #[0,3]
cost+=1
x = current[0]
y = current[1]
path.append(current)
if x == end or y == end:
print(f"Found! + cost = {cost}")
return path
# rule 1
if current[0] < x_capacity and ([x_capacity, current[1]] not in visited):
front.append([x_capacity, current[1]])
visited.append([x_capacity, current[1]])

# rule 2
if current[1] < y_capacity and ([current[0], y_capacity] not in visited):
front.append([current[0], y_capacity])
visited.append([current[0], y_capacity])

# rule 3
if current[0] > x_capacity and ([0, current[1]] not in visited):
front.append([0, current[1]])
visited.append([0, current[1]])

# rule 4
if current[1] > y_capacity and ([x_capacity, 0] not in visited):
front.append([x_capacity, 0])
visited.append([x_capacity, 0])
# rule 5
#(x, y) -> (min(x + y, x_capacity), max(0, x + y - x_capacity)) if y > 0
if current[1] > 0 and ([min(x + y, x_capacity), max(0, x + y - x_capacity)] not in
visited):#[3,0]
front.append([min(x + y, x_capacity), max(0, x + y - x_capacity)])
visited.append([min(x + y, x_capacity), max(0, x + y - x_capacity)])

# rule 6
# (x, y) -> (max(0, x + y - y_capacity), min(x + y, y_capacity)) if x > 0
if current[0] > 0 and ([max(0, x + y - y_capacity), min(x + y, y_capacity)] not in
visited):
front.append([max(0, x + y - y_capacity), min(x + y, y_capacity)])
visited.append([max(0, x + y - y_capacity), min(x + y, y_capacity)])
print(f"{front}\n{visited}\n{path}\n----------------\n --------------- ")

return "Not found"

def gcd(a, b):


if a == 0:
return b
return gcd(b%a, a)

# start state: x = 0 , y = 0
start = [0, 0]
#end = 2
#x_capacity = 4
#y_capacity = 3

# condition for getting a solution:


# the target volume 'end' should be a multiple of gcd(a,b)
print("Solution for water jug problem")
# x_capacity = int(input("Enter Jug 1 capacity:"))
# y_capacity = int(input("Enter Jug 2 capacity:"))
# end = int(input("Enter target volume:"))
x_capacity=4
y_capacity=3
end = 2
if end % gcd(x_capacity,y_capacity) == 0:
print(bfs(start, end, x_capacity, y_capacity))
else:
print("No solution possible for this combination.")
Output:

Water jug problem using DFS

Code:

def DFS(a, b, target):


m = {}
isSolvable = False
path = []
# Stack to maintain states
stack = []
# Initializing with initial state
stack.append((0, 0))
count=0
while len(stack) > 0:

# Current state
u = stack.pop() #[0,0]
count+=1
# If this state is already visited
if (u[0], u[1]) in m:
continue

# Doesn't met jug constraints


if u[0] > a or u[1] > b or u[0] < 0 or u[1] < 0:
continue

# Filling the vector for constructing


# the solution path
path.append([u[0], u[1]])

# Marking current state as visited


m[(u[0], u[1])] = 1

# If we reach solution state, put ans=1


if u[0] == target or u[1] == target:
isSolvable = True

if u[0] == target:
if u[1] != 0:

# Fill final state


path.append([u[0], 0])
elif u[0] != 0:
# Fill final state
path.append([0, u[1]])

# Print the solution path


sz = len(path)
for i in range(sz):
print("(", path[i][0], ",", path[i][1], ")")
break

# If we have not reached final state


# then, start developing intermediate
# states to reach solution state
if u[1] <= b:
stack.append([u[0], b]) # Fill Jug2
elif u[0] <= a:
stack.append([a, u[1]]) # Fill Jug1

for ap in range(max(a, b) + 1):

# Pour amount ap from Jug2 to Jug1


c = u[0] + ap
d = u[1] - ap

# Check if this state is possible or not


if c == a or (d == 0 and d >= 0):
stack.append([c, d])
# Pour amount ap from Jug 1 to Jug2
c = u[0] - ap
d = u[1] + ap

# Check if this state is possible or not


if (c == 0 and c >= 0) or d == b:
stack.append([c, d])
print(stack,f"\n{count} ---------- \n")

# No, solution exists if ans=0


if not isSolvable:
print("No solution")

if name == " main ":

Jug1, Jug2, target = 4, 3, 2


print("Path from initial state " "to solution state ::")
DFS(Jug1, Jug2, target)

Output:

You might also like