Example: A complete graph

Published 2012-02-01 | Author: Jean-Noël Quintin

Download as: [PDF] [TEX]  •  [Open in Overleaf]

A complete graph

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de. En français: TeXnique.fr.

% A complete graph
% Author: Quintin Jean-Noël
% <http://moais.imag.fr/membres/jean-noel.quintin/>
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary[topaths]
% A counter, since TikZ is not clever enough (yet) to handle
% arbitrary angle systems.
\newcount\mycount
\begin{document}
\begin{tikzpicture}[transform shape]
  %the multiplication with floats is not possible. Thus I split the loop in two.
  \foreach \number in {1,...,8}{
      % Computer angle:
        \mycount=\number
        \advance\mycount by -1
  \multiply\mycount by 45
        \advance\mycount by 0
      \node[draw,circle,inner sep=0.25cm] (N-\number) at (\the\mycount:5.4cm) {};
    }
  \foreach \number in {9,...,16}{
      % Computer angle:
        \mycount=\number
        \advance\mycount by -1
  \multiply\mycount by 45
        \advance\mycount by 22.5
      \node[draw,circle,inner sep=0.25cm] (N-\number) at (\the\mycount:5.4cm) {};
    }
  \foreach \number in {1,...,15}{
        \mycount=\number
        \advance\mycount by 1
  \foreach \numbera in {\the\mycount,...,16}{
    \path (N-\number) edge[->,bend right=3] (N-\numbera)  edge[<-,bend
      left=3] (N-\numbera);
  }
}
\end{tikzpicture}
\end{document}

Comments

  • #1 percusse, April 22, 2013 at 3:36 p.m.

    Nice application. TikZ already has a powerful math library so you don't need any counters. \foreach loops can provide counters too. Example; you can replace your first foreach loop with

    \foreach \x in {1,...,8}{ \pgfmathparse{(\x-1)*45} \node[draw,circle,inner sep=0.25cm] (N-\x) at (\pgfmathresult:5.4cm) {}; }

  • #2 ChrisG, May 10, 2013 at 6:07 p.m.

    Thanks, this is very nice. Here's a further simplification. (I made the graph undirected but you can add the arrows back if you like.)

    \begin{tikzpicture}[transform shape]
      \foreach \x in {1,...,16}{%
        \pgfmathparse{(\x-1)*45+floor(\x/9)*22.5}
        \node[draw,circle,inner sep=0.25cm] (N-\x) at (\pgfmathresult:5.4cm) {};
      } 
      \foreach \x [count=\xi from 1] in {2,...,16}{%
        \foreach \y in {\x,...,16}{%
        \path (N-\xi) edge[-,bend right=3] (N-\y)  edge[-,bend left=3] (N-\y);
      }
    }
    \end{tikzpicture}
    
  • #3 ChrisG, May 10, 2013 at 10:02 p.m.

    Sorry, for an undirected graph, the double edge isn't really required, but I forgot to drop it. So either put the edge[- pieces to edge[-> and edge[<- as in the original for directed or replace it with the following for undirected.

    \begin{tikzpicture}[transform shape,line width=0.2pt]
      \foreach \x in {1,...,16}{%
        \pgfmathparse{(\x-1)*45+floor(\x/9)*22.5}
        \node[draw,circle,inner sep=0.25cm] (N-\x) at (\pgfmathresult:5.4cm) [thick] {};
      } 
      \foreach \x [count=\xi from 1] in {2,...,16}{%
        \foreach \y in {\x,...,16}{%
            \path (N-\xi) edge[-] (N-\y);
      }
    }
    \end{tikzpicture}
    

Adding comments is currently not enabled.