Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- def DFS(graph, root_node = "root"):
- stack = []
- children = graph[root_node]
- stack = children + stack
- print(root_node)
- while len(stack) > 0:
- node = stack.pop(0)
- print(node)
- children = graph[node]
- stack = children + stack
- """
- example:
- graph = {"a":["b", "c"], "b":["d", "e"], "c":[], "d":[], "e":[]}
- DFS(graph, root_node = "a")
- # a
- # b
- # d
- # e
- # c
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement