Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- graph = {'A': set(['B', 'C','D','E']),
- 'B': set(['A','C','D', 'E']),
- 'C': set(['A','B','D','E'),
- 'D': set(['A','B','C','E']),
- 'E': set(['A','B','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]))
- list(dfs_paths(graph, 'A', 'A')
- [['A',B,'A'],['A','B','C',A'],['A','B','C','D','A']...['A','D','C','B','A']]
- []
- def bfs_paths(graph, start, goal):
- queue = [(start, [start])]
- while queue:
- (vertex, path) = queue.pop(0)
- for next in graph[vertex] - set(path):
- if next == goal:
- yield path + [next]
- else:
- queue.append((next, path + [next]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement