Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
selfget
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
selfget
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
FAIL: ADD from B to "Cplease modify adjacencylist.graph for it to pass these test. here isdirctggraph.py:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started