Professional Documents
Culture Documents
print(graph)
Output :
Activity 2:
def BFS():
initialState = 'D'
goalState = 'F'
while len(frontier) != 0:
currentNode = frontier.pop(0)
explored.append(currentNode)
for child in graph[currentNode].actions:
if child not in frontier and child not in explored:
graph[child].parent = currentNode
if graph[child].state == goalState:
return actionSequence(graph, initialState, goalState)
frontier.append(child)
solution.reverse()
return solution
solution = BFS()
print(solution)
Output:
Task 1:
class Node:
def __init__(self, state, parent, childs, totalCost):
self.state = state
self.parent = parent
self.childs = childs
self.totalCost = totalCost
map_romania = {'Arad': Node('Arad', None, {'Zerind':75,'Sibiu':140,'Timisoara':118},
0),
'Zerind': Node('Zerind', None,{'Arad':75,'Oradea':71}, 0),
'Oradea': Node('Oradea', None, {'Zerind':71,'Sibiu':151}, 0),
'Sibiu': Node('Sibiu', None, {'Arad':140,'Fagaras':99,'Rimnicu
Vilcea':80,'Oradea': 151}, 0),
'Timisoara': Node('Timisoara', None,{'Arad':118,'Lugoj':111} , 0),
'Lugoj': Node('Lugoj', None,{'Timisoara':111,'Mehadia':70}, 0),
'Mehadia': Node('Mehadia', None,{'Lugoj':70,'Drobeta':120}, 0),
'Rimnicu Vilcea': Node('Rimnicu Vilcea',None,{'Sibiu': 80, 'Pitesti':
97,'Craiova':146}, 0),
'Urziceni' : Node('Urziceni',None,{'Bucharest':85 , 'Hirsova':98 , 'Vaslui':
142},0),
'Vaslui' : Node('Vaslui',None,{'Urziceni': 142 , 'Lasi' : 92}, 0),
'Lasi' : Node('Lasi',None,{'Vaslui': 92 , 'Neamt':87}, 0),
'Hirsova' :Node('Hirsova',None,{'Urziceni' : 98 , 'Eforie' : 86}, 0),
'Drobeta':Node('Drobeta',None,{'Mehadia':75 , 'Craiova': 120}, 0),
'Craiova':Node('Craiova',None,{'Drobeta':120 , 'Pitesti':138, 'Rimnicu
Vilcea' : 146}, 0),
'Pitesti':Node('Pitesti',None,{'Rimnicu Vilcea':97 , 'Craiova' : 138}, 0),
'Fagaras':Node('Fagaras',None,{'Sibiu':99, 'Bucharest':211}, 0),
'Bucharest':Node('Bucharest',None,{'Fagaras':99 , 'Giurgiu':90 , 'Urziceni' :
85 , 'Pitesti' : 101}, 0),
'Eforie': Node('Eforie',None,{'Hirsova': 86},0),
'Neamt': Node('Neamt',None,{'Iasi': 87},0),
'Giurgiu': Node('Giurgiu',None,{'Bucharest':90},0)
}
# Graded ACTIVITY 1
def BFS(graph,startnode,goalnode):
initialState = startnode
goalState = goalnode
frontier = [initialState]
explored = []
while len(frontier) != 0:
currentNode = frontier.pop(0)
explored.append(currentNode)
for child in graph[currentNode].childs:
if child not in frontier and child not in explored:
graph[child].parent = currentNode
if graph[child].state == goalState:
return pathSequence(graph, initialState, goalState)
frontier.append(child)
solution.reverse()
return solution
solution = BFS(map_romania,'Arad','Bucharest')
print("path from arad to bucharest is:",solution)
Output :
Task 2:
maze = [
['S', '.', '.', '.', '.'],
['.', '.', '.', 'B', '.'],
['.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.'],
['.', '.', '.', '.', 'E']
]
# Check if the move is valid (i.e., within the maze and not a wall)
if 0 <= x < len(maze) and 0 <= y < len(maze[0]) and maze[x][y] != '#':
# If we haven't visited this node before, add it to the queue
if (x, y) not in path:
queue.append(((x, y), path + [(x, y)]))
Output :