# How to color vertices in a TikZ graph?

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.

• Given arbitrary graph, you can't color it with only two colors. In your particular case n=4 this is possible, but in general the number of colors that you need is called the 'chromatic number' of the graph. General algorithme for this question with simple styling is in my opinion inpossible.
– Kpym
Commented Feb 27, 2015 at 22:54
• @kpym : I meant only for this style of graph, which can always be colored by two colors. Commented Feb 28, 2015 at 3:17

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
\fi
% set the color
\pgfmathparse{int(mod(\nodenum, 2))}
\pgfkeysalso{color\pgfmathresult, node contents=\the\nodenum}
% count this node and save globally
},
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}


• Nice! Also \def is enough for the first \j. Commented Feb 28, 2015 at 10:13
• @percusse thanks. Technically you have reason, but isn't it better to announce that some macro will be used globally by putting \xdef ? I have added a better version with counter in place of a macro, and without tikzmath.
– Kpym
Commented Feb 28, 2015 at 13:36
• You are already at the top scope. So everything you define will be global. If you need expansion \edef is also enough. Commented Feb 28, 2015 at 13:40