Advertisement
Guest User

Untitled

a guest
May 29th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. def DFS(graph, root_node = "root"):
  5.  
  6. stack = []
  7. children = graph[root_node]
  8. stack = children + stack
  9. print(root_node)
  10.  
  11. while len(stack) > 0:
  12. node = stack.pop(0)
  13. print(node)
  14. children = graph[node]
  15.  
  16. stack = children + stack
  17.  
  18. """
  19. example:
  20.  
  21. graph = {"a":["b", "c"], "b":["d", "e"], "c":[], "d":[], "e":[]}
  22.  
  23. DFS(graph, root_node = "a")
  24. # a
  25. # b
  26. # d
  27. # e
  28. # c
  29. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement