Skip to content

Instantly share code, notes, and snippets.

@lucaswiman lucaswiman/foo.dot
Created May 31, 2016

Embed
What would you like to do?
networkx / graphviz example
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=ε];
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# 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
You can’t perform that action at this time.