3

I was trying to recreate following graphviz graph with tikz (from https://graphs.grevian.org/example)

graph {
    rankdir=LR;
    a -- { b c d }; b -- { c e }; c -- { e f }; d -- { f g }; e -- h;
    f -- { h i j g }; g -- k; h -- { o l }; i -- { l m j }; j -- { m n k };
    k -- { n r }; l -- { o m }; m -- { o p n }; n -- { q r };
    o -- { s p }; p -- { s t q }; q -- { t r }; r -- t; s -- z; t -- z;
    { rank=same; b, c, d }
    { rank=same; e, f, g }
    { rank=same; h, i, j, k }
    { rank=same; l, m, n }
    { rank=same; o, p, q, r }
    { rank=same; s, t }
}

enter image description here

To replicate the setting rankdir=LR; I have followed the advice from Rotate tikz "Layered Layout" and have added grow=right, components go down left aligned:

%! TEX program = lualatex
\documentclass[tikz]{standalone}
\usetikzlibrary{graphs,graphdrawing,shapes}
\usegdlibrary{layered}
\begin{document}
\tikz \graph [layered layout,
    nodes={ellipse, minimum width=25pt, draw},
    % https://tex.stackexchange.com/q/390417/rotate-tikz-layered-layout
    grow=right, components go down left aligned
    ] {
    { [same layer] b, c, d };
    { [same layer] e, f, g };
    { [same layer] h, i, j, k };
    { [same layer] l, m, n };
    { [same layer] o, p, q, r };
    { [same layer] s, t };

    a[nudge left=25mm] -- { b, c, d }; b -- { c, e };c -- { e, f };
    d -- { f, g }; e -- h; f[nudge down=10mm] -- { h, i, j, g };
    g[nudge down=10mm] -- k; h --[bend left=-30] o; h -- l;
    i -- { l, m, j }; j -- { m, n, k }; k -- { n, r }; l[nudge up=10mm] -- { o, m };
    m[nudge up=10mm] -- { o, p, n }; n -- { q, r }; o -- { s, p }; p -- { s, t, q };
    q -- { t, r }; r -- t; s -- z; t -- z;
};
\end{document}

enter image description here

I have removed unnecessary edge crossings by 'nudging' node a to the left. To fill the empty space left by the moved node a, I have moved g and f downwards. Also I had to adjust m and l upwards and bend the edge h--o downwards.

Without these modifications the graph looks like

enter image description here

In particular the edge h--o is completely misleading.

Does anyone know how to recreate the graphviz result, i.e. orient the layered graph horizontally (in contrast to the default) and avoid so many manual adjustments?

UPDATE

placing a on an own layer already fixed nudging a, f and g:

{ [same layer] a };

The remaining problem is apparently the crossing minimization algorithm which does not know the 'easy' solution to move m and l up and add the kink into the other direction:

enter image description here

Note: answer by marmot suggests to add those edges manually which connect nodes of two different layers with at least one additional layer between them. Maybe this could be a rule of thumb for other graphs as well.

3

I do not know if that's helpful, but if I

  • remove all nudges,
  • create a layer for a,
  • change grow=right to grow'=right, and
  • move a single command "out of the graph": \draw (h) to[bend left=40] (o);,

I get

\documentclass[tikz]{standalone}
\usetikzlibrary{graphs,graphdrawing}
\usetikzlibrary{shapes}
\usegdlibrary{layered}
\begin{document}
\begin{tikzpicture}
\graph [layered layout,
    nodes={ellipse, minimum width=25pt, draw},
    % https://tex.stackexchange.com/q/390417/rotate-tikz-layered-layout
    grow'=right, components go down left aligned
    ] {
    { [same layer] a }; %<- added
    { [same layer] b, c, d };
    { [same layer] e, f, g };
    { [same layer] h, i, j, k };
    { [same layer] l, m, n };
    { [same layer] o, p, q, r };
    { [same layer] s, t };
    a -- { b, c, d }; 
    b -- { c, e };
    c -- { e, f };
    d -- { f, g }; 
    e -- h; 
    f -- { h, i, j, g };
    g -- k; 
    h -- l;
    i -- { l, m, j }; 
    j -- { m, n, k }; 
    k -- { n, r }; 
    l -- { o, m };
    m -- { o, p, n }; 
    n -- { q, r }; 
    o -- { s, p }; 
    p -- { s, t, q };
    q -- { t, r }; 
    r -- t; 
    s -- z; 
    t -- z;
};
\draw (h) to[bend left=40] (o);
\end{tikzpicture}
\end{document}

enter image description here

To an ordinary marmots eye, this looks already close to the desired result.

If I, in addition, move one more connection "out of the graph", \draw (k) -- (r);, this gives me

\documentclass[tikz]{standalone}
\usetikzlibrary{graphs,graphdrawing}
\usetikzlibrary{shapes}
\usegdlibrary{layered}
\begin{document}
\begin{tikzpicture}
\graph [layered layout,
    nodes={ellipse, minimum width=25pt, draw},
    % https://tex.stackexchange.com/q/390417/rotate-tikz-layered-layout
    grow'=right, components go down left aligned
    ] {
    { [same layer] a }; %<- added
    { [same layer] b, c, d };
    { [same layer] e, f, g };
    { [same layer] h, i, j, k };
    { [same layer] l, m, n };
    { [same layer] o, p, q, r };
    { [same layer] s, t };
    a -- { b, c, d }; 
    b -- { c, e };
    c -- { e, f };
    d -- { f, g }; 
    e -- h; 
    f -- { h, i, j, g };
    g -- k; 
    h -- l;
    i -- { l, m, j }; 
    j -- { m, n, k }; 
    k -- { n }; 
    l -- { o, m };
    m -- { o, p, n }; 
    n -- { q, r }; 
    o -- { s, p }; 
    p -- { s, t, q };
    q -- { t, r }; 
    r -- t; 
    s -- z; 
    t -- z;
};
\draw (h) to[bend left=40] (o);
\draw (k) -- (r);
\end{tikzpicture}
\end{document}

enter image description here

which is arguably even closer to the desired result.

  • Thanks for your answer. I did not know the different grow and grow'. It looks like the graph drawing algorithm works better when a few edges are drawn manually. I will keep this in mind. – Hotschke Sep 27 '18 at 16:48
  • @Hotschke You're welcome! (I'd like to add that I have not much experience in graph drawing, and I would not be surprised if there was a way to tell the layout algorithm not to take in certain connections for the layout, thus eliminating the need to move certain connections "out of the graph". On the other hand, drawing these connections is also not too inconvenient, I'd say.) – marmot Sep 27 '18 at 16:52
  • 1
    I agree that drawing one or two edges manually is less inconvenient than 'nudging' nodes around. I am not sure whether the author of the graphdrawing lib would be interested in this example. Maybe the algorithm can be improved. However, I am happy with it and will return to the actual work. – Hotschke Sep 27 '18 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.