Last active
July 11, 2021 04:40
-
-
Save 201411108/ea4d5729ac2bc93a2be451feda38ae42 to your computer and use it in GitHub Desktop.
DFS(recursive)_python
This file contains 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
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