Skip to content

Instantly share code, notes, and snippets.

@lucaswiman
Created May 31, 2016 06:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucaswiman/682ce426c8908b2919911e7f352a4b51 to your computer and use it in GitHub Desktop.
Save lucaswiman/682ce426c8908b2919911e7f352a4b51 to your computer and use it in GitHub Desktop.
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=ε];
}
Display the source blob
Display the rendered blob
Raw
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