ajsims1704 / rdecision

Decision Analytic Modelling In Health Economics
1 stars 0 forks source link

DOT graph appearance depends on node order #14

Open ajsims1704 opened 1 month ago

ajsims1704 commented 1 month ago

The appearance of graphs drawn with the GraphViz tools (e.g., dot) depends on the order in which nodes are defined in the dot file created by the as_DOT() functions of Graph and Digraph. To achieve reproducibility of appearance, the nodes could be written in a reproducible order, e.g., by a topological sort of the graph (for a Digraph).

ajsims1704 commented 4 weeks ago

On investigation, this is likely due to there not being a unique topological sort order for DAGs unless there is a Hamiltonian path through the nodes (where every pair of adjacent nodes in the sorted order is joined by an edge). In most cases, that will not be true. For example, in Markov models.

It seems likely that the dot algorithm is already doing a topological sort during drawing, and in cases where there is no Hamiltonian path, the drawing will depend on the order in which nodes and edges are defined. Breaking ties by lexicographical sorting has been used as a technique to achieve a unique topological sort. This could be used in Markov models where each state must be labelled, but is not generally true for Digraphs. A solution in rdecision would be to overload the topological sort algorithm for the SemiMarkov class with one that uses lexicographical order to break ties, or encapsulate the as_DOT call, prefaced with a revised topological sort..