0% found this document useful (0 votes)
84 views3 pages

COMSATS University Islamabad, Lahore Campus: Lab Assignment 1 Statement - FALL 2021

This document contains instructions for a lab assignment in an Artificial Intelligence course. Students are asked to complete three tasks: [1] Traverse the nodes of a graph using breadth-first search, [2] Create a function to show the graph's nodes and relationships, and [3] Create a function to show the nodes related by a given relationship. Python code is provided to define classes for graphs, nodes, and edges to represent the given diagrammatic data as a graph.

Uploaded by

Tech Trop
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)
84 views3 pages

COMSATS University Islamabad, Lahore Campus: Lab Assignment 1 Statement - FALL 2021

This document contains instructions for a lab assignment in an Artificial Intelligence course. Students are asked to complete three tasks: [1] Traverse the nodes of a graph using breadth-first search, [2] Create a function to show the graph's nodes and relationships, and [3] Create a function to show the nodes related by a given relationship. Python code is provided to define classes for graphs, nodes, and edges to represent the given diagrammatic data as a graph.

Uploaded by

Tech Trop
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/ 3

COMSATS University Islamabad, Lahore Campus

Lab Assignment 1 Statement - FALL 2021


Course Title: Artificial Intelligence-Lab Course Code: CSC- Credit Hours: 3(2,1)
Course Dr. Atifa Athar Programme BS Computer Science
Instructor/s: Name:
Semester: 6th Batch: SP19 Section: C Date : 10/10/2021
Due Date: 17/10/2021 Maximum Marks: 10
Student’s S.M. Haseeb Iqbal Reg. No. SP19-BCS-042

Question 1: Make the graph for the following diagram in “Python” Make sure to make classes
of graph, nodes, edges. You have to do:
1. Traverse all nodes using BFS traversal method
2. Make a function show_graph() which will show simply nodes values and relationship name.
3. Make a function show_graphbyrelationship() which will show the relationship and then all
nodes having that relation. 

class Nodes:
neighbour = []
def __init__(self, node):
self.vertex = node
self.neighbours = []

def addNeighbour(self, neighbour):


self.neighbours.append(neighbour)

def toString(self):
print(f'{self.vertex}: {self.neighbours}')

class Edges:
def __init__(self, node, neighbour):
self.node = node
self.neighbour = neighbour[0]
self.relation = neighbour[1]

def toString(self):
print(f'{self.node} and {self.neighbour} relation is {self.relation}')

from Edges import Edges


from Nodes import Nodes

class Graph:
def __init__(self):
self.graph = []
self.edgeIndexs = []
self.edges = []
self.graphDictonary = {}

def insertNode(self,root, neighbours):


node = Nodes(root)
self.graphDictonary[root] = []
for selected in neighbours:
node.addNeighbour(selected[0])
self.graphDictonary[root].append(selected[0])
edge = Edges(root, selected)
self.edges.append(edge)
self.graph.append(node)

def showGraph(self):
for edge in self.edges:
edge.toString()

def showbyrelation(self, relation):


for edge in self.edges:
if relation == edge.relation:
edge.toString()
def BFS(self):
queue = []
queue.append(self.graph[0].vertex)
while True:
vertex = queue.pop()
print(f' {vertex}')
for node in self.graph:
if vertex == node.vertex:
for neighbour in node.neighbours:
queue.append(neighbour)
if len(queue) == 0:
break
print(' |')
print(' v')
print()

from Graph import Graph

root = 'philip'
neighbours = [['Philip',[['Terrence', 'friends'],['Jeap', 'owns']]],
['Terrence',[['Guy', 'friends'],['Buddy', 'friends']]]
,['Guy', []], ['Buddy',[['Jeap', 'rents']]], ['Jeap',[]]]

g = Graph()
for data in neighbours:
g.insertNode(data[0], data[1])
g.showGraph()

print()
g.showbyrelation('friends')
print(g.graphDictonary)

g.BFS()

Q1
Q2

Q3

You might also like