Question: from Edge import Edge from DirectedGraph import DirectedGraph from AdjacencyListVertex import AdjacencyListVertex class AdjacencyListGraph ( DirectedGraph ) : def _ _ init _ _ (

from Edge import Edge
from DirectedGraph import DirectedGraph
from AdjacencyListVertex import AdjacencyListVertex
class AdjacencyListGraph(DirectedGraph):
def __init__(self):
self.vertices =[]
self.adjacency_list ={}
# 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 in self.adjacency_list:
return None # Vertex already exists
new_vertex = AdjacencyListVertex(new_vertex_label)
self.vertices.append(new_vertex)
self.adjacency_list[new_vertex_label]=[]
return new_vertex
# 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):
if from_vertex not in self.adjacency_list or to_vertex not in self.adjacency_list:
return False # One or both vertices do not exist
if to_vertex in self.adjacency_list[from_vertex]:
return False # Edge already exists
self.adjacency_list[from_vertex].append(to_vertex)
return True
# Returns a list of edges with the specified from_vertex.
def get_edges_from(self, from_vertex):
if from_vertex not in self.adjacency_list:
return [] # No such vertex
from_vertex_obj = self.get_vertex(from_vertex)
return [Edge(from_vertex_obj, self.get_vertex(to_vertex)) for to_vertex in self.adjacency_list[from_vertex]]
# Returns a list of edges with the specified to_vertex.
def get_edges_to(self, to_vertex):
if to_vertex not in self.adjacency_list:
return [] # No such vertex
to_vertex_obj = self.get_vertex(to_vertex)
edges_to =[]
for from_vertex in self.adjacency_list:
if to_vertex in self.adjacency_list[from_vertex]:
edges_to.append(Edge(self.get_vertex(from_vertex), to_vertex_obj))
return edges_to
# 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 the graph has an edge from from_vertex to to_vertex
def has_edge(self, from_vertex, to_vertex):
if from_vertex not in self.adjacency_list or to_vertex not in self.adjacency_list:
return False
return to_vertex in self.adjacency_list[from_vertex]
'''
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):
# Your code here (remove placeholder line below)
pass
# 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):
# Your code here (remove placeholder line below)
pass
# Returns a list of edges with the specified from_vertex.
def get_edges_from(self, from_vertex):
# Your code here (remove placeholder line below)
pass
# Returns a list of edges with the specified to_vertex.
def get_edges_to(self, to_vertex):
# Your code here (remove placeholder line below)
pass
# Returns a vertex with a matching label, or None if no such vertex exists
def get_vertex(self, vertex_label):
# Your code here (remove placeholder line below)
pass
# Returns True if self graph has an edge from from_vertex to to_vertex
def has_edge(self, from_vertex, to_vertex):
# Your code here (remove placeholder line below)
pass
''' this code is outputting: AdjacencyListGraph:
PASS: add_vertex("A") returned a vertex
PASS: add_vertex("B") returned a vertex
PASS: get_vertex("C") returned None
PASS: get_vertex("A") returned a vertex with a correct label
PASS: get_vertex("B") returned a vertex with a correct label
PASS: get_vertex("E") returned None please modify adjacencylist.graph for it to pass these test. here isdirectggraph.py:

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!