Created
January 17, 2016 23:13
-
-
Save goodwin64/23ac9d9c5ee516edd2b6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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