Question
PLEASE HELP WHY IS THIS PYTHON CODE ERRORING? class Graph: Represents a directed graph using an adjacency list self.graph will be a dictionary containing
PLEASE HELP WHY IS THIS PYTHON CODE ERRORING?
class Graph:
"""
Represents a directed graph using an adjacency list
self.graph will be a dictionary containing each node in the graph as
a key with its value set to a list of all nodes adjacent to it
For example, if self.graph was { 1: [], 2: [3], 3: [1]}
Node 1 would have a path to no other node, Node 2 would have a
path to Node 3, and Node 3 would have a path to Node 1
"""
def __init__(self):
self.graph = {} # graph will initially be empty
def __repr__(self):
return str(self.graph)
def add_edge(self, node1, node2):
if node1 not in self.graph:
self.graph[node1] = []
self.graph[node1].append(node2)
def add_node(self, node1):
if node1 not in self.graph:
self.graph[node1] = []
def is_reachable(self,node1,visited, node_to_find):
# TODO: Complete this method so that it returns True if there is
# a path from node1 to node_to_find and False otherwise
if node1 in visited:
return False
visited.append(node1)
if node1 == node_to_find:
return True
if node1 not in self.graph:
return False
for neighbor in self.graph[node1]:
if self.is_reachable(neighbor, node_to_find, visited):
return True
return False
THESE ARE MY ERRORS
FAILED tests.py::test_traverse_recursively_is_recursive - TypeError: Node.traverse_recursively() missing 1 required positional argument: 'node' FAILED tests.py::test_traverse_recursively_prints_expected_output - TypeError: Node.traverse_recursively() missing 1 required positional argument: 'node' FAILED tests.py::test_traverse_recursively_prints_expected_one_node - TypeError: Node.traverse_recursively() missing 1 required positional argument: 'node' FAILED tests.py::test_is_reachable_adjacent_nodes - TypeError: Graph.is_reachable() missing 1 required positional argument: 'node_to_find' FAILED tests.py::test_is_reachable_one_direction_only - TypeError: Graph.is_reachable() missing 1 required positional argument: 'node_to_find' FAILED tests.py::test_is_reachable_multiple_steps - TypeError: Graph.is_reachable() missing 1 required positional argument: 'node_to_find' FAILED tests.py::test_is_reachable_cycle - TypeError: Graph.is_reachable() missing 1 required positional argument: 'node_to_find' FAILED tests.py::test_not_reachable - TypeError: Graph.is_reachable() missing 1 required positional argument: 'node_to_find'
THESE ARE MY TESTS
def setup_graph():
graph = Graph()
graph.add_edge(0, 1)
graph.add_edge(0, 2)
graph.add_edge(1, 2)
graph.add_edge(2, 0)
graph.add_edge(2, 3)
graph.add_node(5)
graph.add_edge(3, 5)
graph.add_node(4)
return graph
def test_is_reachable_adjacent_nodes():
graph = setup_graph()
assert graph.is_reachable(3, 5) is True
def test_is_reachable_one_direction_only():
graph = setup_graph()
assert graph.is_reachable(5, 3) is False
def test_is_reachable_multiple_steps():
graph = setup_graph()
assert graph.is_reachable(0, 5) is True
def test_is_reachable_cycle():
graph = setup_graph()
assert graph.is_reachable(3, 3) is True
def test_not_reachable():
graph = setup_graph()
assert graph.is_reachable(0, 4) is False
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