Created
May 31, 2016 06:21
-
-
Save lucaswiman/682ce426c8908b2919911e7f352a4b51 to your computer and use it in GitHub Desktop.
networkx / graphviz 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
digraph { | |
0 -> "*" [key=0, | |
label="[b]"]; | |
1 -> "*" [key=0, | |
label="[d]"]; | |
enter -> "*" [key=0, | |
label=ε]; | |
"*" -> 0 [key=0, | |
label="[a]"]; | |
"*" -> 1 [key=0, | |
label="[c]"]; | |
"*" -> exit [key=0, | |
label=ε]; | |
} |
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
# Setup environment by running the following in the shell: | |
""" | |
virtualenv -p `which python3` foo | |
source foo/bin/activate | |
pip install pygraphviz | |
pip install networkx | |
python | |
""" | |
# Needs Graphviz, which can be installed by `brew install graphviz` on OS X. | |
from networkx import MultiDiGraph | |
from networkx.drawing.nx_agraph import write_dot | |
import os | |
machine = MultiDiGraph() | |
machine.add_edge('enter', '*', label=u'ε') | |
machine.add_edge('*', 0, label='[a]') | |
machine.add_edge(0, '*', label='[b]') | |
machine.add_edge('*', 'exit', label=u'ε') | |
machine.add_edge('*', 1, label='[c]') | |
machine.add_edge(1, '*', label='[d]') | |
write_dot(machine, 'foo.dot') | |
os.system('dot -Tsvg foo.dot -o /tmp/foo.svg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment