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
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.38.0 (20140413.2041)
-->
<!-- Title: %3 Pages: 1 -->
<svg width="139pt" height="216pt"
viewBox="0.00 0.00 138.78 216.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 212)">
<title>%3</title>
<polygon fill="white" stroke="none" points="-4,4 -4,-212 134.781,-212 134.781,4 -4,4"/>
<!-- 0 -->
<g id="node1" class="node"><title>0</title>
<ellipse fill="none" stroke="black" cx="27" cy="-190" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-185.8" font-family="Times,serif" font-size="14.00">0</text>
</g>
<!-- * -->
<g id="node2" class="node"><title>*</title>
<ellipse fill="none" stroke="black" cx="64" cy="-104" rx="27" ry="18"/>
<text text-anchor="middle" x="64" y="-99.8" font-family="Times,serif" font-size="14.00">*</text>
</g>
<!-- 0&#45;&gt;* -->
<g id="edge1" class="edge"><title>0&#45;&gt;*</title>
<path fill="none" stroke="black" d="M23.0413,-171.975C21.6114,-162.217 21.2804,-149.967 25.6758,-140 28.3482,-133.94 32.6452,-128.475 37.4136,-123.762"/>
<polygon fill="black" stroke="black" points="39.9163,-126.223 45.1291,-116.999 35.3021,-120.959 39.9163,-126.223"/>
<text text-anchor="middle" x="34.1621" y="-142.8" font-family="Times,serif" font-size="14.00">[b]</text>
</g>
<!-- *&#45;&gt;0 -->
<g id="edge4" class="edge"><title>*&#45;&gt;0</title>
<path fill="none" stroke="black" d="M56.7052,-121.561C51.4892,-133.403 44.3665,-149.573 38.4034,-163.111"/>
<polygon fill="black" stroke="black" points="35.1381,-161.842 34.31,-172.404 41.5441,-164.664 35.1381,-161.842"/>
<text text-anchor="middle" x="55.769" y="-142.8" font-family="Times,serif" font-size="14.00">[a]</text>
</g>
<!-- 1 -->
<g id="node3" class="node"><title>1</title>
<ellipse fill="none" stroke="black" cx="28" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="28" y="-13.8" font-family="Times,serif" font-size="14.00">1</text>
</g>
<!-- *&#45;&gt;1 -->
<g id="edge5" class="edge"><title>*&#45;&gt;1</title>
<path fill="none" stroke="black" d="M44.5452,-91.1225C36.7724,-85.2315 28.6776,-77.3621 24.4619,-68 21.4152,-61.234 20.8479,-53.4076 21.4267,-46.0229"/>
<polygon fill="black" stroke="black" points="24.9039,-46.4317 22.8691,-36.0341 17.9758,-45.4312 24.9039,-46.4317"/>
<text text-anchor="middle" x="32.769" y="-56.8" font-family="Times,serif" font-size="14.00">[c]</text>
</g>
<!-- exit -->
<g id="node5" class="node"><title>exit</title>
<ellipse fill="none" stroke="black" cx="103" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="103" y="-13.8" font-family="Times,serif" font-size="14.00">exit</text>
</g>
<!-- *&#45;&gt;exit -->
<g id="edge6" class="edge"><title>*&#45;&gt;exit</title>
<path fill="none" stroke="black" d="M71.7051,-86.4043C77.207,-74.5542 84.7154,-58.3822 90.9985,-44.8493"/>
<polygon fill="black" stroke="black" points="94.2742,-46.1052 95.3109,-35.5612 87.9252,-43.1574 94.2742,-46.1052"/>
<text text-anchor="middle" x="89.1069" y="-56.8" font-family="Times,serif" font-size="14.00">ε</text>
</g>
<!-- 1&#45;&gt;* -->
<g id="edge2" class="edge"><title>1&#45;&gt;*</title>
<path fill="none" stroke="black" d="M35.0977,-35.5612C40.1727,-47.4029 47.1028,-63.5733 52.9048,-77.1113"/>
<polygon fill="black" stroke="black" points="49.7313,-78.5916 56.8876,-86.4043 56.1653,-75.8341 49.7313,-78.5916"/>
<text text-anchor="middle" x="57.1621" y="-56.8" font-family="Times,serif" font-size="14.00">[d]</text>
</g>
<!-- enter -->
<g id="node4" class="node"><title>enter</title>
<ellipse fill="none" stroke="black" cx="103" cy="-190" rx="27.5633" ry="18"/>
<text text-anchor="middle" x="103" y="-185.8" font-family="Times,serif" font-size="14.00">enter</text>
</g>
<!-- enter&#45;&gt;* -->
<g id="edge3" class="edge"><title>enter&#45;&gt;*</title>
<path fill="none" stroke="black" d="M95.2949,-172.404C89.793,-160.554 82.2846,-144.382 76.0015,-130.849"/>
<polygon fill="black" stroke="black" points="79.0748,-129.157 71.6891,-121.561 72.7258,-132.105 79.0748,-129.157"/>
<text text-anchor="middle" x="89.1069" y="-142.8" font-family="Times,serif" font-size="14.00">ε</text>
</g>
</g>
</svg>
# 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