Quote:Run the code as install to graph but python3.8 have not accept to install library for graph pip install graph what should to do.
Error:
ModuleNotFoundError: No module named 'graph'
class Graph:
def __init__(self):
self.vertices: list = []
self.adjacency_list: dict = {}
self.prev: dict = {}
self.distance: dict = {}
self.colors: dict = {}
self.entry: dict = {}
self.exit: dict = {}
self.time: int = 0
def add_vertex(self, label: str):
self.vertices.append(label)
self.adjacency_list[label]: list = []
self.prev[label] = None
self.distance[label] = 0
self.colors[label] = "white"
def add_edge(self, label1: str, label2: str):
self.adjacency_list[label1].append(label2)
self.adjacency_list[label2].append(label1)
def dfs(self, label: str):
self.colors[label] = "gray"
self.time += 1
self.entry[label] = self.time
for neighbour in self.adjacency_list[label]:
if self.colors[neighbour] == "white":
self.prev[neighbour] = label
self.distance[neighbour] = self.distance[label] + 1
self.dfs(neighbour)
self.colors[label] = "black"
self.time += 1
self.exit[label] = self.time
def return_path(self, label: str) -> str:
if self.prev[label] is None:
return label
else:
return self.return_path(self.prev[label]) + " -> " + label
from graph import Graph
graph = Graph()
my_vertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
# add vertices
for i in range(len(my_vertices)):
graph.add_vertex(my_vertices[i])
graph.add_edge('A', 'B')
graph.add_edge('A', 'C')
graph.add_edge('A', 'D')
graph.add_edge('C', 'D')
graph.add_edge('C', 'G')
graph.add_edge('D', 'G')
graph.add_edge('D', 'H')
graph.add_edge('B', 'E')
graph.add_edge('B', 'F')
graph.add_edge('E', 'I')
graph.dfs("A")
print(graph.return_path("H"))
Error:
Traceback (most recent call last):
File "D:/Python3.8.0/Python/Lib/WordFinder.py", line 43, in <module>
from graph import Graph
ModuleNotFoundError: No module named 'graph'