Creating a graph from cli

2016-06-08

Well, sort of from cli.

GraphViz is a nice tool for this. You can easily automatically generate the connections and produce a nice graph. Lots of options, basics are simple enough. Capable of outputting in various outputs, including png and pdf.

The docs for this language slash tool are in this pdf.

An example of a graph I was working on is below. You can generate a graph with the following, after installing it (sudo apt-get install graphviz) of course:

Code:
// png
dot -Tpng input.txt -o graph1.png
// pdf (tends to be easier with large graphs)
dot -Tpdf input.txt -o graph1.pdf

input.txt could be the following:

Code:
digraph G {
0 [label="18363 props\n9541 vars"];
0 -> 00 ;
00 [label="11123 props\n6613 vars"];
00 -> 000 ;
000 [label="10487 props\n6231 vars"];
0 -> 00 ;
00 [label="11123 props\n6231 vars"];
00 -> 001 ;
001 [label="10487 props\n6231 vars"];
001 -> 0010 ;
0010 [label="10471 props\n6217 vars"];
00 -> 001 ;
001 [label="10487 props\n6217 vars"];
001 -> 0011 ;
0011 [label="10471 props\n6217 vars"];
0011 -> 00110 ;
00110 [label="10457 props\n6205 vars"];
001 -> 0011 ;
0011 [label="10471 props\n6205 vars"];
0011 -> 00111 ;
00111 [label="10457 props\n6205 vars"];
00111 -> 001110 ;
001110 [label="10443 props\n6193 vars"];
0011 -> 00111 ;
00111 [label="10457 props\n6193 vars"];
00111 -> 001111 ;
001111 [label="10443 props\n6193 vars"];
001111 -> 0011110 ;
0011110 [label="10425 props\n6179 vars"];
00111 -> 001111 ;
001111 [label="10443 props\n6179 vars"];
001111 -> 0011111 ;
0011111 [label="10425 props\n6179 vars"];
}



An alternative format is graphml, which is a bit more convoluted because it uses xml. I think it has more options than dot, though.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!-- This file was written by the JAVA GraphML Library. -->
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd" xmlns:y="http://www.yworks.com/xml/graphml">
<graph>
<node id="n1_0_0_0" />
<edge source="n1_0_0_0" target="n2_1_0_0" />
<node id="n2_1_0_0" />
<edge source="n2_1_0_0" target="n3_2_0_0" />
<node id="n3_2_0_0" />
<edge source="n2_1_0_0" target="n4_2_0_1" />
<node id="n4_2_0_1" />
<edge source="n4_2_0_1" target="n5_3_0_0" />
<node id="n5_3_0_0" />
<edge source="n4_2_0_1" target="n6_3_1_0" />
<node id="n6_3_1_0" />
<edge source="n4_2_0_1" target="n7_3_2_0" />
<node id="n7_3_2_0" />
<edge source="n4_2_0_1" target="n8_3_3_0" />
<node id="n8_3_3_0" />
<edge source="n4_2_0_1" target="n9_3_4_0" />
<node id="n9_3_4_0" />
... etc
</graph>
</graphml>

You can import this in tools like yEd, although their web editor has a non-intuitive interface for me. And I was unable to get labels to work there. But that may just be my fault. Labels in graphml aren't a simple desc="root" attribute. I hate whoever thought that wasn't a great alternative way to support simple labels. Hate.