Skip to content

Instantly share code, notes, and snippets.

@201411108
Created July 11, 2021 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 201411108/c2461e8f62f90ce7845195f4029d5244 to your computer and use it in GitHub Desktop.
Save 201411108/c2461e8f62f90ce7845195f4029d5244 to your computer and use it in GitHub Desktop.
BFS(queue)_python
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