Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a cyclic graph using GraphViz DOT?

Tags:

dot

I tried to create a simple cyclic graph with this DOT input file:

digraph {
    rankdir=LR;

    node0 [label = "0",  group="bottom"]; 
    node1 [label = "1", group="bottom"]; 
    node2 [label = "2", group="top"]; 
    node3 [label = "3", group="top"]; 

    node0 -> node1; 
    node1 -> node0; 

    node2 -> node3; 
    node3 -> node2; 
}

and so far so good, it results in:

First try at a cyclic graph

but now I want to have the following relationships added to the nodes:

node0 -> node3; 
node3 -> node0; 

node2 -> node1; 
node1 -> node2; 

and surprisingly, this results in:

Unsatisfactory horizontal node placement

If I remove rankdir=LR;, I get a vertical graph. I need a simple cyclic graph, since the node placement in space has a connection to what they relate to. So the new connections should be vertical, and the nodes should form a square. Is this possible to achieve using DOT?

like image 512
tmaric Avatar asked Dec 02 '14 12:12

tmaric


People also ask

How do you make a graph on graphviz?

Create a graph object, assemble the graph by adding nodes and edges, and retrieve its DOT source code string. Save the source code to a file and render it with the Graphviz installation of your system. Use the view option/method to directly inspect the resulting (PDF, PNG, SVG, etc.) file with its default application.

How do I convert a dot file to a graph?

For windows: dl the msi and install; Find gvedit.exe in your programs list; Open . dot file in question; Click running person on toolbar; Go to graph -> settings ; change Output file type to file type of your liking and press ok..

What is a .dot file graph?

DOT is the text file format of the suite GraphViz. It has a human-readable syntax that describes network data, including subgraphs and elements appearances (i.e. color, width, label).

What language does Graphviz use?

Graphviz is an open-source python module that is used to create graph objects which can be completed using different nodes and edges. It is based on the DOT language of the Graphviz software and in python it allows us to download the source code of the graph in DOT language.


2 Answers

It might be possible to achieve using DOT; I haven't played with it for a couple of years, so I'm a bit rusty. However, your data is rendered as a neat diamond if you use dot's sister program circo, which should be part of a normal GraphViz installation.

like image 137
PM 2Ring Avatar answered Sep 21 '22 08:09

PM 2Ring


One way with dot would be to order the nodes in two rows:

digraph {
     0 -> {1 3}
     1 -> {0 2}
     2 -> {3 1}
     3 -> {2 0}
     // Put specified nodes on same row:
     {rank=same; 0; 1}
     {rank=same; 2; 3}
}


Formatted Graph

like image 26
kotchwane Avatar answered Sep 19 '22 08:09

kotchwane