Created
July 22, 2017 18:10
-
-
Save slashvar/d4954d04352fc38356f774198e3aa86b to your computer and use it in GitHub Desktop.
A simple DFS example
This file contains 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
import graphviz | |
g = { | |
'A': ['B', 'C', 'D', 'F'], | |
'B': ['C', 'D'], | |
'D': ['A', 'C'], | |
'C': [], | |
'E': ['C', 'G'], | |
'F': ['A', 'C'], | |
'G': ['E'] | |
} | |
def dfs(g, cur, mark, tree): | |
mark[cur] = True | |
for succ in g[cur]: | |
if not mark[succ]: | |
tree.edge(cur, succ) | |
dfs(g, succ, mark, tree) | |
tree = graphviz.Digraph(name='g') | |
mark = { v : False for v in g.keys() } | |
dfs(g, 'A', mark, tree) | |
tree.save(filename='g.dot') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment