Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Make a python implementation in 'DFS' (depth first search) below to find the path, if no path, should return an empty list. Will rate. from
Make a python implementation in 'DFS' (depth first search) below to find the path, if no path, should return an empty list. Will rate.
from collections import deque class Graph: def __init__(self, n): self.adj = {} for i in range(n): self.adj[i] = [] def are_connected(self, node1, node2): return node2 in self.adj[node1] def adjacent_nodes(self, node): return self.adj[node] def add_node(self): self.adj[len(self.adj)] = [] def add_edge(self, node1, node2): if node1 not in self.adj[node2]: self.adj[node1].append(node2) self.adj[node2].append(node1) def number_of_nodes(): return len()
def DFS(G, node1, node2): S = [node1] marked = {} for node in G.adj: marked[node] = False while len(S) != 0: current_node = S.pop() if not marked[current_node]: print("Visiting node:" + str(current_node)) marked[current_node] = True for node in G.adj[current_node]: if node == node2: return True S.append(node) return False
G = Graph(10) G.add_edge(0, 1) G.add_edge(0, 4) G.add_edge(0, 7) G.add_edge(1, 2) G.add_edge(2, 3) G.add_edge(4, 5) G.add_edge(5, 6) G.add_edge(7, 8) G.add_edge(8, 9)
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