[Tex/LaTex] Algorithmic graph drawing with TikZ & PGF

graphstikz-graphdrawing

I'm trying out the algorithmic graph drawing capabilities of TikZ/PGF and initially I was very exited. However, now I am stuck.

My example is below. I think the graph would look better if node "J" was moved down and left and node "G" was moved inside the cycle A-I-J-F-A.

So far I have tried different random seeds and different random orders of the lines in the TeX-file. I have also tried to use absolute node positioning that the force directed drawing would fine-tune, but it never produced anything.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{force}
\begin{document}
\tikz \graph [spring layout, random seed=1] {
  A -- F;
  A -- G;
  A -- I;
  B -- F;
  C -- D;
  C -- F;
  D -- E;
  D -- H;
  D -- I;
  E -- I;
  F -- J;
  G -- I;
  G -- J;
  I -- J;
};
\end{document}

Simple graph

Best Answer

I see that you're using spring layout but it seems to me that the resulting graphic is more like distributed charges (electric charges) making me think that the spring electrical layout is more suiting. Just by using it you already see the difference in the final graph. Furthermore, to fine tune the graphic you can use the nudge=<coordinate> key (or the variants nudge up, down, left and right), although to mee it seems a little rudimentar, but if the drawing needs 'simple' nudges, than it's ok:

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{force}
\begin{document}
\tikz \graph [spring electrical layout, random seed=1] {
  A -- F;
  A -- I;
  B -- F;
  C -- D;
  C -- F;
  D -- E;
  D -- H;
  D -- I;
  E -- I;
  F -- J[nudge=(210:10mm)];
  I -- J;
  G[nudge=(260:11mm)] -- J;
  G -- A;
  G -- I;
};
\end{document}

enter image description here

You can alse try to set electric charges to the nodes, fine tunning the intensity they repell (positive values) or atract (negative values) the connected nodes. Though with this way the result is sometimes quite unpredictable.

Second Approach

If you look at the previous drawing seems that node J has an invisible connection with nodes C and D. The same could be said for the node G with F. My thought was, if I could make this invisible connection then the resulting graph would probably be what the OP wants without any nudges and with more reliable graph. To do so we must give the line-to operator (--) the option draw=none when making the Connections (I also took the liberty of shortening the code a little):

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{force}
\begin{document}
\tikz \graph [spring electrical layout] {
  A -- F -- C -- D -- E -- I -- A;
  D -- I;
  B -- F;
  H -- D;
  I -- J -- F;
  I -- G -- A;
  G -- J;
  % invisible connections
  D --[draw=none] J --[draw=none] C;
  G --[draw=none] F;
};
\end{document}

The result is nearly the same as before, but should give no Errors this time:

enter image description here

Related Question