Created
May 11, 2021 21:37
-
-
Save rdisipio/2aba5b6104d3063fefe5d5285eefe132 to your computer and use it in GitHub Desktop.
DFS
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 = set() | |
| def dfs(visited, graph, node): | |
| if node not in visited: | |
| print(node) | |
| visited.add(node) | |
| for neighbour in graph[node]: | |
| print(f"{node} ->", end=" ") | |
| dfs(visited, graph, neighbour) | |
| if __name__ == '__main__': | |
| dfs(visited, graph, 'A') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment