Given a .dot representation of a graph, I'd like to be able to compile some statistics about each node. Statistics could be: # of edges, # of levels, # of nodes.
Is there a package available that lets me do this?
Given a .dot representation of a graph, I'd like to be able to compile some statistics about each node. Statistics could be: # of edges, # of levels, # of nodes.
Is there a package available that lets me do this?
Yes, this comes with graphviz out of the box.
General statistics about a graph can be obtained by feeding your graph into gc
- count graph components:
gc (...) prints to standard output the number of nodes, edges, connected com- ponents or clusters contained in the input files.
If you would like to generate more specific stats about your graph, you can use the tool gvpr
- graph pattern scanning and processing language.
gvpr
allows executing a custom script against your graph. The script may simply gather custom statistics like in your case, or it may even modify the input graph.
The above linked documentation is very complete and explains all the available properties and features better than I can do here. Below just a simple example to get you started.
If we have the following graph graph.gv
:
digraph graphinfotest {
a -> {b; c; d} -> e;
b -> c;
}
The following gvpr
script (in file graphinfo.gvpr
):
BEG_G {
int n = nNodes($G);
int e = nEdges($G);
printf("There are %d nodes and %d edges in %s\n", n, e, $G.name);
}
N {
printf("Node %s - indegree %d, outdegree %d\n", $.name, $.indegree, $.outdegree);
}
Called with
gvpr -f graphinfo.gvpr graph.gv
Will produce the following output:
There are 5 nodes and 7 edges in graphinfotest
Node a - indegree 0, outdegree 3
Node b - indegree 1, outdegree 2
Node c - indegree 2, outdegree 1
Node d - indegree 1, outdegree 1
Node e - indegree 3, outdegree 0