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():
graph1= AdjacencyListGraph()
graph2= AdjacencyMatrixGraph()
# Test AdjacencyListGraph first
print("AdjacencyListGraph: ")
adj_pass = test_graph(sys.stdout, graph1)
print()
# Test AdjacencyMatrixGraph second
print("AdjacencyMatrixGraph: ")
mat_pass = test_graph(sys.stdout, graph2)
print(f"""
Summary:
AdjacencyListGraph: {"PASS" if adj_pass else "FAIL"}
AdjacencyMatrixGraph: {"PASS" if mat_pass else "FAIL"}""",
)
def test_graph(test_feedback, graph):
commands =(
AddVertexCommand("A", True),
AddVertexCommand("B", True),
# exist, C, D, E
GetVertexCommand("C", False),
GetVertexCommand("A", True),
GetVertexCommand("B", True),
GetVertexCommand("E", False),
GetVertexCommand("D", False),
# vertices
AddVertexCommand("C", True),
AddVertexCommand("D", True),
AddVertexCommand("E", True),
# edges
AddEdgeCommand("B","C", True),
AddEdgeCommand("C","A", True),
AddEdgeCommand("C","D", True),
AddEdgeCommand("C","E", True),
AddEdgeCommand("D","C", True),
AddEdgeCommand("E","A", True),
AddEdgeCommand("E","D", True),
# fail
AddEdgeCommand("C","E", False),
AddEdgeCommand("D","C", False),
VerifyEdgesFromCommand("A",()),
VerifyEdgesFromCommand("B",("C")),
VerifyEdgesFromCommand("C",("A","D","E")),
VerifyEdgesFromCommand("D",("C")),
VerifyEdgesFromCommand("E",("A","D")),
VerifyEdgesToCommand("A",("C","E")),
VerifyEdgesToCommand("B",()),
VerifyEdgesToCommand("C",("B","D")),
VerifyEdgesToCommand("D",("C","E")),
VerifyEdgesToCommand("E",("C")),
# edges
HasEdgeCommand("A","C", False),
HasEdgeCommand("A","E", False),
HasEdgeCommand("B","C", True),
HasEdgeCommand("C","A", True),
HasEdgeCommand("C","B", False),
HasEdgeCommand("C","D", True),
HasEdgeCommand("C","E", True),
HasEdgeCommand("D","C", True),
HasEdgeCommand("E","A", True),
HasEdgeCommand("E","C", False),
HasEdgeCommand("E","D", True)
)
# command, fails
for command in commands:
if not command.execute(test_feedback, graph):
return False
return True
if __name__=='__main__':
main()
class Vertex:
def __init__(self, vertex_label):
self.vertex_label = vertex_label
def set_label(self, new_label):
self.vertex_label = new_label
def get_label(self,):
return self.vertex_label
# Prints self vertex's label
def print(self, output):
print(self.vertex_label, file=output, end='')
from Vertex import Vertex
class Edge:
def __init__(self, from_vertex, to_vertex):
self.from_vertex = from_vertex
self.to_vertex = to_vertex
def equals(self, other):
return (self.from_vertex == other.from_vertex and
self.to_vertex == other.to_vertex)
# Prints self edge in the form "A to B", where "A" is the from_vertex's label
# and "B" is the to_vertex's label.
def print(self, output):
self.from_vertex.print(output)
print(" to ", file=output, end='')
self.to_vertex.print(output)
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 add_vertex(self, new_vertex_label):
self.new_vertex_label = new_vertex_label
# 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 add_directed_edge(self, from_vertex, to_vertex):
pass
# Returns a list of edges with the specified from_vertex.
@abstractmethod
def get_edges_from(from_vertex):
pass
# Returns a list of edges with the specified to_vertex
@abstractmethod
def get_edges_to(to_vertex):
pass
# Returns a vertex with a matching label, or None if no such vertex exists
@abstractmethod
def get_vertex(self, vertex_label):
pass
# Returns True if this graph has an edge from from_vertex to to_vertex, False otherwise
@abstractmethod
def has_edge(self, from_vertex, to_vertex):
pass
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 =[]
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.
# Other
import sys from AdjacencyListGraph import

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 Accounting Questions!