-
-
Save rpglover64/fadfd0a5a01a3a3319c2 to your computer and use it in GitHub Desktop.
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
gm = {0: [0, 1, 4], 1: [0, 2], 2: [3, 4], 3: [2, 1], 4: []} | |
g = lambda x: gm[x] | |
def dfs(graph, start, stack=None): | |
if stack is None: | |
stack = [] | |
if start in stack: | |
print("Cycle detected involving:", start) | |
return | |
print("Visiting:", start) | |
stack.append(start) | |
for newStart in graph(start): | |
dfs(graph, newStart, stack) | |
print("Done with:", start) | |
dfs(g,0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment