Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. graph = {'A': set(['B', 'C','D','E']),
  2. 'B': set(['A','C','D', 'E']),
  3. 'C': set(['A','B','D','E'),
  4. 'D': set(['A','B','C','E']),
  5. 'E': set(['A','B','C','E'])}
  6.  
  7. def dfs_paths(graph, start, goal):
  8. stack = [(start, [start])]
  9. while stack:
  10. (vertex, path) = stack.pop()
  11. for next in graph[vertex] - set(path):
  12. if next == goal:
  13. yield path + [next]
  14. else:
  15. stack.append((next, path + [next]))
  16.  
  17. list(dfs_paths(graph, 'A', 'A')
  18.  
  19. [['A',B,'A'],['A','B','C',A'],['A','B','C','D','A']...['A','D','C','B','A']]
  20.  
  21. []
  22.  
  23. def bfs_paths(graph, start, goal):
  24. queue = [(start, [start])]
  25. while queue:
  26. (vertex, path) = queue.pop(0)
  27. for next in graph[vertex] - set(path):
  28. if next == goal:
  29. yield path + [next]
  30. else:
  31. queue.append((next, path + [next]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement