Skip to content

Instantly share code, notes, and snippets.

@sidharthkuruvila
Created December 14, 2017 08:27
Show Gist options
  • Save sidharthkuruvila/f59b38215316c0cf6f7d18d21347e504 to your computer and use it in GitHub Desktop.
Save sidharthkuruvila/f59b38215316c0cf6f7d18d21347e504 to your computer and use it in GitHub Desktop.
Generate a graphviz dot file for a digraph represented as an adjacency list
G = [("A", "B"), ("B", "C"), ("C", "D")]
def render_digraph(G):
def g():
for p, c in G:
yield '"{}" -> "{}"'.format(p, c)
lines = "\n".join(g())
return """
digraph {{
{}
}}
""".format(lines)
with file("graph.dot", "w") as fo:
fo.write(render_digraph(G))
@sidharthkuruvila
Copy link
Author

python render_digraph.py
dot -Tpng graph.dot > graph.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment