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

Lab 7 and 8

The document contains two Python program implementations: one for Breadth First Search (BFS) traversal on a graph and another for solving the Water Jug Problem using BFS. The BFS program initializes a graph and uses a queue to traverse and print nodes, while the Water Jug Problem program explores states to reach a target volume using two jugs. Both programs include driver code to execute the respective algorithms.

Uploaded by

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

Lab 7 and 8

The document contains two Python program implementations: one for Breadth First Search (BFS) traversal on a graph and another for solving the Water Jug Problem using BFS. The BFS program initializes a graph and uses a queue to traverse and print nodes, while the Water Jug Problem program explores states to reach a target volume using two jugs. Both programs include driver code to execute the respective algorithms.

Uploaded by

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

EXPERIMENT NO.

Program Name: Write a Python program to implement Breadth First Search


Traversal.
Implementation:
graph = { '0' : ['1','2'], '1' : ['3', '4'], '2' : ['5','6'], '3' : [], '4' : [],'5' : [], '6' : []
}
visited = [] # List for visited nodes.
queue = [] #Initialize a queue
def bfs(visited, graph, node): #function for BFS
visited.append(node)
queue.append(node)
while queue: # Creating loop to visit each node
m = queue.pop(0)
print (m, end = " ")
for neighbour in graph[m]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)

# Driver Code
print("Following is the Breadth-First Search")
bfs(visited, graph, '0') # function calling

Prince Yadav IT3 2100270130133


EXPERIMENT NO. 8

Program Name: Write a Python program to implement Water Jug Problem.


Implementation:
from collections import deque

def bfs(a, b, target):


visited = set()
queue = deque()

initial_state = (0, 0)
queue.append((initial_state, []))
visited.add(initial_state)

while queue:
current_state, path = queue.popleft()
x, y = current_state

if x == target or y == target:
print("Path to reach the target:")
for state in path:
print(state)
return True

# Fill jug 1
if (a, y) not in visited:
new_state = (a, y)
new_path = path + [new_state]
queue.append((new_state, new_path))
visited.add(new_state)

# Fill jug 2
if (x, b) not in visited:
new_state = (x, b)
new_path = path + [new_state]
queue.append((new_state, new_path))
visited.add(new_state)

Prince Yadav IT3 2100270130133


# Empty jug 1
if (0, y) not in visited:
new_state = (0, y)
new_path = path + [new_state]
queue.append((new_state, new_path))
visited.add(new_state)

# Empty jug 2
if (x, 0) not in visited:
new_state = (x, 0)
new_path = path + [new_state]
queue.append((new_state, new_path))
visited.add(new_state)

# Pour from jug 1 to jug 2


pour = min(x, b - y)
if (x - pour, y + pour) not in visited:
new_state = (x - pour, y + pour)
new_path = path + [new_state]
queue.append((new_state, new_path))
visited.add(new_state)

# Pour from jug 2 to jug 1


pour = min(y, a - x)
if (x + pour, y - pour) not in visited:
new_state = (x + pour, y - pour)
new_path = path + [new_state]
queue.append((new_state, new_path))
visited.add(new_state)

return False

if __name__ == "__main__":
a=3
b=5
target = 4

if not bfs(a, b, target):


print("No solution exists!")

Prince Yadav IT3 2100270130133


Prince Yadav IT3 2100270130133

You might also like