5

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 Graph with 10 nodes

Now, using a bigger graph with much more nodes, the graph is too tight: Graph with 100 nodes

Here is the code for the example having 100 nodes:

http://pastebin.com/LHjcee8Z

Any idea what to change?

Thank you!

2
  • Bigger values of node distance results in a wider spread for the central nodes still maintaining the overlay for the "leaf nodes"
    – Robin
    Commented Jun 12, 2015 at 11:59
  • Please don't use minimal for examples.
    – cfr
    Commented Sep 4, 2016 at 3:13

1 Answer 1

2

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. Each edge is a spring 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 many edges 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.

\begin{tikzpicture}
  \graph [spring layout, nodes={vertex}]
  {
    0 -- {1-- {2 -- 25,4,5-- {8 -- 12 -- 26,9-- 10 -- {18,20,22,23},11,24},6,7,16,17,21}, 3,13 -- 14,15,19}
    ;
  };
\end{tikzpicture}

There are various ways of influencing the result. For example, I tried

cooling factor=.1, convergence tolerance=.0001, node distance=25mm

which results in the nodes being more spaced out

modified spring layout

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.

\begin{tikzpicture}
  \graph [nodes={vertex}, node distance=25mm]
  {
    0 -- {1-- {2 -- 25,4,5-- {8 -- 12 -- 26,9-- 10 -- {18,20,22,23},11,24},6,7,16,17,21}, 3,13 -- 14,15,19}
    ;
  };
\end{tikzpicture}

non-sprung graph

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.

\RequirePackage{luatex85}
\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{graphdrawing,graphs}
\usegdlibrary{force}
\definecolor{burntorange}{cmyk}{0,0.52,1,0}
\tikzset{% \tikzstyle is deprecated
  vertex/.style={draw, circle, burntorange, left color=orange!30, text=violet, minimum size=20pt},
}
\begin{document}
\begin{tikzpicture}
  \graph [spring layout, nodes={vertex}, cooling factor=.1, convergence tolerance=.0001, node distance=25mm]
  {
    0 -- {1-- {2 -- 25,4,5-- {8 -- 12 -- 26,9-- 10 -- {18,20,22,23},11,24},6,7,16,17,21}, 3,13 -- 14,15,19}
    ;
  };
\end{tikzpicture}
\begin{tikzpicture}
  \graph [nodes={vertex}, node distance=25mm]
  {
    0 -- {1-- {2 -- 25,4,5-- {8 -- 12 -- 26,9-- 10 -- {18,20,22,23},11,24},6,7,16,17,21}, 3,13 -- 14,15,19}
    ;
  };
\end{tikzpicture}
\end{document}
2
  • How can you tell tikz to use splines instead of straight lines? (in order to avoid some edges crossing across some nodes, and jump instead)
    – skan
    Commented Nov 20, 2017 at 10:31
  • @skan I don't know what splines are, so I have no idea.
    – cfr
    Commented Nov 21, 2017 at 4:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .