Skip to content

Instantly share code, notes, and snippets.

@goodwin64
Created January 17, 2016 23:13
Show Gist options
  • Save goodwin64/23ac9d9c5ee516edd2b6 to your computer and use it in GitHub Desktop.
Save goodwin64/23ac9d9c5ee516edd2b6 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_paths(graph, start, goal):
stack = [(start, [start])]
while stack:
(vertex, path) = stack.pop()
for next in graph[vertex] - set(path):
if next == goal:
yield path + [next]
else:
stack.append((next, path + [next]))
print(list(dfs_paths(graph, 'A', 'F')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment