Question
!!PLEASE COMPLETE THE CODE SNIPPIT BELOW IN PYTHON WITH THE TODO MARKER!! class Graph: Represents a directed graph using an adjacency list self.graph will
!!PLEASE COMPLETE THE CODE SNIPPIT BELOW IN PYTHON WITH THE TODO MARKER!!
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, node_to_find, visited):
# TODO: Complete this method so that it returns True if there is
# a path from node1 to node_to_find and False otherwise
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