Skip to content

Instantly share code, notes, and snippets.

@rpglover64
Created July 17, 2015 17:51
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 rpglover64/fadfd0a5a01a3a3319c2 to your computer and use it in GitHub Desktop.
Save rpglover64/fadfd0a5a01a3a3319c2 to your computer and use it in GitHub Desktop.
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