0% found this document useful (0 votes)
55 views10 pages

Lab Report 5: Abdullah Gondal

The document contains a lab report submitted by Abdullah Gondal. It includes 4 activities involving representing a graph as nodes in Python and performing depth-first search, breadth-first search, and uniform cost search on the graph to find paths between different nodes. Code implementations and outputs are provided for each activity.

Uploaded by

Abdullah Gondal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views10 pages

Lab Report 5: Abdullah Gondal

The document contains a lab report submitted by Abdullah Gondal. It includes 4 activities involving representing a graph as nodes in Python and performing depth-first search, breadth-first search, and uniform cost search on the graph to find paths between different nodes. Code implementations and outputs are provided for each activity.

Uploaded by

Abdullah Gondal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Lab Report 5

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

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

'B': Node('B', None, ['D', 'E', 'A'], 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)}

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:

class Node: def __init__(self, state, parent, actions,

total_cost):

self.state = state

self.parent = parent

self.actions = actions

self.total_cost = total_cost

def DFS():

initialstate = 'A'

goalstate = 'D'

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

'B': Node('B', None, ['D', 'E', 'A'], 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: current_node =

frontier.pop(len(frontier)-1) print(current_node)

explored.append(current_node)

currentChildren=0 for child in

graph[current_node].actions: if child not in

frontier and child not in explored:

graph[child].parent = current_node

if graph[child].state == goalstate:

print(explored) return

action_sequence(graph, initialstate, goalstate)

frontier.append(child) if currentChildren==0:

del explored[len(explored)-1]

def action_sequence(graph, initialstate, goalstate):

solution = [goalstate] current_parent =

graph[goalstate].parent while

current_parent != None:

solution.append(current_parent)

current_parent =

graph[current_parent].parent

solution.reverse() return solution


solution = DFS() print(solution)

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:

class Node: def __init__(self, state, parent, actions,

total_cost):

self.state = state

self.parent = parent

self.actions = actions

self.total_cost = total_cost def

action_sequence(graph,

initialstate, goalstate):
solution = [goalstate] current_parent =

graph[goalstate].parent while current_parent !=

None: solution.append(current_parent)

current_parent = graph[current_parent].parent

solution.reverse() return solution

def BFS():

initialstate = 'D'

goalstate = 'C'

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

'B': Node('B', None, ['D', 'E', 'A'], 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:

current_node = frontier.pop(0)

explored.append(current_node) for child in

graph[current_node].actions: if child not in

frontier and child not in explored:


graph[child].parent = current_node

if graph[child].state == goalstate:

return action_sequence(graph, initialstate, goalstate)

frontier.append(child)

solution = BFS() print(solution)

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:

import math class Node: def init

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

self.state = state

self.parent = parent

self.actions = actions

self.totalCost = totalCost

def findMin (frontier):

minV=math.inf

node='' for i in

frontier:

if minV>frontier [i] [1]:

minV=frontier [i] [1] node

=i

return node
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

def UCS():
initialState = 'C'

goalState = 'B'

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

'B': Node ('B', None, [('A', 6), ('D',3), ('E', 4)], 0),

'C': Node ('c', None, [('A',9), ('F',2), ('G',3)], 0),

'D': Node ('D', None, [('B',3), ('E',5), ('F',7)], 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),

'G': Node ('G', None, [('C',3)], 0)}

frontier = dict() frontier

[initialState] = (None, 0)

explored=[] while len(frontier)!

=0:

currentNode=findMin(frontier)

del frontier [currentNode] if

graph[currentNode].state==goalSt

ate:

return actionSequence(graph, initialState, goalState)

explored.append(currentNode) for

child in graph[currentNode].actions:

currentCost=child [1] + graph[currentNode].totalCost

if child[0] not in frontier and child [0] not in explored:


graph [child [0]].parent=currentNode graph [child

[0]].totalCost=currentCost frontier [child[0]]= (graph [child[0]].parent,

graph [child [0]].totalCost) elif child[0] in frontier: if frontier

[child[0]] [1] < currentCost:

graph [child[0]].parent=frontier [child[0]] [0]

graph [child[0]].totalCost=frontier [child[0]] [1]

else:

frontier [child [0]]= (currentNode, currentCost)

graph [child [0]].parent=frontier [child[0]] [0]

graph [child [0]]. totalCost=frontier [child[0]] [1]

solution=UCS() print(solution)

You might also like