Question: import sys from AdjacencyListGraph import AdjacencyListGraph from AdjacencyMatrixGraph import AdjacencyMatrixGraph from GraphCommands import * def main ( ) : graph 1 = AdjacencyListGraph ( )
import sys
from AdjacencyListGraph import AdjacencyListGraph
from AdjacencyMatrixGraph import AdjacencyMatrixGraph
from GraphCommands import
def main:
graph AdjacencyListGraph
graph AdjacencyMatrixGraph
# Test AdjacencyListGraph first
printAdjacencyListGraph:
adjpass testgraphsysstdout, graph
print
# Test AdjacencyMatrixGraph second
printAdjacencyMatrixGraph:
matpass testgraphsysstdout, graph
printf
Summary:
AdjacencyListGraph: PASS if adjpass else "FAIL"
AdjacencyMatrixGraph: PASS if matpass else "FAIL"
def testgraphtestfeedback, graph:
commands
AddVertexCommandA True
AddVertexCommandB True
# exist, C D E
GetVertexCommandC False
GetVertexCommandA True
GetVertexCommandB True
GetVertexCommandE False
GetVertexCommandD False
# vertices
AddVertexCommandC True
AddVertexCommandD True
AddVertexCommandE True
# edges
AddEdgeCommandBC True
AddEdgeCommandCA True
AddEdgeCommandCD True
AddEdgeCommandCE True
AddEdgeCommandDC True
AddEdgeCommandEA True
AddEdgeCommandED True
# fail
AddEdgeCommandCE False
AddEdgeCommandDC False
VerifyEdgesFromCommandA
VerifyEdgesFromCommandBC
VerifyEdgesFromCommandCADE
VerifyEdgesFromCommandDC
VerifyEdgesFromCommandEAD
VerifyEdgesToCommandACE
VerifyEdgesToCommandB
VerifyEdgesToCommandCBD
VerifyEdgesToCommandDCE
VerifyEdgesToCommandEC
# edges
HasEdgeCommandAC False
HasEdgeCommandAE False
HasEdgeCommandBC True
HasEdgeCommandCA True
HasEdgeCommandCB False
HasEdgeCommandCD True
HasEdgeCommandCE True
HasEdgeCommandDC True
HasEdgeCommandEA True
HasEdgeCommandEC False
HasEdgeCommandED True
# command, fails
for command in commands:
if not command.executetestfeedback, graph:
return False
return True
if namemain:
main
class Vertex:
def initself vertexlabel:
self.vertexlabel vertexlabel
def setlabelself newlabel:
self.vertexlabel newlabel
def getlabelself:
return self.vertexlabel
# Prints self vertex's label
def printself output:
printselfvertexlabel, fileoutput, end
from Vertex import Vertex
class Edge:
def initself fromvertex, tovertex:
self.fromvertex fromvertex
self.tovertex tovertex
def equalsself other:
return selffromvertex other.fromvertex and
self.tovertex other.tovertex
# Prints self edge in the form A to B where A is the fromvertex's label
# and B is the tovertex's label.
def printself output:
self.fromvertex.printoutput
print to fileoutput, end
self.tovertex.printoutput
from abc import ABC, abstractmethod
# class for a directed, unweighted graph
class DirectedGraph:
# 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:
self.newvertexlabel newvertexlabel
# Adds a directed edge from the first to the second vertex. No change is made
# and false is returned if the edge already exists in the graph. Otherwise the
# new edge is added and True is returned.
@abstractmethod
def adddirectededgeself fromvertex, tovertex:
pass
# Returns a list of edges with the specified fromvertex.
@abstractmethod
def getedgesfromfromvertex:
pass
# Returns a list of edges with the specified tovertex
@abstractmethod
def getedgestotovertex:
pass
# Returns a vertex with a matching label, or None if no such vertex exists
@abstractmethod
def getvertexself vertexlabel:
pass
# Returns True if this graph has an edge from fromvertex to tovertex, False otherwise
@abstractmethod
def hasedgeself fromvertex, tovertex:
pass
from Vertex import Vertex
class AdjacencyListVertexVertex:
# 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 initself label:
superinitlabel
self.adjacent
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.
# Other
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
