Skip to content

Instantly share code, notes, and snippets.

@201411108
Last active July 11, 2021 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 201411108/ea4d5729ac2bc93a2be451feda38ae42 to your computer and use it in GitHub Desktop.
Save 201411108/ea4d5729ac2bc93a2be451feda38ae42 to your computer and use it in GitHub Desktop.
DFS(recursive)_python
def recursive_dfs(v, discovered=[]):
discovered.append(v)
for w in graph[v]:
if w not in discovered:
discovered = recursive_diff(w, discovered)
return discovered
# adjacency list
graph = {
1: [2, 3, 4],
2: [5],
3: [5],
4: [],
5: [6, 7],
6: [],
7: [3],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment