Ever have questions about the ranking of a large graph?

Ever have questions about the ranking of a large graph?

  • how many ranks, total?
  • which rank has the most (or fewest) nodes?
  • what rank is node XYZ on?
  • what are all the nodes(ranks) “between” two (or three or more) nodes(ranks)

OK, I admit I can’t get you all the way to such answers, but this will get you closer. It uses the recently discovered (undocumented) phase attribute (Document undocumented attributes (#75) · Issues · graphviz / graphviz.gitlab.io · GitLab). If you run dot with the phase attribute set to 1 graph[phase=1], dot will do the ranking, report the result (see below) and then quit.
(And it is blazingly fast)
like so:
dot -Tdot -Gphase=1 myfile.gv >myfile.dot

It produces a valid, but incomplete, Graphiz input/output file in the dot format. Node ranking is done (A is on rank 0 and B is on rank 1) and sizing is complete, but node positioning and edge positioning are not done.
Example output:

graph test {
	graph [bb="0,0,0,0",
		phase=1
	];
	node [shape=plaintext,
		label="\N"
	];
	edge [dir=none];
	subgraph clusterA {
		graph [bb="0,0,0,0",
			margin=19
		];
		A	[height=0.5,
			pos="0,0",
			rank=0,
			width=0.75];
	}
	subgraph clusterB {
		graph [bb="0,0,0,0"];
		B	[height=0.5,
			pos="0,0",
			rank=1,
			width=0.75];
	}
	A -- B;
}

OK, back to the questions above.
A simple(?) python, or gvpr (see below), or C or whatever, program can turn the above file into node name / rank value data pairs.
gvpr 'N{print($.rank, " ",$.name)}' myfile.dot

Producing:

0 C
0 E
2 F
3 G
1 D
4 H

Take this data and most any programming language or spreadsheet system (Excel) can start answering the questions listed at the top.
Fun!
p.s. If you want to further use the output of a phase=1 output as input to dot remember to set phase=“”, or dot will stop early (phase=1 is added to the output)

1 Like

More & more correct info about getting the rank of nodes:

  • sometimes the rank value changes between phases 1, 2 and 3. So to get the final ranking, run dot -Gphase=3.
  • normally, dot -Gphase=3 will add a rank attribute to the output, but dot -Gphase=4 or dot -Gphase="" or plain old dot ... will NOT show the rank attribute
  • however, dot will keep any rank attribute it is given as input, so you can run this pipeline that saves the rank as a final attribute: dot -Gphase=3 myfile.gv | dot -Gphase="" >myfile.dot (note the phase=“” to reset the phase attribute set in the first dot invocation)