You are on page 1of 6

Name: Ayesha Siddiqa

Reg No: SP22-BSE-009


Subject: Intro to AI
Lab # 3
Submitted to: Mr. Waqas Ali
Activity 1:
class Node:

def __init__(self, state, parent, actions, totalCost):


self.state = state
self.parent = parent
self.actions = actions
self.totalCost = totalCost

graph = {'A': Node('A', None, ['B', 'C', 'E'], None),


'B': Node('B', None, ['A', 'D', 'E'], None),
'C': Node('C', None, ['A', 'F', 'G'], None),
'D': Node('D', None, ['B', 'E'], None),
'E': Node('E', None, ['A', 'B', 'D'], None),
'F': Node('F', None, ['C'], None),
'G': Node('G', None, ['C'], None)
}

print(graph)

Output :

Activity 2:
def BFS():

initialState = 'D'
goalState = 'F'

graph = {'A': Node('A', None, ['B', 'C', 'E'], None),


'B': Node('B', None, ['A', 'D', 'E'], None),
'C': Node('C', None, ['A', 'F', 'G'], None),
'D': Node('D', None, ['B', 'E'], None),
'E': Node('E', None, ['A', 'B', 'D'], None),
'F': Node('F', None, ['C'], None),
'G': Node('G', None, ['C'], None) }
frontier = [initialState]
explored = []

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)

def actionSequence(graph, initialState, goalState):


solution = [goalState]
currentParent = graph[goalState].parent
while currentParent != None:
solution.append(currentParent)
currentParent = graph[currentParent].parent

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)

def pathSequence(graph, initialState, goalState):


solution = [goalState]
currentParent = graph[goalState].parent
while currentParent != None:
solution.append(currentParent)
currentParent = graph[currentParent].parent

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']
]

def bfs(maze, start):


# Define the possible moves: up, down, left, right
moves = [(-1, 0), (1, 0), (0, -1), (0, 1)]

# Create a queue and add the start node to it


queue = [(start, [start])]

# Loop until the queue is empty


while queue:
# Get the next node to visit from the queue
node, path = queue.pop(0)
# If we have reached the end, return the path
if maze[node[0]][node[1]] == 'E':
return path

# Otherwise, try to move in each of the four possible directions


for move in moves:
x, y = node[0] + move[0], node[1] + move[1]

# 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)]))

# If we haven't found a path, return None


return None

# Find the path through the maze using BFS


path = bfs(maze, (2,2))

# Print the path


if path:
for node in path:
print(node)
else:
print("No path found!")

Output :

You might also like