diff --git a/chapter6/ex6.1.py b/chapter6/ex6.1.py index c2d9adc..52e9b53 100644 --- a/chapter6/ex6.1.py +++ b/chapter6/ex6.1.py @@ -21,6 +21,8 @@ graph = { "f": [], } +visited = [] + def bfs(): queue = deque() @@ -32,6 +34,10 @@ def bfs(): if current_node.name == "f": return current_node.distance + if current_node.name in visited: + continue + visited.append(current_node.name) + next_nodes = graph[current_node.name] for node in next_nodes: node.distance = current_node.distance + 1