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 AdjacencyListGraphDirectedGraph:
def initself:
self.vertices
self.adjacencylist
# 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 addvertexself newvertexlabel:
if newvertexlabel in self.adjacencylist:
return None # Vertex already exists
newvertex AdjacencyListVertexnewvertexlabel
self.vertices.appendnewvertex
self.adjacencylistnewvertexlabel
return newvertex
# 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 adddirectededgeself fromvertex, tovertex:
if fromvertex not in self.adjacencylist or tovertex not in self.adjacencylist:
return False # One or both vertices do not exist
if tovertex in self.adjacencylistfromvertex:
return False # Edge already exists
self.adjacencylistfromvertexappendtovertex
return True
# Returns a list of edges with the specified fromvertex.
def getedgesfromself fromvertex:
if fromvertex not in self.adjacencylist:
return # No such vertex
fromvertexobj self.getvertexfromvertex
return Edgefromvertexobj, self.getvertextovertex for tovertex in self.adjacencylistfromvertex
# Returns a list of edges with the specified tovertex.
def getedgestoself tovertex:
if tovertex not in self.adjacencylist:
return # No such vertex
tovertexobj self.getvertextovertex
edgesto
for fromvertex in self.adjacencylist:
if tovertex in self.adjacencylistfromvertex:
edgestoappendEdgeselfgetvertexfromvertex tovertexobj
return edgesto
# Returns a vertex with a matching label, or None if no such vertex exists
def getvertexself vertexlabel:
for vertex in self.vertices:
if vertex.getlabel vertexlabel:
return vertex
return None
# Returns True if the graph has an edge from fromvertex to tovertex
def hasedgeself fromvertex, tovertex:
if fromvertex not in self.adjacencylist or tovertex not in self.adjacencylist:
return False
return tovertex in self.adjacencylistfromvertex
from Edge import Edge
from DirectedGraph import DirectedGraph
from AdjacencyListVertex import AdjacencyListVertex
class AdjacencyListGraphDirectedGraph:
def initself:
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 addvertexself newvertexlabel:
# 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 adddirectededgeself fromvertex, tovertex:
# Your code here remove placeholder line below
pass
# Returns a list of edges with the specified fromvertex.
def getedgesfromself fromvertex:
# Your code here remove placeholder line below
pass
# Returns a list of edges with the specified tovertex.
def getedgestoself tovertex:
# Your code here remove placeholder line below
pass
# Returns a vertex with a matching label, or None if no such vertex exists
def getvertexself vertexlabel:
# Your code here remove placeholder line below
pass
# Returns True if self graph has an edge from fromvertex to tovertex
def hasedgeself fromvertex, tovertex:
# Your code here remove placeholder line below
pass
this code is outputting: AdjacencyListGraph:
PASS: addvertexA returned a vertex
PASS: addvertexB returned a vertex
PASS: getvertexC returned None
PASS: getvertexA returned a vertex with a correct label
PASS: getvertexB returned a vertex with a correct label
PASS: getvertexE 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
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