Skip to content

Instantly share code, notes, and snippets.

@yask123
Created April 24, 2015 07:57
Show Gist options
  • Save yask123/d6b815c1352208ffdf79 to your computer and use it in GitHub Desktop.
Save yask123/d6b815c1352208ffdf79 to your computer and use it in GitHub Desktop.
graph = {'A': set(['B', 'C']),
'B': set(['A', 'D', 'E']),
'C': set(['A', 'F']),
'D': set(['B']),
'E': set(['B', 'F']),
'F': set(['C', 'E'])}
def dfs (graph,start,visited=None):
if visited is None:
visited=set()
visited.add(start)
for next in graph[start]-visited:
visited.add(next)
dfs(graph,next,visited)
return visited
print dfs(graph,'A')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment