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": [],
|
"f": [],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
visited = []
|
||||||
|
|
||||||
|
|
||||||
def bfs():
|
def bfs():
|
||||||
queue = deque()
|
queue = deque()
|
||||||
@ -32,6 +34,10 @@ def bfs():
|
|||||||
if current_node.name == "f":
|
if current_node.name == "f":
|
||||||
return current_node.distance
|
return current_node.distance
|
||||||
|
|
||||||
|
if current_node.name in visited:
|
||||||
|
continue
|
||||||
|
visited.append(current_node.name)
|
||||||
|
|
||||||
next_nodes = graph[current_node.name]
|
next_nodes = graph[current_node.name]
|
||||||
for node in next_nodes:
|
for node in next_nodes:
|
||||||
node.distance = current_node.distance + 1
|
node.distance = current_node.distance + 1
|
||||||
|
Loading…
Reference in New Issue
Block a user