mirror of
https://github.com/onyx-and-iris/grokking-algorithms.git
synced 2024-11-15 17:30:52 +00:00
add visited array
This commit is contained in:
parent
ecf623c2c4
commit
10914ee208
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user