From 6301018adc11715d09e24cb5e1799151f75fb839 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Thu, 11 Jan 2024 18:54:08 +0000 Subject: [PATCH] use set for visited --- chapter6/ex6.1.py | 4 ++-- chapter6/ex6.2.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/chapter6/ex6.1.py b/chapter6/ex6.1.py index 52e9b53..88b0e06 100644 --- a/chapter6/ex6.1.py +++ b/chapter6/ex6.1.py @@ -21,7 +21,7 @@ graph = { "f": [], } -visited = [] +visited = set() def bfs(): @@ -36,7 +36,7 @@ def bfs(): if current_node.name in visited: continue - visited.append(current_node.name) + visited.add(current_node.name) next_nodes = graph[current_node.name] for node in next_nodes: diff --git a/chapter6/ex6.2.py b/chapter6/ex6.2.py index cebf63d..c2f74b7 100644 --- a/chapter6/ex6.2.py +++ b/chapter6/ex6.2.py @@ -21,7 +21,7 @@ graph = { "bat": [], } -visited = [] +visited = set() def bfs(): @@ -36,7 +36,7 @@ def bfs(): if current_node.name in visited: continue - visited.append(current_node.name) + visited.add(current_node.name) next_nodes = graph[current_node.name] for node in next_nodes: