Skip to content

Instantly share code, notes, and snippets.

@rdisipio
Created May 11, 2021 21:45
Show Gist options
  • Save rdisipio/b99c59a38bc68442809f3709a4b2e8d6 to your computer and use it in GitHub Desktop.
Save rdisipio/b99c59a38bc68442809f3709a4b2e8d6 to your computer and use it in GitHub Desktop.
BFS
#!/usr/env/python
graph = {
'A' : ['B','C'],
'B' : ['D', 'E'],
'C' : ['F'],
'D' : [],
'E' : [],
'F' : []
}
visited = []
queue = []
def bfs(visited, graph, node):
visited.append(node)
queue.append(node)
while queue:
s = queue.pop(0)
print(s, end=" ")
for neighbour in graph[s]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
print("\n")
if __name__ == '__main__':
bfs(visited, graph, 'A')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment