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}
With mygraph=10
:
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}