I'm currently experimenting with automatic Tikz graph generation.
Having a graph containing 10 nodes, the automatic layout creation of Tikz' spring layout is great. However, if the number of nodes in the graph increases, scaling of the graph struggles.
I started adapting the node distance parameter (which was fine for small graphs having nodes < 20) but I also played around with the other parameters like spring constants and electrical charge.
This is my MWE (run lualatex
) for a graph having 10 nodes:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphdrawing}
\usegdlibrary{force}
\definecolor{burntorange}{cmyk}{0,0.52,1,0}
\definecolor{violet}{cmyk}{0.79,0.88,0,0}
\def\oran{orange!30}
\tikzstyle{vertex}=[draw,circle,burntorange, left color=\oran,text=violet,minimum width=20pt]
\begin{document}
\begin{tikzpicture}[spring layout, node distance=50pt]
\node (0) [vertex] {0};
\node (1) [vertex] {1};
\node (2) [vertex] {2};
\node (3) [vertex] {3};
\node (4) [vertex] {4};
\node (5) [vertex] {5};
\node (6) [vertex] {6};
\node (7) [vertex] {7};
\node (8) [vertex] {8};
\node (9) [vertex] {9};
\path (0) edge[--] (1);
\path (0) edge[--] (2);
\path (0) edge[--] (3);
\path (1) edge[--] (4);
\path (3) edge[--] (5);
\path (3) edge[--] (6);
\path (2) edge[--] (7);
\path (7) edge[--] (8);
\path (5) edge[--] (9);
\end{tikzpicture}
\end{document}
This results in graph
Now, using a bigger graph with much more nodes, the graph is too tight:
Here is the code for the example having 100 nodes:
Any idea what to change?
Thank you!
Best Answer
Note that I know nothing about graph-drawing ...
Caveat emptor ...
I am not sure what result exactly you are trying to achieve. However, it is not clear to me that you understand the code you are trying to use.
spring layout
uses an algorithm to model the effects of forces between nodes in the graph. Eachedge
is aspring
which pushes the nodes apart or pulls them together, depending on whether it is compressed or extended. If you add 100 nodes to the graph and you add manyedge
s between them, then imagine the result of the forces acting on those nodes. Some will be pulled together, some pushed apart. This will result in a different balance of forces and another cycle. Eventually, the system will reach equilibrium. In fact, of course, TikZ does not necessarily continue until this point. Rather, it works through a given number of cycles (iterations
) to track the state of the system.The
spring layout
is designed for use with the\graph
command. You don't have to use the graph syntax, but it is much more concise. I translated the beginning of your specification for the larger graph.There are various ways of influencing the result. For example, I tried
which results in the nodes being more spaced out
However, note that TikZ simply isn't trying to stop the nodes from overlapping each other. It is, rather, trying to show how the springs you've defined will affect the movement and location of the nodes over time.
You can visualise the problem by taking the springs out.
Looking at the edges, you can imagine what happens when they are turned into springs of equal length and stiffness. 3, 13, 15, 19 will be pulled far in towards 0. 6, 7, 16, 17, 21 will be pulled towards 1. But there is nothing to push these nodes away from each other.
Of course, I didn't use the whole graph, partly because I didn't want to translate it an partly because this took long enough to compile already. But think about whether the connections between the nodes are appropriate for this algorithm. If you need some of the nodes connected to 0 to end up far from 0 and others to end up close, in order to get a good result, then this probably isn't the kind of layout you want.