How can I get just a distance(number of edges) between any two nodes of the graph using BFS algorithms?
I do not want to save the path information as a list (like the code below) to decrease the runtime of the code. (for better performance)
def check_distance(self, satrt, end, max_distance):
queue = deque([start])
while queue:
path = queue.popleft()
node = path[-1]
if node == end:
return len(path)
elif len(path) > max_distance:
return False
else:
for adjacent in self.graph.get(node, []):
queue.append(list(path) + [adjacent])