COMSATS University Islamabad, Lahore Campus: Lab Assignment 1 Statement - FALL 2021
COMSATS University Islamabad, Lahore Campus: Lab Assignment 1 Statement - FALL 2021
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 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}')
class Graph:
def __init__(self):
self.graph = []
self.edgeIndexs = []
self.edges = []
self.graphDictonary = {}
def showGraph(self):
for edge in self.edges:
edge.toString()
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