0% found this document useful (0 votes)
6 views7 pages

Aiml

The document discusses three algorithms: depth-first search (DFS), breadth-first search (BFS), and two implementations of a vacuum cleaner algorithm - one using a goal-based approach and finding the nearest dirty cell, the other using a utility-based approach and finding the dirtiest cell.

Uploaded by

Muskan gupta
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)
6 views7 pages

Aiml

The document discusses three algorithms: depth-first search (DFS), breadth-first search (BFS), and two implementations of a vacuum cleaner algorithm - one using a goal-based approach and finding the nearest dirty cell, the other using a utility-based approach and finding the dirtiest cell.

Uploaded by

Muskan gupta
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/ 7

DFS:

nodes = int(input("Enter the number of nodes : "))


nums = []
for i in range(nodes):
row = input("Enter for node " + str(i) + " : ").split(' ')
nums.append(list(map(int, row)))
print("Given Adjacency List : \n")
print(nums)

visited=set()
def dfs(nums,visited,node):
if node not in visited:
print(node)
visited.add(node)
for neighbour in nums[node]:
dfs(nums,visited,neighbour)

dfs(nums,visited,0)

OUTPUT:
BFS:
graph = {
'0' : ['1','2'],
'1' : ['0','2', '3'],
'2' : ['1','1','4'],
'3' : ['2','1','4'],
'4' : ['3','5'],
'5' : ['4']
}
visited = []
queue = []

def bfs(visited, graph, node):


visited.append(node)
queue.append(node)
while queue:
m = queue.pop(0)
print (m, end = " ")
for neighbour in graph[m]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)

print("Following is the Breadth-First Search")


bfs(visited, graph, '5')

OUTPUT:
Vacuum Cleaner (GOAL BASED)
class VacuumCleaner:
def __init__(self, environment, initial_position=(0, 0)):
self.environment = environment
self.rows = len(environment)
self.cols = len(environment[0])
self.position = initial_position

def print_environment(self):
for row in self.environment:
print(row)

def clean(self, position):


if self.environment[position[0]][position[1]] == 1:
print("Cleaning cell at position", position)
self.environment[position[0]][position[1]] = 0
else:
print("Cell at position", position, "is already clean.")

def move(self, direction):


if direction == "UP" and self.position[0] > 0:
self.position = (self.position[0]-1, self.position[1])
print("Moving UP")
elif direction == "DOWN" and self.position[0] < self.rows-1:
self.position = (self.position[0]+1, self.position[1])
print("Moving DOWN")
elif direction == "LEFT" and self.position[1] > 0:
self.position = (self.position[0], self.position[1]-1)
print("Moving LEFT")
elif direction == "RIGHT" and self.position[1] < self.cols-1:
self.position = (self.position[0], self.position[1]+1)
print("Moving RIGHT")

def is_goal_reached(self):
return not any(1 in row for row in self.environment)

def find_nearest_dirty_cell(self):
dirty_cells = [(i, j) for i in range(self.rows) for j in range(self.cols) if self.environment[i][j] == 1]
if not dirty_cells:
return None
return min(dirty_cells, key=lambda loc: abs(loc[0]-self.position[0]) + abs(loc[1]-self.position[1]))

def clean_until_goal_reached(self):
while not self.is_goal_reached():
nearest_dirty_cell = self.find_nearest_dirty_cell()
if not nearest_dirty_cell:
print("No more dirty cells to clean.")
break
self.move_to_destination(nearest_dirty_cell)
self.clean(nearest_dirty_cell)

def move_to_destination(self, destination):


while self.position != destination:
if self.position[0] < destination[0]:
self.move("DOWN")
elif self.position[0] > destination[0]:
self.move("UP")
elif self.position[1] < destination[1]:
self.move("RIGHT")
elif self.position[1] > destination[1]:
self.move("LEFT")

# Get user input for environment and initial position


def get_environment():
rows = int(input("Enter the number of rows: "))
cols = int(input("Enter the number of columns: "))
environment = []
for i in range(rows):
row = []
for j in range(cols):
cell = int(input(f"Enter 0 for clean and 1 for dirty at position ({i}, {j}): "))
row.append(cell)
environment.append(row)
return environment

def get_initial_position():
row = int(input("Enter the initial row position: "))
col = int(input("Enter the initial column position: "))
return (row, col)

# Test the VacuumCleaner class


environment = get_environment()
initial_position = get_initial_position()
vacuum = VacuumCleaner(environment, initial_position)
print("Initial environment:")
vacuum.print_environment()
print("\nCleaning until goal is reached (ALL cells are clean):")

vacuum.clean_until_goal_reached()

OUTPUT:
Vacuum Cleaner(UTILITY BASED)
class VacuumCleaner:
def __init__(self, environment, initial_position=(0, 0)):
self.environment = environment
self.rows = len(environment)
self.cols = len(environment[0])
self.position = initial_position

def print_environment(self):
for row in self.environment:
print(row)

def clean(self, position):


if self.environment[position[0]][position[1]] > 0:
print("Cleaning cell at position", position)
self.environment[position[0]][position[1]] = 0
else:
print("Cell at position", position, "is already clean.")

def move(self, direction):


if direction == "UP" and self.position[0] > 0:
self.position = (self.position[0]-1, self.position[1])
print("Moving UP")
elif direction == "DOWN" and self.position[0] < self.rows-1:
self.position = (self.position[0]+1, self.position[1])
print("Moving DOWN")
elif direction == "LEFT" and self.position[1] > 0:
self.position = (self.position[0], self.position[1]-1)
print("Moving LEFT")
elif direction == "RIGHT" and self.position[1] < self.cols-1:
self.position = (self.position[0], self.position[1]+1)
print("Moving RIGHT")

def is_goal_reached(self):
return not any(self.environment[i][j] > 0 for i in range(self.rows) for j in range(self.cols))

def find_nearest_dirty_cell(self):
dirty_cells = [(i, j) for i in range(self.rows) for j in range(self.cols) if self.environment[i][j] > 0]
if not dirty_cells:
return None
return max(dirty_cells, key=lambda loc: self.environment[loc[0]][loc[1]])

def clean_until_goal_reached(self):
while not self.is_goal_reached():
nearest_dirty_cell = self.find_nearest_dirty_cell()
if not nearest_dirty_cell:
print("No more dirty cells to clean.")
break
self.move_to_destination(nearest_dirty_cell)
self.clean(nearest_dirty_cell)

def move_to_destination(self, destination):


while self.position != destination:
if self.position[0] < destination[0]:
self.move("DOWN")
elif self.position[0] > destination[0]:
self.move("UP")
elif self.position[1] < destination[1]:
self.move("RIGHT")
elif self.position[1] > destination[1]:
self.move("LEFT")

# Get user input for environment and initial position


def get_environment():
rows = int(input("Enter the number of rows: "))
cols = int(input("Enter the number of columns: "))
environment = []
for i in range(rows):
row = []
for j in range(cols):
dirt_level = int(input(f"Enter dirt level (0 for clean, 1-5 for dirty level) at position ({i}, {j}): "))
row.append(dirt_level)
environment.append(row)
return environment

def get_initial_position():
row = int(input("Enter the initial row position: "))
col = int(input("Enter the initial column position: "))
return (row, col)

# Test the VacuumCleaner class


environment = get_environment()
initial_position = get_initial_position()
vacuum = VacuumCleaner(environment, initial_position)
print("Initial environment:")
vacuum.print_environment()
print("\nCleaning until goal is reached (ALL cells are clean):")
vacuum.clean_until_goal_reached()

OUTPUT

You might also like