-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs.py
31 lines (23 loc) · 840 Bytes
/
bfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from collections import deque
from cities import cities
# graph = list()
def bfs(start: str, finish: str):
visited, search_queue = set(), deque()
visited.add(start)
search_queue.append(start)
marked_vertexs = list()
graph = set()
while len(search_queue) > 0:
current_node = search_queue.popleft()
visited.add(current_node)
if current_node == finish:
return graph
for node in cities[current_node]:
if node["name"] not in visited:
if node["name"] not in marked_vertexs:
marked_vertexs.append(node["name"])
graph.add(current_node + " --> " + node["name"])
if node["name"] == finish:
return graph
search_queue.append(node["name"])
return graph