Created
July 11, 2021 04:43
-
-
Save 201411108/c2461e8f62f90ce7845195f4029d5244 to your computer and use it in GitHub Desktop.
BFS(queue)_python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bfs(start_v): | |
discovered = [start_v] | |
queue = deque() | |
queue.append(start_v) | |
while queue: | |
v = queue.popleft() | |
for w in graph[v]: | |
if w not in discovered: | |
discovered.append(w) | |
queue.append(w) | |
return discovered | |
# adjacency list | |
graph = { | |
1: [2, 3, 4], | |
2: [5], | |
3: [5], | |
4: [], | |
5: [6, 7], | |
6: [], | |
7: [3], | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment