Skip to main content
Tweeted twitter.com/StackCodeReview/status/1270506045417422851
Question Protected by Jamal
edited tags
Link
Apollo
  • 363
  • 1
  • 3
  • 5
Source Link
Apollo
  • 363
  • 1
  • 3
  • 5

BFS Implementation in Python 3

def bfs(graph, root):
    visited, queue = [], [root]
    while queue:
        vertex = queue.pop(0)
        for w in graph[vertex]:
            if w not in visited:
                visited.append(w)
                queue.append(w)

graph = {0: [1, 2], 1: [2], 2: []}
bfs(graph, 0)

Does this look like a correct implementation of BFS in Python 3?