Aiml
Aiml
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 = []
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 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 get_initial_position():
row = int(input("Enter the initial row position: "))
col = int(input("Enter the initial column position: "))
return (row, col)
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 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 get_initial_position():
row = int(input("Enter the initial row position: "))
col = int(input("Enter the initial column position: "))
return (row, col)
OUTPUT