0

I want to produce a circular graph with one (and possibly several others...) in the middle of the circle. As an example, consider this simple 7-nodes undirected graph:

graph g1 {

0--1;
0--2;
0--3;
0--4;
0--5;
0--6;


1--2--3--4--5--6--1;
}

I would like something like this (did online with csacademy.com/app/graph_editor/: enter image description here

The Graphviz circo engine does a good job putting all the nodes in a circular layout, but I want the node 0 to be in the middle.

I read on its page that the attribute root can be used to specify which one is in the middle:

The center of the layout will be the root of the generated spanning tree. As a graph attribute, this gives the name of the node. As a node attribute, it specifies that the node should be used as a central node.

As I understand it, this attribute can be used either as a global graph attribute, either as a "node" attribute. So I tried adding:

0[root=true];

at the beginning, to tell him that the node 0 is the root one. No change.

I also tried graph [root=0];, no success (see live demo here). How can I achieve this?

Other answers suggest adding a "hidden" node, but I can't do that because the dot file is used both to produce a rendering and as data input for something else.

Second question: would it be possible to have a "cluster" of several nodes in the middle of the circle, using that technique? Or does the "root" attribute need to be assigned to a single node?

Edit For example, something like this, with 3 nodes inside the circular layout. enter image description here

kebs
  • 6,387
  • 4
  • 41
  • 70

2 Answers2

2

Use twopi (https://graphviz.org/docs/layouts/twopi/) instead of circo.
Note that the node placement (starting point, direction, and sequence) seems to be out of our control (though OK to my eye).

Please say more or show an example of Question #2 (sketch would be fine)

graph g1 {
root="0"

0--1;
0--2;
0--3;
0--4;
0--5;
0--6;
1--2--3--4--5--6--1;
}

Gives:
enter image description here

sroush
  • 5,375
  • 2
  • 5
  • 11
  • Ah, great, thanks, I clearly misunderstood how to use the "root" attribute. I'll edit question to clarify the second point. – kebs Jul 17 '23 at 15:43
  • I tried with 2 root nodes, went fine, but fails with 3. – kebs Jul 17 '23 at 15:59
1

twopi layout is pretty much "take it or leave it".
The following gets pretty close, but starts getting fiddley with weight attributes (different than weight for the other engines).
If you want more control, here is a link to a radial layout preprocessor for Graphviz https://gist.github.com/steveroush/60d9a850a545ec4e02b0482c7bac3ad5

graph g1 {
  root="X"  // added - invisible root
  X [shape=point style=invis]
  ranksep=".5:2"  // inches
  { edge [style=invis]
    X -- {0 6 7}
  }
  0 -- {6 7}
  6 -- 7

  0 -- {2 3}
  6 -- {4 5}
  7 -- {1 }
  7 -- {2 } [style=invis]

  edge[weight=0]  // similar to constraint=false for dot
  0 -- 4
  6 -- 1

  edge[weight=1]  
  1 -- 2 -- 3 -- 4 -- 5 -- 1
}

Giving:
enter image description here

sroush
  • 5,375
  • 2
  • 5
  • 11
  • Thanks, that very interesting! And I'll have a look to your sh script. I'll accept this one, although in my case I cannot add an invisible node. – kebs Jul 18 '23 at 18:28