Lab Report 5: Abdullah Gondal
Lab Report 5: Abdullah Gondal
Name
ABDULLAH GONDAL
FA20-BEE-109
Registration Number
Class
BEE-5B
Instructor’s Name
SIR AZFAR
Activity 1:
Consider a toy problem that can be represented as a following graph. How would you
represent this graph in python?
Code:
class Node: def __init__(self, state, parent, actions,
total_cost):
self.state = state
self.parent = parent
self.actions = actions
self.total_cost = total_cost
Activity 2:
For the graph in previous activity, imagine node A as starting node and your goal is to reach F. Keeping
depth first search in mind, describe a sequence of actions that you must take to reach that goal state.
Code:
total_cost):
self.state = state
self.parent = parent
self.actions = actions
self.total_cost = total_cost
def DFS():
initialstate = 'A'
goalstate = 'D'
explored = []
frontier.pop(len(frontier)-1) print(current_node)
explored.append(current_node)
graph[child].parent = current_node
if graph[child].state == goalstate:
print(explored) return
frontier.append(child) if currentChildren==0:
del explored[len(explored)-1]
graph[goalstate].parent while
current_parent != None:
solution.append(current_parent)
current_parent =
graph[current_parent].parent
Output:
Activity 3:
Change initial state to D and set goal state as C. What will be resulting path of BFS search? What will be
the sequence of nodes explored?
Code:
total_cost):
self.state = state
self.parent = parent
self.actions = actions
action_sequence(graph,
initialstate, goalstate):
solution = [goalstate] current_parent =
None: solution.append(current_parent)
current_parent = graph[current_parent].parent
def BFS():
initialstate = 'D'
goalstate = 'C'
frontier = [initialstate]
explored = []
while len(frontier) != 0:
current_node = frontier.pop(0)
if graph[child].state == goalstate:
frontier.append(child)
Output:
Activity 4:
Imagine the same tree but this time we also mention the cost of each edge.
Implement a uniform cost solution to find the path from C to B.
Code:
(self,state,parent,actions,totalCost):
self.state = state
self.parent = parent
self.actions = actions
self.totalCost = totalCost
minV=math.inf
node='' for i in
frontier:
=i
return node
def actionSequence(graph,initialState,goalState):
None: solution.append(currentParent)
currentParent = graph[currentParent].parent
def UCS():
initialState = 'C'
goalState = 'B'
'B': Node ('B', None, [('A', 6), ('D',3), ('E', 4)], 0),
'E': Node ('E', None, [('A', 1), ('B',4), ('D',5), ('F',6)], 0),
'F': Node ('F', None, [('C',2), ('E', 6), ('D', 7)], 0),
[initialState] = (None, 0)
=0:
currentNode=findMin(frontier)
graph[currentNode].state==goalSt
ate:
explored.append(currentNode) for
child in graph[currentNode].actions:
else:
solution=UCS() print(solution)