[Tex/LaTex] Finite complete graph of arbitrary size in Tikz

graphstikz-pgf

I want to present a complete graph of arbitrary finite size. This means a number of nodes which are interconnected to all other nodes. In addition I want to clearify that the number of nodes is not important. For numbers this would simply look like this:

$$1, \dots, n$$

I want to add one additional node at the side which is connected to all other nodes in the complete graph.

I am not looking for a manual how to do an exact representation in tikz which is most elegant. Those exists. I looking for a more abstract depicted which tells the viewer that this is a complete graph on the first sight. I am open to unorthodox solutions. Like a cloud of interconnected nodes with or without \dots e.g. … Any ideas?

Best Answer

Hmmm... less conventional request than it may appear. Here is a simple take on the problem. More/less nodes can be added at will.

\documentclass{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \foreach \x in {1,...,7}{%
    \pgfmathparse{(\x-1)*360/8}
    \node[draw,circle,inner sep=0.25cm] (N-\x) at (\pgfmathresult:5.4cm) [thick] {};
  }
  \pgfmathparse{7*360/8}
  \node[circle,red] (N-8) at (\pgfmathresult:5.4cm) {\ldots};
  \foreach \x in {1,...,7}{%
    \foreach \y in {\x,...,7}{%
        \path (N-\x) edge[ultra thin,-] (N-\y);
  }
  }
    \foreach \y in {1,...,8}{%
        \path (N-8) edge[red, very thick, loosely dotted] (N-\y);
  }
\end{tikzpicture}
\end{document}

and the output:

Another variant, with a node drawn in dotted line with or without \ldots inside requires some fiddling with the above code:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}
\begin{tikzpicture}[ultra thick,decoration={border,segment length=2mm,amplitude=0.3mm,angle=90}]
  \foreach \x in {1,...,7}{%
    \pgfmathparse{(\x-1)*360/8}
    \node[draw,circle,inner sep=0.25cm] (N-\x) at (\pgfmathresult:5.4cm) [thick] {};
  }
  \pgfmathparse{7*360/8}
  \node[draw,circle,red,inner sep=0.25cm,decorate] (N-8) at (\pgfmathresult:5.4cm) {};
  \foreach \x in {1,...,7}{%
    \foreach \y in {\x,...,7}{%
        \path (N-\x) edge[ultra thin,-] (N-\y);
  }
  }
    \foreach \y in {1,...,8}{%
        \path (N-8) edge[red, very thick, loosely dotted] (N-\y);
  }
\end{tikzpicture}
\end{document}

Of course, you can have the \ldots in the red dotted node, but you will have to set its inner sep to another value (I'd suggest 0.125cm).

EDIT:

@JLDiaz suggested to label the nodes and the last two should be labelled $n-1$ and $n$. That sounds cool, so I decided to do just that. The trick is to replace the node drawing line in the first foreach loop with:

\node[draw,circle] (N-\x) at (\pgfmathresult:5.4cm) [thick] {\pgfmathparse{(\x==2)?"$n$":((\x==1)?"$n-1$":int(\x-2))}\pgfmathresult};

There is a conditional in it, checking for the number of the node. Since the last node to be drawn is the one with the \ldots, the first should contain $n-1$ and the second $n$, so that is what the code does. int is needed to force TikZ to print an integer, otherwise you would have something like '1.0', etc. for labels. And then there is also minimum size=3.5em for all the nodes, which makes sure the nodes that contain only a number will be as large as the one that must accommodate $n-1$ - em is chosen to scale if the font size changes.

Full code:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}
\begin{tikzpicture}[ultra thick,every node/.style={minimum size=3.5em},decoration={border,segment length=2mm,amplitude=0.3mm,angle=90}]
  \foreach \x in {1,...,7}{%
    \pgfmathparse{(\x-1)*360/8}
    \node[draw,circle] (N-\x) at (\pgfmathresult:5.4cm) [thick] {\pgfmathparse{(\x==2)?"$n$":((\x==1)?"$n-1$":int(\x-2))}\pgfmathresult};
  }
  \pgfmathparse{7*360/8}
  \node[draw,circle,red,inner sep=0.25cm,decorate] (N-8) at (\pgfmathresult:5.4cm) {};
  \foreach \x in {1,...,7}{%
    \foreach \y in {\x,...,7}{%
        \path (N-\x) edge[ultra thin,-] (N-\y);
  }
  }
    \foreach \y in {1,...,8}{%
        \path (N-8) edge[red, very thick, loosely dotted] (N-\y);
  }
\end{tikzpicture}
\end{document}

... and ta-dah:

enter image description here

(Note: If I come up with something smarter, I'll edit my answer...)