use set for visited

This commit is contained in:
onyx-and-iris 2024-01-11 18:54:08 +00:00
parent 10914ee208
commit 6301018adc
2 changed files with 4 additions and 4 deletions

View File

@ -21,7 +21,7 @@ graph = {
"f": [], "f": [],
} }
visited = [] visited = set()
def bfs(): def bfs():
@ -36,7 +36,7 @@ def bfs():
if current_node.name in visited: if current_node.name in visited:
continue continue
visited.append(current_node.name) visited.add(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:

View File

@ -21,7 +21,7 @@ graph = {
"bat": [], "bat": [],
} }
visited = [] visited = set()
def bfs(): def bfs():
@ -36,7 +36,7 @@ def bfs():
if current_node.name in visited: if current_node.name in visited:
continue continue
visited.append(current_node.name) visited.add(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: