bfs in python 3

Solutions on MaxInterview for bfs in python 3 by the best coders in the world

showing results for - "bfs in python 3"
Luce
12 Jan 2020
1graph = {
2  'A' : ['B','C'],
3  'B' : ['D', 'E'],
4  'C' : ['F'],
5  'D' : [],
6  'E' : ['F'],
7  'F' : []
8}
9
10visited = [] # List to keep track of visited nodes.
11queue = []     #Initialize a queue
12
13def bfs(visited, graph, node):
14  visited.append(node)
15  queue.append(node)
16
17  while queue:
18    s = queue.pop(0) 
19    print (s, end = " ") 
20
21    for neighbour in graph[s]:
22      if neighbour not in visited:
23        visited.append(neighbour)
24        queue.append(neighbour)
25
26# Driver Code
27bfs(visited, graph, 'A')
Soren
12 Feb 2020
11
22
33
4