Question: Given the python code below, find the output of the print _ graph function for the following graph: # implementation of an undirected graph using

Given the python code below, find the output of the "print_graph" function for the following graph:
# implementation of an undirected graph using Adjacency Matrix, with weighted or unweighted edges
# its definitely work
class Vertex:
def __init__(self, n):
self.name = n
class Graph:
vertices ={}
edges =[]
edge_indices ={}
def add_vertex(self, vertex):
if isinstance(vertex, Vertex) and vertex.name not in self.vertices:
self.vertices[vertex.name]= vertex
for row in self.edges:
row.append(0)
self.edges.append([0]*(len(self.edges)+1))
self.edge_indices[vertex.name]= len(self.edge_indices)
return True
else:
return False
def add_edge(self, u, v, weight=1):
if u in self.vertices and v in self.vertices:
self.edges[self.edge_indices[u]][self.edge_indices[v]]= weight
self.edges[self.edge_indices[v]][self.edge_indices[u]]= weight
return True
else:
return False
def print_graph(self):
for v, i in sorted(self.edge_indices.items()):
print(v +'', end='')
for j in range(len(self.edges)):
print(self.edges[i][j], end='')
print('')
g = Graph()
# print(str(len(g.vertices)))
a = Vertex('A')
g.add_vertex(a)
g.add_vertex(Vertex('B'))
for i in range(ord('A'), ord('K')):
g.add_vertex(Vertex(chr(i)))
edges =['AB','AE','BF','CG','DE','DH','EH','FG','FI','FJ','GJ','HI']
for edge in edges:
g.add_edge(edge[:1], edge[1:])
g.print_graph()Graph
 Given the python code below, find the output of the "print_graph"

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!