[Tex/LaTex] Why does PGF/TikZ 3.0 draw the simple layered graph as non-planar by default

graphstikz-graphstikz-pgftikz-trees

I have two questions:

  1. If I’m reading §30.4 “Crossing Minimization” of the PGF/TikZ 3.0 manual correctly, the layered graph drawing algorithm should have crossing minimization enabled by default. So why doesn’t the algorithm automatically choose a planar embedding for this graph?
  2. What is the best way to tell the algorithm to avoid the edge crossing in this graph? In this case, I specifically want C to be positioned to the left of B and its children (all in gray), without changing other attributes of the graph (as in percusse’s answer).

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphs, graphdrawing}
\usegdlibrary{layered}

\begin{document}
\tikz [gr/.style={gray!50}, font=\bfseries]
\graph [layered layout] {
    % Swapping the next two lines removes the edge crossing, but
    % it doesn’t make much sense, and also isn’t what I want.
    A -- C -- F,
    { [nodes=gr, edges=gr] A -- B -- { E, D -- F } }
};
\end{document}

 

Update

I looked again at Pohlmann’s thesis describing his implementation of layered graph drawing for PGF/TikZ. I am now convinced that it is not possible to solve this problem unless you either:

  1. manually define minimum layers or weight for all or part of the graph, or
  2. reimplement the crossing minimization step of the algorithm (in Lua).

The first item is not preferable in my case, since (a) this problem affects only a small subset of a number of large graphs, and (b) it also defeats the point of using automatic graph layout.

The second is feasible but time-consuming for someone not versed in Lua.

Here are a few quotes from the thesis (emphasis added by me):

Forcing certain edges to have an increased minimum span can sometimes unravel drawings with undesired edge crossings. For this purpose, the modular layered drawing algorithm provides the /graph drawing/layered drawing/minimum layers edge option. (75)

Applied small graphs, the algorithm appears to have no problems generating layered drawings that have only a few edge crossings and bends, even though it does not produce optimal results in all situations—see for instance the drawing of BarthMJ-fig2 where the edge crossing could easily be avoided. Overall, most edges are as short as possible. (102)

 

For what it’s worth, Mathematica and dot not suffer from this problem:

edges = {"a" -> "c", "c" -> "f", "a" -> "b", "b" -> "e", "b" -> "d", "d" -> "f"};
Row[{LayeredGraphPlot[edges, VertexLabeling -> True, ImageSize -> 100],
     Graph[edges /. Rule -> DirectedEdge, VertexLabels -> Placed["Name", Before],
           ImagePadding -> 10, ImageSize -> 110]}]
digraph g { a -> c -> f; a -> b; b -> e; b -> d -> f }

 

Update 2

I eventually went with setting minimum layers and weight for a single edge, as hinted by esdd’s answer. I was happy enough with this result:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphs, graphdrawing}
\usegdlibrary{layered}

\begin{document}
\tikz [gr/.style={gray!50}, font=\bfseries]
\graph [layered layout] {
    % A and F are horizontally aligned if you also set weight=0.5 for A -- C.
    A -- [minimum layers=2] C -- F,
    { [nodes=gr, edges=gr] A -- B -- { E, D -- F } }
};
\end{document}

Best Answer

Based on percusse's answer you can change the number of minimum layers (e.g. the minimum number of levels that an edge must span) only for the edge from C to F:

\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{graphs, graphdrawing}
\usegdlibrary{layered}

\begin{document}
\tikz [gr/.style={gray!50}, font=\bfseries]
\graph [layered layout] {
    A -- C -- F [>minimum layers=2],
    { [nodes=gr, edges=gr] A -- B -- { E, D -- F } },
};
\end{document}

enter image description here

In addition you can set the weight parameter of the edge from D to F to 0:

\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{graphs, graphdrawing}
\usegdlibrary{layered}

\begin{document}
\tikz [gr/.style={gray!50}, font=\bfseries]
\graph [layered layout] {
    A -- C -- F [>minimum layers=2],
    { [nodes=gr, edges=gr] A -- B -- { E, D -- F [>weight=0] } },
};
\end{document}

enter image description here

Related Question