Skip to content

Instantly share code, notes, and snippets.

@rdisipio
Created May 11, 2021 21:37
Show Gist options
  • Select an option

  • Save rdisipio/2aba5b6104d3063fefe5d5285eefe132 to your computer and use it in GitHub Desktop.

Select an option

Save rdisipio/2aba5b6104d3063fefe5d5285eefe132 to your computer and use it in GitHub Desktop.
DFS
#!/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