Skip to content

Instantly share code, notes, and snippets.

@aeter
Created April 15, 2010 08:37
Show Gist options
  • Save aeter/366845 to your computer and use it in GitHub Desktop.
Save aeter/366845 to your computer and use it in GitHub Desktop.
# prints a depth-first-search from the starting node
def dfs(node):
visited = set()
visited.add(node)
stack = []
stack.append(node)
while len(stack) > 0:
current = stack.pop()
print current,
for nodes in graph[current]:
if nodes not in visited:
stack.append(nodes)
visited.add(nodes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment