Search code examples
graphviz

Circular list in Graphviz? or how to bend the edge


I had been trying to do what I thought was a simple thing in Graphviz, something like this:

enter image description here (taken from https://tex.stackexchange.com/questions/394432/how-to-draw-circular-linked-list)

And I am trying to do something similar with Graphviz, this is how it looks right now:

enter image description here

I read the documentation and tried already with neato and circo diagrams and no success.... How can I achieve bending the edge in the way I need? I now I could try using a coordinate, like p3:e -> p1:w but that will remove the start point from the middle of the record.

This is my code so far:

digraph {
  node[shape=record];
  graph[pencolor=transparent];
  rankdir=LR;
  p1[label="{<data> 12|<next>}"];
  p2[label="{<data> 99|<next>}"];
  p3[label="{<data> 37|<next>}"];

  edge[tailclip=false,arrowtail=dot,dir=both];

  p1:next:c -> p2:data;
  p2:next:c -> p3:data;
  p3:next:c -> p1:data[constraint=false];
}

Solution

  • you need some helper nodes (p0 and p4)

    digraph {
      node[shape=record];
      graph[pencolor=transparent];
      rankdir=LR;
      p1[label="{<data> 12|<next>}"];
      p2[label="{<data> 99|<next>}"];
      p3[label="{<data> 37|<next>}"];
    
      edge[tailclip=false,arrowtail=dot,dir=both];
    
      {node[shape=point height=0] p0 p4} // make p0 and p4 to small to see
      p0:n -> p1[arrowtail=none]
      p0:s -> p4:s[dir=none] // add edge with no arrow to make it look like one long edge, also make p0 the tail so we don't have a recursition that may course dot to rearange the nodes
      p1:next:c -> p2:data;
      p2:next:c -> p3:data;
      //p3:next:c -> p1:data[constraint=false];
      p3:next:c -> p4:n[arrowhead=none]
    }
    

    enter image description here

    You can probably do the cross in the tail by using html table label and adding an image to one of the cells