Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

when runnung adjacencygraphlist.py: from Edge import Edge from DirectedGraph import DirectedGraph from AdjacencyListVertex import AdjacencyListVertex class AdjacencyListGraph ( DirectedGraph ) : def _ _ init

when runnung adjacencygraphlist.py:
from Edge import Edge
from DirectedGraph import DirectedGraph
from AdjacencyListVertex import AdjacencyListVertex
class AdjacencyListGraph(DirectedGraph):
def __init__(self):
self.vertices =[]
# Creates and adds a new vertex to the graph, provided a vertex with the
# same label doesn't already exist in the graph. Returns the new vertex on
# success, None on failure.
def add_vertex(self, new_vertex_label):
if new_vertex_label not in [vertex.get_label() for vertex in self.vertices]:
new_vertex = AdjacencyListVertex(new_vertex_label)
self.vertices.append(new_vertex)
return new_vertex
return None
# Adds a directed edge from the first to the second vertex. If the edge
# already exists in the graph, no change is made and False is returned.
# Otherwise the new edge is added and True is returned.
def add_directed_edge(self, from_vertex, to_vertex):
from_vertex_obj = self.get_vertex(from_vertex)
to_vertex_obj = self.get_vertex(to_vertex)
if from_vertex_obj is None:
from_vertex_obj = self.add_vertex(from_vertex)
if to_vertex_obj is None:
to_vertex_obj = self.add_vertex(to_vertex)
if to_vertex in from_vertex_obj.adjacent:
return False
from_vertex_obj.adjacent.append(to_vertex)
return True
# Returns a list of edges with the specified from_vertex.
def get_edges_from(self, from_vertex):
from_vertex_obj = self.get_vertex(from_vertex)
if from_vertex_obj is not None:
return [Edge(from_vertex, to_vertex) for to_vertex in from_vertex_obj.adjacent]
return []
# Returns a list of edges with the specified to_vertex.
def get_edges_to(self, to_vertex):
edges =[]
for vertex in self.vertices:
if to_vertex in vertex.adjacent:
edges.append(Edge(vertex.get_label(), to_vertex))
return edges
# Returns a vertex with a matching label, or None if no such vertex exists
def get_vertex(self, vertex_label):
for vertex in self.vertices:
if vertex.get_label()== vertex_label:
return vertex
return None
# Returns True if self graph has an edge from from_vertex to to_vertex
def has_edge(self, from_vertex, to_vertex):
from_vertex_obj = self.get_vertex(from_vertex)
if from_vertex_obj is not None:
return to_vertex in from_vertex_obj.adjacent
return False
# Returns a list of vertex labels
def get_vertex_labels(self):
return [vertex.get_label() for vertex in self.vertices]
, I am runnung into this error:
Traceback (most recent call last):
File "/usercode/zylab_unit_test_runner.py", line 76, in
passed = test_case.test_passed(writer)
File "/usercode/coding_rooms_unit_tests.py", line 50, in test_passed
return test.execute(test_feedback, GradingALGraph())
File "/usercode/DirectedGraphTest.py", line 13, in execute
if not command.execute(test_feedback, graph):
File "/usercode/Grading.py", line 31, in execute
Actual: [{','.join([vertex.get_label() for vertex in vertices])}]
TypeError: sequence item 5: expected str instance, AdjacencyListVertex found
How can I fix this?
here is adjacentlistvertex.py
from Vertex import Vertex
class AdjacencyListVertex(Vertex):
# List of vertices adjacent to this vertex. For each vertex V in this list, V is
# adjacent to this vertex, meaning an edge from this vertex to V exists in the graph.
def __init__(self, label):
super().__init__(label)
self.adjacent =[]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Find the area of the shaded region. 2=sin(20) Need Help? Read It

Answered: 1 week ago