Ass2_10
Ass2_10
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
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 --------------- ")
# start state: x = 0 , y = 0
start = [0, 0]
#end = 2
#x_capacity = 4
#y_capacity = 3
Code:
# Current state
u = stack.pop() #[0,0]
count+=1
# If this state is already visited
if (u[0], u[1]) in m:
continue
if u[0] == target:
if u[1] != 0:
Output: