[Tex/LaTex] How to color vertices in a TikZ graph

graphicsgraphstikz-pgf

Consider the following code to draw a graph using TikZ:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{graphs,graphs.standard}

\begin{document}

\begin{tikzpicture}
  \graph [nodes={draw, circle}, clockwise, radius=.75cm, empty nodes, n=4] {
    subgraph C_n [name=inner] -- [shorten <=1pt, shorten >=1pt]
    subgraph C_n [name=outer]
};
\end{tikzpicture}

\end{document}

which was adapted from the documentation. Is there a nice way to obtain a graph coloring of this graph (with two colors). That is, no two vertices connected by an edge have the color?

Because this is a simplification of what I'm actually trying to do, I would prefer a solution which didn't manually place the vertices, unless there was an automated way to do so. In particular, I'm looking for a solution that doesn't make us of the fact n=4.

Best Answer

Here is a solution for the graph of your example with n even. If n is odd there is no such coloring.

I use math library to make the logic, but if you wan you can replace this part of the code by vanilla tex solution.

\documentclass[tikz, border=7pt]{standalone}
\usetikzlibrary{graphs,graphs.standard,math}

\xdef\j{0} % the node number is stored globally
\tikzset{
  color0/.style = {fill=red!35},
  color1/.style = {fill=blue!35},
  setcolor/.code = {
    \tikzmath{
      int \j, \k;
      \j = \j+1;
      \k = mod((\j <= #1 ? \j+1:\j), 2);
    }
    \xdef\j{\j}
    \pgfkeysalso{node contents=\j, color\k}
  },
  graphs/mygraph/.style = {
    nodes={draw, circle, setcolor=#1}, clockwise, radius=#1*.25cm, empty nodes, n=#1
  }
}

\begin{document}
  \begin{tikzpicture}
    \graph [mygraph=4] {
      subgraph C_n [name=inner] -- [shorten <=1pt, shorten >=1pt]
      subgraph C_n [name=outer]
    };
  \end{tikzpicture}
\end{document}

enter image description here

With mygraph=10 :

enter image description here

EDIT (after this answer has been accepted): Here is a code without tikzmath and such that the node counter is automatically reset at the begining of the graph. This allows you to draw more than one graph without reseting the counter manually.

\documentclass[tikz, border=7pt]{standalone}
\usetikzlibrary{graphs,graphs.standard}

\newcount\nodenum % the node number is stored globally
\tikzset{
  color0/.style = {fill=red!35},
  color1/.style = {fill=blue!35},
  setcolor/.code = {
    % if we start the second part
    \ifnum \nodenum = #1
      \global\advance\nodenum 1\relax
    \fi
    % set the color
    \pgfmathparse{int(mod(\nodenum, 2))}
    \pgfkeysalso{color\pgfmathresult, node contents=\the\nodenum}
    % count this node and save globally
    \global\advance\nodenum 1\relax
  },
  graphs/mygraph/.style = {
    nodes={draw, circle, setcolor=#1}, clockwise, radius=#1*.25cm, empty nodes, n=#1
  },
  graphs/mygraph/.append code={
    \global\nodenum 0\relax % reset the counter at the beginning
  }
}

\begin{document}
  \begin{tikzpicture}
    \graph [mygraph=4] {
      subgraph C_n [name=inner] -- [shorten <=1pt, shorten >=1pt]
      subgraph C_n [name=outer]
    };

    \graph [mygraph=12] {
      subgraph C_n [name=inner] -- [shorten <=1pt, shorten >=1pt]
      subgraph C_n [name=outer]
    };
  \end{tikzpicture}
\end{document}

enter image description here