Created
May 11, 2021 21:45
-
-
Save rdisipio/b99c59a38bc68442809f3709a4b2e8d6 to your computer and use it in GitHub Desktop.
BFS
This file contains hidden or 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
#!/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